正在加载图片...
5:06:48 例:复数类 class complex float real, image, public: Complex(float r=0, float i=0)real=r; image=i Complex operator +(const Complex right friend Complex opera tor -(const Complex left const Complex & right) ComplexComplex:: operator +(const Complex right)t Complex temp(real+right. real, image+right image) eturn t emt° Complex c1(1,2),c2(3,4); Complex c3=c1 +c2 Complex c4=c1-c2 注意: 元运算符重载对应的成员函数由左操作数调用,意即 c1+c2 c1 operator+(c2)15:06:48 例:复数类 class Complex{ float real,image; public: Complex(float r=0,float i=0){real=r;image=i;} Complex operator +(const Complex & right); friend Complex operator -(const Complex & left, const Complex & right); }; Complex Complex::operator +(const Complex & right){ Complex temp(real+right.real, image+right.image); return temp; } Complex operator-(const Complex& left, const Complex& right){ Complex temp(left.real-right.real, left.image-right.image); return temp; } Complex c1(1,2),c2(3,4); Complex c3 = c1 + c2; Complex c4 = c1 – c2; 注意: 二元运算符重载对应的成员函数由左操作数调用,意即 c1+c2  c1.operator+(c2)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有