5.3.3友元运算符函数 把运算符函数定义成某个类的友元函数,称为友元运算函数符 1.友元运算符函数定义的语法形式 友元运算符函数与成员运算符函数不同,后者本身是类中的成员 函数,而前者是类的友元函数。 友元运算符函数在类的内部声明格式如下: friend type operator@(参数表) 与成员运算符函数的声明格式相比较,只是在前面多了一个关键字 friend。 定义友元运算符函数与定义一般的友元函数相似,其格式如下: type operator@(参数表) //函数体 由于友元函数不是类的成员函数,所以不需要缀上类名。与成员运 算符函数不同,友元运算符函数是不属于任何类对象的,它没有 this指针。若重载的是双目运算符,则参数表中有两个操作数; 若重载的是单目运算符,则参数表中只有一个操作数
21 5.3.3友元运算符函数 把运算符函数定义成某个类的友元函数,称为友元运算函数符. 1.友元运算符函数定义的语法形式 友元运算符函数与成员运算符函数不同,后者本身是类中的成员 函数,而前者是类的友元函数。 友元运算符函数在类的内部声明格式如下: friend type operator @ (参数表); 与成员运算符函数的声明格式相比较,只是在前面多了一个关键字 friend。 定义友元运算符函数与定义一般的友元函数相似,其格式如下: type operator @ (参数表) { //函数体 } 由于友元函数不是类的成员函数,所以不需要缀上类名。与成员运 算符函数不同,友元运算符函数是不属于任何类对象的,它没有 this指针。若重载的是双目运算符,则参数表中有两个操作数; 若重载的是单目运算符,则参数表中只有一个操作数
2.双目运算符重载 当用友元函数重载双目运算符时,两个操作数都要传给运算符图纵。 例5.5友元函数重载双目运算符 在例5.3中,用成员运算符函数进行复数运算,现在我们采用友元 运算符函数。 Include<iostream. h> class complex private: double reala double imag; public complex(double r=0.0, double l=0.0)[real=r; imag=i;] void print(; friend complex operator +(complex a, complex b) ∥用友元运算符函数重载复数“+ friend complex operator-(complex a, complex b) )友元运算符函数重载复数 65 friend complex operator *( complex a, complex b) ∥)友元运算符函数重载复数“” 22 friend complex operator /complex a, complex b)
22 2.双目运算符重载 当用友元函数重载双目运算符时,两个操作数都要传给运算符函数。 例5.5 友元函数重载双目运算符 在例5.3中,用成员运算符函数进行复数运算,现在我们采用友元 运算符函数。 #include<iostream.h> class complex{ private: double real; double imag; public: complex(double r=0.0,double I=0.0) {real=r; imag=i;} void print(); friend complex operator + (complex a,complex b); //用友元运算符函数重载复数 “+” friend complex operator – (complex a,complex b); //用友元运算符函数重载复数 “-” friend complex operator * (complex a,complex b); //用友元运算符函数重载复数 “*” friend complex operator / (complex a,complex b); //用友元运算符函数重载复数 “/
接1例55 complex operator+( complex a, complex b)∥重载“+”定义 i complex temp; temp. real=a real+b real; temp. imag=a imag+b imag; return temp complex operator- complex a, complex b)∥重载“”定义 i complex temp temp. real=a. real-b real; temp. imag=a. imag-b imag return temp complex operator(complex a, complex b)∥重载“*”定义 i complex temp; temp. real=a real*b. real-a imag*b imag temp. imag=a real*b imag+a imag b real; return temp 23
23 接1 例5.5 complex operator +(complex a,complex b) //重载“+”定义 { complex temp; temp.real=a.real+b.real; temp.imag=a.imag+b.imag; return temp; } complex operator-(complex a,complex b) //重载“-”定义 { complex temp; temp.real=a.real-b.real; temp.imag=a.imag-b.imag; return temp; } complex operator (complex a,complex b) //重载“*”定义 { complex temp; temp.real=a.real*b.real-a.imag*b.imag; temp.imag=a.real*b.imag+a.imag*b.real; return temp; }
接2例5.5 complex operator/complex a, complex b) ∥重载“P定义 I complex temp; double t. t=1/b real*b real+b imag *b imag); temp. real=(a real"b real+a imag * b imag)*t; temp. imag=(b real*a. imag-a real*b imag*t return temp; void complex: print(∥输出显示复数 cout<<real if(imag>0)cout<<“+”; if( imag!=0) cout<<imag≤“in 24
24 接2 例5.5 complex operator/(complex a,complex b) //重载“/”定义 { complex temp; double t; t=1/(b.real*b.real+b.imag*b.imag); temp.real=(a.real*b.real+a.imag*b.imag)*t; temp.imag=(b.real*a.imag-a.real*b.imag)*t; return temp; } void complex::print() //输出显示复数 { cout<<real; if(imag>0) cout<<“+”; if(imag!=0) cout<<imag<<“i\n” }
void maino compleXA1(2,3,46),A2(3,6,28), A3A42A5,A6;∥定义6个复数对象 A3=A1+A2;∥复数相加 A4=A1A2;∥复数相减 A5=A1a2;W复数相乘 A6=A1/A2;∥复数相除 A1 printo;∥输出复数A1 A2 printo;∥输出复数A2 A3 print(;/输出复数相加结果A3 A4 print(;输出复数相减结果A4 A5 printo输出复数相乘结果A5 A6. print(;输出复数相除结果A6 程序运行结果如下: 2.3+4.6i 5.9+74i 3.6+2.8i 13+18i -4.6+23i 1.017308+0486538i
25 void main() { complexA1(2,3,4.6),A2(3,6,2.8), A3,A4,A5,A6;//定义6个复数对象 A3=A1+A2; //复数相加 A4=A1-A2; //复数相减 A5=A1*a2; //复数相乘 A6=A1/A2; //复数相除 A1.print(); //输出复数A1 A2.print(); //输出复数A2 A3.print();//输出复数相加结果A3 A4.print();//输出复数相减结果A4 A5.print();//输出复数相乘结果A5 A6.print();//输出复数相除结果A6 } 程序运行结果如下: 2.3+4.6i 5.9+7.4i 3.6+2.8i -1.3+1.8i -4.6+23i 1.017308+0.486538i