# Class problem...

• 04-30-2005
webren
Class problem...
Hello,
I am trying to write a program that will simply output the sum and product of complex numbers. I am having a difficult time getting it to work properly. Any ideas would be appreciated.

Code:

```#include <iostream> using namespace std; class complex { public:         void printNumber() const;         int getReal() const;         int getImaginary() const;         complex addNumbers();         complex multiplyNumbers();         cons(); private:         int a;         int b; }; int main() {         complex x = new complex(3,4);     complex y = new complex(9,8);         cout << "When the program first executes you have empty vales: ";         x.printNumber();         y.printNumber();         cout << endl << endl;         cout << "When you add the complex numbers, you'll get this: ";         x.getReal(x);         y.getReal(y);         x.addNumbers(x);         y.addNumbers(y);         x.printNumber(x);         y.printNumber(y);     cout << endl << endl;         cout << "When you multiple the complex numbers, you'll get this: ";         x.getReal(x);         y.getReal(y);         x.multiplyNumbers(x);         y.multiplyNumbers(y);         x.printNumber();         y.printNumber()     cout << endl << endl;     return 0; } void complex::printNumber() const {         cout << a << " + " << b << "i"; } int complex::getReal() const {         x = a;         return x; } complex complex::getImaginary() const {         y = b;         return complex; } complex complex::addNumbers (x) {         x.getReal();         x = a + b;         return complex; } complex complex::multiplyNumbers (y) {         y.getImaginary();         result = a * b;         return complex; }```
• 04-30-2005
Zach L.
Your functions addNumbers and multiplyNumbers are not declared as taking parameters. Additionally, you have to specify what type of parameter the function should expect. An example:

Code:

```ret_type fname(param1_type param1, param2_type param2, ..., paramN_type paramN); int foo(int x);```
Also, please be more specific about what problems (runtime, compile-time, what errors or behavior you are getting, etc) in future posts.

Cheers
• 05-01-2005
7stud

Code:

```int someFunc(double p1); ... ... int className::someFunc (double p1) {         return someInt; }```
• 05-01-2005
hk_mp5kpdw
Code:

```complex x = new complex(3,4); complex y = new complex(9,8);```
For that to work, your variables would first need to be pointers and you would need a constructor declared which accepts two parameters. As it is, I don't think you want to be using pointers so you can get rid of the new operator and stick to something more standard, i.e.:

Code:

```complex x(3,4); complex y(9,8);```
You will still need to code up your constructor to handle the above.

Multiplication and addition of your complex number objects would be better served by overloading the addition (+) and multiplication (*) operators since the operations then would be more intuitive when expressed through your program's code. I posted something demonstrating this (just the + operator) in the past couple weeks here.