Let's take a step back. What should
c.sum(a,b)
actually do?
1. should it add a and b, and set c to the sum?
2. should it add a and b, and return the sum? In that case why is c involved?
Your main program looks like it assumes #1. To make that work, you should change the definition of sum() back to returning void and have it modify
this
.
void ComplexNum::sum(ComplexNum a, ComplexNum b)
{
this->real = a.real + b.real;
this->imaginary = a.imaginary + b.imaginary;
}
printComplex() won't compile. Remember, in order to print out the value of your own class, you have to print the values of the appropriate members. For now, let's just print out the real and imaginary values:
1 2 3 4
|
void ComplexNum::printComplexNum()
{
cout << real << ',' << imaginary;
}
| |
Later when you have some of the code working, you can change this to print it in the format requested.
Your main program is wrong. You don't define a or b. Also why are you getting c from the user? Isn't c supposed to hold the result? It should be :
1 2 3 4 5 6 7 8 9 10
|
int main()
{
ComplexNum a,b,c;
a.getComplexNum(); // get the number a
b.getComplexNum(); // get the number b
c.sum(a, b); // add a and b, storing the result in c
c.printComplexNum(); // print c
return 0;
}
| |
Before you do prod() and square(), be sure to review how one multiplies complex numbers. You have to be sure to get the math right.
Once you have prod() working, can you code square() using prod()? After all, square() just multiplies a number by itself, why not use prod() for the multiplication?