/*复数的加减运算
算法:运算符的重载函数
by adengou 2011.8.12
windows 7 dev c++5.0 和vs 2010编译通过、
*/
#include <iostream>
using namespace std;
class Complex
{
private:
double real,imag;//实部和虚部变量
public:
Complex(idouble a=0,double b=0)
{real=a;imag=b;}
Complex operator+( Complex b)//运算符+号重载
{
Complex c;
c.real=real+b.real;
c.imag=imag+b.imag;//实部相加,虚部相加
return Complex(c.real,c.imag);
}
Complex operator-( Complex b)//运算符-号重载
{
Complex c;
c.real=real-b.real;
c.imag=imag-b.imag;//实部相减,虚部相减
return Complex(c.real,c.imag);
}
void add()
{
cout<<"sum("<<real<<","<<imag<<")"<<endl;
}
};
int main()
{
Complex A(2,3), B(4,5) ,C(6 ,7),O,P,Q;//定义6个复数
O=A+B+C;//三个复数相加
cout<<"复数A+B+C=";O.add();
P=B+C;
cout<<"复数B+C=";P.add();
Q=P-O;//两复数相减
cout<<"复数P-O=";Q.add();
system("pause");
return 0;
}