/* C++ program to overload =, -, *, / operator */
#include <iostream>
using namespace std;
class complex
{
private:double r;
double i;
public: complex(double,double);
complex();
void showdata();
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
};
complex :: complex(double a,double b)
{
r=a;
i=b;
}
complex :: complex()
{
}
void complex :: showdata()
{
cout<<r<<" + "<<i<<" i";
}
complex complex :: operator+(complex A)
{
complex R;
R.r=r+A.r;
R.i=i+A.i;
return(R);
}
complex complex :: operator-(complex A)
{
complex R;
R.r=r-A.r;
R.i=i-A.i;
return(R);
}
complex complex :: operator*(complex A)
{
complex R;
R.r=(r*A.r)-(i*A.i);
R.i=(r*A.i)+(i*A.r);
return(R);
}
complex complex :: operator/(complex A)
{
complex R;
R.r=((r*A.r)+(i*A.i))/((A.r*A.r)+(A.i*A.i));
R.i=((i*A.r)-(r*A.i))/((A.r*A.r)+(A.i*A.i));
return(R);
}
int main()
{
complex c1(2,4);
cout<<"\n\tThe 1st complex number: ";
c1.showdata();
complex c2(3,4);
cout<<"\n\tThe 2nd complex number: ";
c2.showdata();
complex c3;
c3=c1+c2;
cout<<"\n Result of Addition: ";
c3.showdata();
c3=c1-c2;
cout<<"\n Result of Subtraction: ";
c3.showdata();
c3=c1*c2;
cout<<"\n Result of Multiplication: ";
c3.showdata();
c3=c1/c2;
cout<<"\n Result of Division: ";
c3.showdata();
return 0;
}
No comments:
Post a Comment