- #1
jersiq1
- 7
- 0
For one of my assignments, I have to write a program to add or subtract complex numbers. I wrote a class, and the problem I am having is that one of my methods isn't returning the data to the calling object. I am going to omit the things in my code that I don't think are pertinent.
Here's the Class:
I added the print function within the add function just to make sure it was adding, and it is. However, it isn't returning the values of added_numbers. I guess I am just really failing to understand why.
Code:
int main()
{
Complex num1(2,5),num2(7,8),num3,num4;
num3.add(num1,num2); //this is the function that isn't working as expected
num3.print(); //I am not getting the proper output here. Just the values of the default constructor
cin.get();
return 0;
}
Here's the Class:
Code:
class Complex
{
public:
Complex();
Complex(int , int );
~Complex();
Complex add(Complex &,Complex &);
Complex sub(Complex &,Complex &);
void print();
private: //data accessed by public functions
int real;
int imaginary;
};
Complex Complex::add(Complex & c1, Complex & c2)
{
Complex added_numbers;
added_numbers.real = c2.real+ c1.real;
added_numbers.imaginary = c2.imaginary + c1.imaginary;
added_numbers.print(); //I added this to make sure the addition was occurring
return added_numbers;
}
}
I added the print function within the add function just to make sure it was adding, and it is. However, it isn't returning the values of added_numbers. I guess I am just really failing to understand why.