第8章多态性 例8.2(续一) void CComplex. Printo cout≤<"("<srea<<","<<imag≤")"<<endl; CComplex CComplex:: operator +(CComplex c) CComplex temp temp. real= real c real; temp. imag= imag +c imag return temp; CComplex CComplex operator-(CComplex c) CComplex temp: 1temp. real=real-creal temp. imag= imag-C imag return temp
例8.2 (续一) void CComplex::Print() { cout << "(" << real << "," << imag << ")" << endl; } CComplex CComplex::operator +(CComplex c) { CComplex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } CComplex CComplex::operator -(CComplex c) { CComplex temp; temp.real = real - c.real; temp.imag = imag - c.imag; return temp; } 第8章 多态性
第8章多态性 例8.2(续二) CComplex CComplex: operator *(CComplex c) CComplex temp 总结:设有双目运算符B,如果要 temp. real= real *c real: Imag"CImag:;重载B为类的成员函数,使之能够 temp. Imag=rea·cmag+ imag *c real;实现表达式 oprd1 B oprc2,其中 return temp; oprd1为A类对象,则B应被重载 void main(void) 为A类的成员函数,形参类型应该 是oprd2所属的类型。 CComplex a(1, 2), b(3.0, 4.0),c, d, e, f; 经重载后,表达式 oprd1 B C= a+b oprd2相当于oprd1 operator d e a-b B(oprd2),注意重载双目运算符只 f=a+1: 需要一个参数。 cout <<c C Printo cout <<"d d Printo 程序运行结果为: cout <<"e=. e Print(; c=(4,6) cout<"=";d=(2,-2) f Print(; e=(-5,10) f=(2,2
例8.2 (续二) CComplex CComplex::operator *(CComplex c) { CComplex temp; temp.real = real * c.real - imag * c.imag; temp.imag = real * c.imag + imag * c.real; return temp; } void main(void) { CComplex a(1, 2), b(3.0, 4.0), c, d, e, f; c = a+b; d = a-b; e = a*b; f = a+1; cout << "c = "; c.Print(); cout << "d = "; d.Print(); cout << "e = "; e.Print(); cout << "f = "; f.Print(); } 第8章 多态性 程序运行结果为: c = (4, 6) d = (-2, -2) e = (-5,10) f = (2, 2) 总结:设有双目运算符 B,如果要 重载 B 为类的成员函数,使之能够 实现表达式oprd1 B oprd2,其中 oprd1 为A 类对象,则B 应被重载 为 A 类的成员函数,形参类型应该 是 oprd2 所属的类型。 经重载后,表达式oprd1 B oprd2 相当于 oprd1.operator B(oprd2),注意重载双目运算符只 需要一个参数
第8章多态性 8.2运算符重载为类的成员函数 822单目运算符重载 单目运算符,如果重载为类的成员函数,不需要参数 为区分前置和后置运算符,C++规定: 对于前置单目运算符,重载函数没有参数 对于后置单目运算符,重载函数有一个整型参数,这个整型 参数没有其他用途,只是用于区分前置运算与后置运算
8.2 运算符重载为类的成员函数 8.2.2 单目运算符重载 单目运算符,如果重载为类的成员函数,不需要参数。 为区分前置和后置运算符,C++规定: 对于前置单目运算符,重载函数没有参数 对于后置单目运算符,重载函数有一个整型参数,这个整型 参数没有其他用途,只是用于区分前置运算与后置运算。 第8章 多态性
第8章多态性 例8.3定义一个Cnt类,类中只有一个数据成员i,两个运 符“++"的重载函数,一个没有参数,实现的是前置运算符重 载,另一个有一个整型参数,实现后置运算符重载。 include iostream h void CInt: PrintO class cInt cout <<i=<<i<< end private int i: CInt cInt: operator ++o publIc CInt(nt a=O) CInt temp void Printo temp. i =++i; CInt operator ++0: return temp CInt operator ++(int) CInt CInt: operator ++(int) CInt: CInt (int a) CInt temp a temp. i=1++ return temp
例8.3 定义一个CInt类,类中只有一个数据成员i,两个运算 符“++”的重载函数,一个没有参数,实现的是前置运算符重 载,另一个有一个整型参数,实现后置运算符重载。 #include "iostream.h" class CInt { private: int i; public: CInt(int a=0); void Print(); CInt operator ++(); CInt operator ++(int); }; CInt::CInt (int a) { i = a; } 第8章 多态性 void CInt::Print() { cout << "i=" << i << endl; } CInt CInt::operator ++() { CInt temp; temp.i = ++i; return temp; } CInt CInt::operator ++(int) { CInt temp; temp.i = i++; return temp; }
第8章多态性 例83(续) void main(void) CInt a(5), b(5), C, d c=a++; d=++b; cout <<"a a Printo cout <<"b b Printo cout <<"c Printo cout <<"d 程序运行结果为: d Printo a:i=6 i=6 bcd 6
例8.3 (续) void main(void) { CInt a(5), b(5), c, d; c = a++; d = ++b; cout << "a: "; a.Print(); cout << "b: "; b.Print(); cout << "c: "; c.Print(); cout << "d: "; d.Print(); } 第8章 多态性 程序运行结果为: a: i=6 b: i=6 c: i=5 d: i=6