You are on page 1of 3

Complex Number Class

============================================
#include <iostream.h>

class Complex{
public:
Complex(){real=0;imag=0;}
Complex(int real, int imag)
{
this->real=real;
this->imag=imag;
}
void setReal(int real){this->real=real;}
int getReal(){return real;}

void setImag(int imag){this->imag=imag;}


int getImag(){return imag;}

void operator=(Complex p)
{
real=p.real;
imag=p.imag;
}

Complex operator+(Complex p)
{
p.setReal(real+p.getReal());
p.setImag(imag+p.getImag());
return p;

}
Complex operator-(Complex p)
{
p.setReal(real-p.getReal());
p.setImag(imag-p.getImag());
return p;
}
Complex operator*(Complex p)
{
p.setReal(real*p.getReal());
p.setImag(imag*p.getImag());
return p;
}
bool operator==(Complex p)
{
if(real==p.getReal() && imag==p.getImag())
return true;
else
return false;

bool operator!=(Complex p)
{
if(real!=p.getReal() && imag!=p.getImag())
return true;
else
return false;

void print()
{
cout<<"("<<real<<","<<imag<<"i)";
}

private:
int real;
int imag;

};

void main()
{
Complex a(10,2),b(10,1),c;
cout<<"Input Values are \t(10,2i)\tand\t(10,1i)\n\n\n";
c=a+b;
cout<<"Result of Operator + is: ";
c.print();
c=a-b;
cout<<"\nResult of Operator - is: ";
c.print();
c=a*b;
cout<<"\nResult of Operator * is: ";
c.print();
cout<<endl<<endl;
cout<<"============================";
cout<<endl;
cout<<"\na = ";
a.print();
cout<<endl;
a =a + b;
b =b - a;
a = a * a;
c = a;
if(a==b){
cout<<"\na = ";
a.print();
cout<<endl;
}

else if (b!=c){
cout<<"\nb = ";
b.print();
cout<<endl;
}
cout<<"\nc = ";
c.print();
cout<<"\n\n============================\n\n";

http://www.ravianeducation.blogspot.com
FARHAN: 03008855006