//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...
// Operator overloaded using a member functionComplex 
Complex Complex::operator+( Complex &other )
{
         return Complex( re + other.re, im + other.im );
}

I hope this helps...

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.

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

Exactly!

What problem are you facing ????

I'm not facing any problems!

This article has been dead for over six months. Start a new discussion instead.