警察特训营之搜救犬:定义一个复数类complex,重载运算符

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/06 02:29:49
定义一个复数类complex,重载运算符"+","_","*","/",使之能用于复数的加,减,乘,除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积和商。

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;
}