# 关于运算符重载的问题

szj2006 2008-03-15 09:40:24

include<iostream.h>
class Complex
{
private:
double real;
double image;
public:
Complex(void):real(0),image(0)
{}
Complex(double rp):real(rp),image(0)
{}
Complex(double rp,double ip):real(rp),image(ip)
{}
~Complex()
{}

Complex operator+(const Complex &x)const;
Complex operator-(const Complex &x)const;
Complex operator*(const Complex &x)const;
Complex operator/(const Complex &x)const;
bool operator==(const Complex &x)const;
Complex& operator+=(const Complex &x);
void Print(void) const;
};

inline Complex Complex::operator+(const Complex &x) const
{
return Complex(real+x.real,image+x.image );
}

inline Complex Complex::operator-(const Complex &x) const
{
return Complex(real-x.real,image-x.image );
}

inline Complex Complex::operator*(const Complex &x) const
{
return Complex(real*x.real,image*x.image );
}

Complex Complex::operator/(const Complex &x)const
{
double m;
m=x.real*x.real+x.image*x.image;
return Complex((real*x.real+image*x.image)/m,(image*x.real-real*x.image)/m);
}

inline bool Complex::operator==(const Complex &x)const
{
return bool(real==x.real &&image==x.image);
}

Complex&Complex::operator+=(const Complex&x)
{
real+=x.real ;
image+=x.image ;
return *this;
}
void Complex::Print(void ) const
{
cout<<"("<<real<<","<<image<<"i)"<<endl;
}
...全文
49 2 打赏 收藏 举报

2 条回复

szj2006 2008-03-15
• 打赏
• 举报

paidfighting 2008-03-15
• 打赏
• 举报

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-03-15 09:40

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下