例4-5:多次重载友元函数实现交换律 1int operator+(intc1,complex &c2) ① #include <iostream> 川友员函数定义 ② class complex ② {int c;c=c1+c2.real; ③ {public: ③ return c; ④ double real,image; ④ int operator+(complex &c2,int c1) ⑤ complex(double r=0,double i 川友员函数定义 =0) ⑤ {intc;c=c1+c2.real; ⑥ real =r;image=i;} ⑥ return c; ⑦ friend int operator+(int c1, ⑦ int main() complex&c2);I友员函数声明 ⑧ complex ob2(1,5.0);ints; ⑧ friend int operator+(complex ⑨ s=3+ob2; /运算符重载 &c2,int c1); ⑩ cout<<"s="<<s<<endlj ⑨}; ① S=ob2+3; ∥运算符重载 ② cout<<"s="<<s<<endl; ③ return 0; ④} 15
15 ① #include <iostream> ② class complex ③ {public: ④ double real, image; ⑤ complex (double r = 0, double i = 0) ⑥ { real = r; image = i;} ⑦ friend int operator+ (int c1, complex &c2); //友员函数声明 ⑧ friend int operator+ (complex &c2,int c1 ) ; ⑨ }; ① int operator+ (int c1, complex &c2) //友员函数定义 ② {int c; c=c1+c2.real; ③ return c; } ④ int operator+ (complex &c2,int c1 ) //友员函数定义 ⑤ {int c; c=c1+c2.real; ⑥ return c; } ⑦ int main( ) ⑧ { complex ob2(1, 5.0); int s; ⑨ s = 3+ ob2; //运算符重载 ⑩ cout<<"s="<<s<<endl; ⑪ s = ob2+3; //运算符重载 ⑫ cout<<"s="<<s<<endl; ⑬ return 0; ⑭ } 例4-5:多次重载友元函数实现交换律
课堂练习一 使用友元方式重载运算符: 自定义如下形式的一个称为point的类,其对象 表示平面上的一个点(X,y),并通过友元方式对 该类重载二目运算符+,用来求出两个对象的和。 +运算符的运算结果如下所示: (1,5)+(2,6)=(3,11) 16
课堂练习一 使用友元方式重载运算符: 自定义如下形式的一个称为point的类,其对象 表示平面上的一个点(x,y),并通过友元方式对 该类重载二目运算符+,用来求出两个对象的和。 +运算符的运算结果如下所示: (1,5)+(2,6)=(3,11) 16
参考答案 #include <iostream.h> void main() class point∥类定义 {point p1(1.5,3),p2(1,4.5); doublex,y; public: pointp3;p3=p1+p2; p3.show(); point(double r=0,double i=0) {x=r;y=i;} void show() {cout<"("<<x<<","<y<""<end;} friend point operator+(point p1,point p2); ∥类的友元函数,2个参数c1,c2 }; point operator+(point p1,point p2) point p; p.x=p1.x+p2.x;p.y=p1.y+p2.y; return p; 17
参考答案 • #include <iostream.h> • class point //类定义 • { double x,y; • public: • point(double r=0,double i=0) • {x=r; y=i;} • void show() • { cout<<"("<<x<<","<<y<<")"<<endl; } • friend point operator+(point p1,point p2); • // 类的友元函数,2个参数c1,c2 • }; • point operator+(point p1,point p2) • { point p; • p.x=p1.x+p2.x; p.y=p1.y+p2.y; • return p; • } 17 void main() { point p1(1.5,3),p2(1,-4.5); point p3; p3=p1+p2; p3.show(); }
课堂练习二 使用友元方式重载运算符: 自定义如下形式的一个称为point的类,其对象表示平 面上的一个点(x,y),并通过成员函数形式重载二目运 算符+,友元函数方式重载二目运算符-,==,*;分别 用来求两个点对象的和,差,判断两个点对象是否相等, 以及求出两个点对象之间的距离。各运算符的运算结果如 下所示: (1.2,-3.5)只与它本身相等; (1.2,-3.5)+(-1,2.8)=(0.2,-0.7); (1.2,-3.5)-(-1,2.8)=(2.2,-6.3): (-1,2.8)*(6,6)=7.69675 18
18 使用友元方式重载运算符: 自定义如下形式的一个称为point的类,其对象表示平 面上的一个点(x,y),并通过成员函数形式重载二目运 算符+,友元函数方式重载二目运算符-, = = , *;分别 用来求两个点对象的和,差,判断两个点对象是否相等, 以及求出两个点对象之间的距离。各运算符的运算结果如 下所示: (1.2,-3.5)只与它本身相等; (1.2,-3.5)+(-1,2.8)=(0.2,-0.7); (1.2,-3.5)-(-1,2.8)=(2.2,-6.3); (-1,2.8)*(6,6)=7.69675 课堂练习二
#include <iostream.h> bool operator==(point pl,point p2) #include<math.h> class point/类定义 return((pl.x==p2.x)&&(p1.y-=p2.y)); double x,y; double operator*(point pl,point p2) public: double d,dl,d2; point(double r=0,double i=0) {x=r;y=i;} d1=p1.x-p2.x; void show({ d2=p1.y-p2.y; cout<<""<<x<<","<<y<<")"<endl;} d上sqrt(d*dl+d2*d2); point operator+(point ) return d;} friend bool operator==(point pl,point p2); void main() friend point operator-(point pl,point p2); point pl(1,1),p2(1,1),p3,p4;bool T; friend double operator*(point pl,point p2); p3=p1+p2;p4=pl-p2; }; T=pl-=p2; cout<<"pl=";pl.show(; pointpoint::operator+(point q) cout<<"p2=";p2.show0; point p;p.x=x+q.x;p.y=y+q-y;return p;) cout<<"p1+p2=";p3.show(); point operator-(point pl,point p2) cout<<"p1-p2=";p4.show0; point p;p.x=pl.x-p2.x;p.y=pl.y-p2.y; cout<<"pl==p2 ?:"<<T<<endl; return p; cout<<"pl与p2的距离是:"<pl*p2<endl; 19
• #include <iostream.h> • #include<math.h> • class point //类定义 • { double x,y; • public: • point(double r=0,double i=0) {x=r; y=i;} • void show() { cout<<"("<<x<<","<<y<<")"<<endl; } • point operator+(point ); • friend bool operator==(point p1,point p2); • friend point operator-(point p1,point p2); • friend double operator*(point p1,point p2); • }; • point point::operator+(point q) • { point p; p.x=x+q.x; p.y=y+q.y; return p;} • point operator-(point p1,point p2) • { point p; p.x=p1.x-p2.x; p.y=p1.y-p2.y; • return p; • } 19 bool operator==(point p1,point p2) { return ((p1.x==p2.x)&&(p1.y==p2.y)); } double operator*(point p1,point p2) { double d,d1,d2; d1=p1.x-p2.x; d2=p1.y-p2.y; d=sqrt(d1*d1+d2*d2); return d; } void main() { point p1(1,1),p2(1,1),p3,p4; bool T; p3=p1+p2; p4=p1-p2; T=p1==p2; cout<<"p1=";p1.show(); cout<<"p2=";p2.show(); cout<<"p1+p2="; p3.show(); cout<<"p1-p2="; p4.show(); cout<<"p1==p2 ?:"<<T<<endl; cout<<"p1与p2的距离是:"<<p1*p2<<endl; }