问题补充说明:定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”, 使之能用于复数的加,减,乘,除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数的和,差,积,商。
#include<iostream>
usingnamespacestd;
classCComplex
{
private:
floatreal,image;
public:
CComplex(){}
CComplex(f采价放哪毫担片针督检loatr,floatimg){real=r;image=img;}
CComplex(CComplex&another)
{
real=another.real;
image=another.image;
}
CComplex&operator=(CComplex&another)
{
real=another.real;
image=another.image;
return*this;
}
CComplexoperator+(CComplex&another)
{
re来自turnCComplex(real+another.real,i360问答mage+another.image);
}
CComplexoperator-(CComplex&another)
{
returnCComplex(real-another.real,image-another.image);
}
CComplexoperator*(C准水笔赶Complex&another)
{
CComplexprod;
//prod=*this;
prod.real=real*anoth若承差er.real-image*another.image;
prod.image=real*another.image+image*another.real;
retu差适力程脚空罗和时曲言rnprod;
//returnCComplex(r友这势牛报易轻重车供eal+another械预吧掌围市依耐比值.real,image+another.i扩项题mage);
}
CComplexoperator/(CComplex&another)
{
CComplexquot;
f称果费loatsq=another.real*another.real+将是续杂看another.image*another.image;
quot.real=(real*another.real+image*another.image)/sq;
quot.image=(image*another及呀音呼际形.real-real*another.image)/sq;
returnquot;
}
};
voidmain()
{
CComplexc1(2,3),c2(3,3);
CComplexc4,c5,c6,c7;
c4=c1+c2;
c5=c1-c2;
c6=c1*c2;
c7=c1/c2;
}
标签:运算符,C++,Complex