Write a program in C++ for adding two complex numbers using binary operator overloading - WAY2IT

Latest Post :

Home Top Ad

Write a program in C++ for adding two complex numbers using binary operator overloading



Code :-

#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex(){}
complex(float r,float i)
{
x=r;
y=i;
}
complex operator+(complex);
void display()
{
cout<<x<<"+"<<y<<"i"<<endl;
}
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void main()
{

complex c1,c2,c3;
c1=complex(10,20);
c2=complex(3,54);
c3=c1+c2;
clrscr();
cout<<" ";c1.display();
cout<<"+ ";c2.display();
cout<<"-----------"<<endl;
cout<<" ";c3.display();
getch();
}


Output :-