易到私家车加盟条件:定义一个复数类complex,重载运算符
来源:百度文库 编辑:神马品牌网 时间:2024/05/10 06:16:40
class complex{
public:
complex(double r=0,double i=0);//构造
complex operator+(const complex& other);//重载加法运算符
complex operator-(const complex& other);//重载减法
complex operator*();//重载乘法
complex operator/(const complex& other);//重载除法
protected:
double real,image;
};
complex::complex(double r,double i)
{
real=r;
image=i;
return;
}
complex complex::operator+(const complex& other)
{
complex temp;
temp.real=real+other.real;
temp.image=image+other.image;
return temp;
}
complex complex::operator-(const complex& other)
{
complex temp;
temp.real=real-other.real;
temp.image=image-other.image;
return temp;
}
complex complex::operator*(const complex& other)
{
complex temp;
temp.real=real*other.real-image*other.image;
temp.image=image*other.real+real*other.image;
return temp;
}
complex complex::operator+(const complex& other)
{
complex temp;
temp.real= //这里有点多了,自己写吧。。。
temp.image=
return temp;
}