#include <iostream.h>
class Complex //复数类
{
private:
double real,image;
public:
Complex(double x=0.0,double y=0.0)//构造函数
{
real=x;
image =y;
}
Complex operator -(const Complex &c); //减法操作,双目运算符
bool operator ==(const Complex &c); //关系判断相等,双目运算符
Complex operator -(); //求负取反,单目运算符
Complex operator +=(const Complex &c);//叠加操作,双目运算符
void Print();
};
void Complex::Print()
{
cout<<real<<"+"<<image<<"i"<<endl; //以复数格式输出
}
Complex Complex ::operator -(const Complex &c)
{
Complex temp(real-c.real,image-c.image);
return temp;
}
bool Complex ::operator ==(const Complex &c)
{
return (real==c.real && image==c.image);//判断分量是否全相等
}
Complex Complex ::operator -()
{
return Complex(-real,-image);//返回一个无名对象
}
Complex Complex ::operator +=(const Complex &c)
{
real+=c.real; //成员叠加
image+=c.image;
this->image=12;
Complex temp(real,image);
return temp; //返回对象的引用
}
int main()
{
Complex c1(2,7),c2(4,2),c3;
c3=c1-c2; //表达式的完整形式应该是 c3=c1.operator-(c2)
c3.Print();
if(c3==c1)cout<<"c3 equals to c1"<<endl;//判断c3和c1是否相等c3.operator==(c1)
else cout<<"c3 doesn’t equale to c1"<<endl;
c3=-c2; //把c3赋值为c2的求负取反值operator -()
c3.Print();
c3+=c2;
c3.Print();
return 0;
}
class Complex //复数类
{
private:
double real,image;
public:
Complex(double x=0.0,double y=0.0)//构造函数
{
real=x;
image =y;
}
Complex operator -(const Complex &c); //减法操作,双目运算符
bool operator ==(const Complex &c); //关系判断相等,双目运算符
Complex operator -(); //求负取反,单目运算符
Complex operator +=(const Complex &c);//叠加操作,双目运算符
void Print();
};
void Complex::Print()
{
cout<<real<<"+"<<image<<"i"<<endl; //以复数格式输出
}
Complex Complex ::operator -(const Complex &c)
{
Complex temp(real-c.real,image-c.image);
return temp;
}
bool Complex ::operator ==(const Complex &c)
{
return (real==c.real && image==c.image);//判断分量是否全相等
}
Complex Complex ::operator -()
{
return Complex(-real,-image);//返回一个无名对象
}
Complex Complex ::operator +=(const Complex &c)
{
real+=c.real; //成员叠加
image+=c.image;
this->image=12;
Complex temp(real,image);
return temp; //返回对象的引用
}
int main()
{
Complex c1(2,7),c2(4,2),c3;
c3=c1-c2; //表达式的完整形式应该是 c3=c1.operator-(c2)
c3.Print();
if(c3==c1)cout<<"c3 equals to c1"<<endl;//判断c3和c1是否相等c3.operator==(c1)
else cout<<"c3 doesn’t equale to c1"<<endl;
c3=-c2; //把c3赋值为c2的求负取反值operator -()
c3.Print();
c3+=c2;
c3.Print();
return 0;
}