Thursday, April 1, 2010

C++ PROGRAM TO ADD, SUBTRACT AND MULTIPLY TWO COMPLEX NUMBERS USING OPERATOR OVERLOADING

/*C++ program to add, subtract and multiply two complex numbers using operator overloading*/
#include
#include
class complex
{
float x,y;
public:
complex() {}
complex(float real,float img)
{
x=real; y=img;
}
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
void display()
{
cout< }
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
complex complex::operator-(complex d)
{
complex temp;
temp.x=x-d.x;
temp.y=y-d.y;
return(temp);
}
complex complex::operator*(complex e)
{
complex temp;
temp.x=x*e.x+y*e.y*(-1);
temp.y=x*e.y+y*e.x;
return(temp);
}
void main()
{
clrscr();
complex c1(5,3),c2(3,2),c3=c1+c2,c4=c1-c2,c5=c1*c2;
c1.display();
c2.display();
cout<<"Addition"< c3.display();
cout<<"Subtraction"< c4.display();
cout<<"Multiplication"< c5.display();
getch();
}