C++ Program to demonstrate binary operator overloading [DEVCPP/GCC]

We have already discussed about Operator Overloading in C++ in our previous post: http://techcpp.blogspot.com/2017/07/operator-overloading.html. Here is a simple program on it.

PROGRAM

//Program to demonstrate operator (Binary +) overloading 

# include <iostream>
using namespace std;

class complex
{
int real,imag;

public:

complex()
{
real=0;
          imag=0;
}
complex( int x,int y )
{
real=x;
          imag=y;
}

    complex operator + (complex c)  
{                                                   
complex temp;
temp.real= real+c.real;    
temp.imag= imag+c.imag;
return temp;
}

void show()
{
cout<<"Complex number is:"<<real<<"+"<<imag<<"i";
}
};

int main()
{
complex c1(1,2),c2(2,3),c3;
c3=c1+c2;                                          //c1.operator+(c2)
c3.show();
return 0;
}

OUTPUT


Share this

Related Posts

FIND US ON FACEBOOK!