0

whenever I run the program, there is no output, the program just ends. Am i doing something wrong? I'm sure there's something i missed but i can't seem to figure it out.

#include <iostream>
#include <string>

using namespace std;

class Addr
{
public:
    Addr(int i = 0){
        total = i;
    }

void addNum(int num){
    total += num;
}

int getNum(){ 
    return total; }

friend int print(Addr& var);

private:
   int total;
};

int print(Addr& var){
    return var.total;
}

int main()
{
    Addr object1;
    object1.addNum(3);
    print(object1);

    return 0;
 }
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

Your program behaves correctly. There is no output because you are not printing anything to the console in your program.

The print function merely returns the total.

If you wish to print the value to the console then you could for example change the definition as follows:

int print(Addr& var){
    cout << var.total << endl; // this prints to the console output
    return var.total;
}
Ely
  • 10,860
  • 4
  • 43
  • 64
0

There is no issue with your code. The fact is that no print function is used. I have modified your main function.

int main()
{
    Addr object1;
    object1.addNum(3);

    cout<<print(object1);

    return 0;
}
pkthapa
  • 1,029
  • 1
  • 17
  • 27