Thread: Class problem...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    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;
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    In addition, your function declarations/prototypes need to match the function header in your definitions:

    Code:
    int someFunc(double p1);
    ...
    ...
    int className::someFunc (double p1) {
    	return someInt;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM