1
//The following example overloads the + operator to add two complex numbers and returns the result.
// operator_overloading.cpp// compile with: /EHsc
#include <iostream>
using namespace std;
class Complex
{
public:   
         Complex( double r, double i ) : re(r), im(i) {}   
         Complex operator+( Complex &other );   
         void Display( ) 
         {   
                  cout << re << ", " << im << endl; 
         }
private:   
         double re, im;
};

// Operator overloaded using a member functionComplex Complex::operator+( Complex &other )
{
         return Complex( re + other.re, im + other.im );
}

int main()
{   
         Complex a = Complex( 1.2, 3.4 );   
         Complex b = Complex( 5.6, 7.8 );  
         Complex c = Complex( 0.0, 0.0 );  
         c = a + b;  
         c.Display();
}
//i cant run my program...need help...
4
Contributors
4
Replies
5
Views
11 Years
Discussion Span
Last Post by Micko
1
// Operator overloaded using a member functionComplex 
Complex Complex::operator+( Complex &other )
{
         return Complex( re + other.re, im + other.im );
}

I hope this helps...

1

Hey Micko, your program properly compiles and gives the expected output. What problem are you facing ????

My output:

6.8, 11.2
Press ENTER to continue.

Hope it helped,bye.

1

Hey Micko, your program properly compiles and gives the expected output.

Exactly!

What problem are you facing ????

I'm not facing any problems!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.