Thread: Multiplying overloaded operator

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Multiplying overloaded operator

    I'm working on this complex function to multiply two complex numbers. A and B are not in the scope of the overloaded function. How do I get the function to read them; do I have to make a separate getFunction()?

    Code:
    #include <iostream>
    using namespace std;
    
    class Complex
    {
    private:
    
        double real;
        double img;
    
    public:
    
        Complex( double realPart = 0.0, double imagPart = 0.0 ) : real(realPart), img(imagPart)
        {
        } // default constructor
    
        ~Complex(); // destructor
    
        friend Complex operator*( const Complex &left, const Complex &right );
        friend ostream &operator<<( ostream &output, const Complex &right );
        bool operator!() const; // is entry empty? ***
    
    }; // end class Complex
    
    Complex operator*( const Complex &left, const Complex &right ) 
    {
        Complex tmp;
        tmp.real = A.real * B.real - A.img * B.img;
        tmp.img = A.real * B.img + B.real * A.img;
        cout << "C is A * B: " << tmp << "i" << endl;
        // return tmp;
    }
    
    ostream &operator<<( ostream &output, const Complex &right )
    {
        return output << "(" << right.real << ") + (" << right.img << ")i";
    }
    
    bool Complex::operator !() const // is the entry empty?
    {
    	cout << "Invalid entry." << endl;
    	real == 0.0;
    	img == 0.0;
    }
    
    Complex::~Complex()
    { 
    }
    
    int main()
    {
        Complex A(-2.5,2.0), B(5.2,1.0), C;
        C = A * B;
    
        return 0;
    }
    THE redheaded stepchild.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Didn't you mean
    Code:
    Complex operator*( const Complex &left, const Complex &right ) 
    {
        Complex tmp;
        tmp.real = left.real * right.real - left.img * right.img;
        tmp.img = left.real * right.img + left.real * right.img;
        cout << "C is A * B: " << tmp << "i" << endl;
        return tmp;
    }
    Kurt

  3. #3
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Well ha! I tried that initially, but had a couple of them switched, so I wasn't getting the correct result. Thanks much for your time Kurt!
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM