class complex
{
public:
double real,imag;
complex(double r=0.0,double i=0.0):real(r),imag(i){ };
complex operator-(const complex &c);
};
complex operator+(const complex &a,const complex &b)
{
return complex(a.real+b.real,a.imag+b.imag);//返回临时对象
}//重载为普通函数,参数个数 运算符目数
complex::operator-(const complex &c)
{
return complex(real-c.real+imag-c.imag);//返回临时对象
//重载为成员函数,参数个数为 运算符目数-1
} //real 为当前对象所指
C++:运算符重载
最新推荐文章于 2022-09-10 16:04:53 发布
本文详细介绍了在C++中如何实现复数类的运算符重载,包括加法和减法运算符的重载过程。通过示例代码展示了如何创建复数类,并在类中重载加法和减法运算符,以便于进行复数之间的数学运算。
843

被折叠的 条评论
为什么被折叠?



