几个典型运算符重载C++四.ppt
《几个典型运算符重载C++四.ppt》由会员分享,可在线阅读,更多相关《几个典型运算符重载C++四.ppt(55页珍藏版)》请在三一办公上搜索。
1、2023/10/27,1,7.3 几个典型运算符重载,2023/10/27,2,7.3 几个典型运算符重载,数学类中常用的几个运算重载的特点和应用,6.3 几个典型运算符重载,2023/10/27,3,7.3.1 重载+与-,设A Aobject;运算符+和-有两种方式:前置方式:+Aobject-Aobject,后置方式:Aobject+Aobject-,成员函数 重载A:A operator+();解释为:Aobject.operator+();友员函数 重载friend A operator+(A,成员函数 重载A:A operator+(int);解释为:Aobject.operato
2、r+(0);友员函数 重载:friend A operator+(A 解释为:operator+(Aobject,0),伪参数,6.3.1 重载+与-,2023/10/27,4,成员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;Increase operator+();/前置 Increase operator+(int);/后置 private:unsigned value;Increase Increase:operator+()value+;return*this;
3、Increase Increase:operator+(int)Increase temp;temp.value=value+;return temp;void main()Increase a,b,n;for(int i=0;i 10;i+)a=n+;cout n=;n.display();cout a=;a.display();for(i=0;i 10;i+)b=+n;cout n=;n.display();cout b=;b.display();,6.3.1 重载+与-,2023/10/27,5,成员函数重载+,#includeclass Increase public:Increase
4、()value=0;void display()const coutvaluen;Increase operator+();/前置 Increase operator+(int);/后置 private:unsigned value;Increase Increase:operator+()value+;return*this;Increase Increase:operator+(int)Increase temp;temp.value=value+;return temp;void main()Increase a,b,n;for(int i=0;i 10;i+)a=n+;cout n=;
5、n.display();cout a=;a.display();for(i=0;i 10;i+)b=+n;cout n=;n.display();cout b=;b.display();,Increase operator+();,Increase Increase:operator+()value+;return*this;,+n;,预定义版本,6.3.1 重载+与-,2023/10/27,6,成员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;Increase oper
6、ator+();/前置 Increase operator+(int);/后置 private:unsigned value;Increase Increase:operator+()value+;return*this;Increase Increase:operator+(int)Increase temp;temp.value=value+;return temp;void main()Increase a,b,n;for(int i=0;i 10;i+)a=n+;cout n=;n.display();cout a=;a.display();for(i=0;i 10;i+)b=+n;c
7、out n=;n.display();cout b=;b.display();,Increase operator+();,Increase Increase:operator+()value+;return*this;,+n;,重载版本,6.3.1 重载+与-,2023/10/27,7,成员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;Increase operator+();/前置 Increase operator+(int);/后置 private:unsigne
8、d value;Increase Increase:operator+()value+;return*this;Increase Increase:operator+(int)Increase temp;temp.value=value+;return temp;void main()Increase a,b,n;for(int i=0;i 10;i+)a=n+;cout n=;n.display();cout a=;a.display();for(i=0;i 10;i+)b=+n;cout n=;n.display();cout b=;b.display();,Increase operat
9、or+(int);,Increase Increase:operator+(int)Increase temp;temp.value=value+;return temp;,n+;,预定义版本,6.3.1 重载+与-,2023/10/27,8,成员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;Increase operator+();/前置 Increase operator+(int);/后置 private:unsigned value;Increase Increa
10、se:operator+()value+;return*this;Increase Increase:operator+(int)Increase temp;temp.value=value+;return temp;void main()Increase a,b,n;for(int i=0;i 10;i+)a=n+;cout n=;n.display();cout a=;a.display();for(i=0;i 10;i+)b=+n;cout n=;n.display();cout b=;b.display();,Increase operator+(int);,Increase Incr
11、ease:operator+(int)Increase temp;temp.value=value+;return temp;,n+;,重载版本,6.3.1 重载+与-,2023/10/27,9,成员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;Increase operator+();/前置 Increase operator+(int);/后置 private:unsigned value;Increase Increase:operator+()value+;ret
12、urn*this;Increase Increase:operator+(int)Increase temp;temp.value=value+;return temp;void main()Increase a,b,n;for(int i=0;i 10;i+)a=n+;cout n=;n.display();cout a=;a.display();for(i=0;i 10;i+)b=+n;cout n=;n.display();cout b=;b.display();,6.3.1 重载+与-,2023/10/27,10,友员函数重载+,#includeclass Increase publi
13、c:Increase()value=0;void display()const coutvaluen;friend Increase operator+(Increase,6.3.1 重载+与-,2023/10/27,11,友员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;friend Increase operator+(Increase,friend Increase operator+(Increase,Increase operator+(Increase,+n;
14、,通过引用参数操作对象,6.3.1 重载+与-,2023/10/27,12,友员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;friend Increase operator+(Increase,friend Increase operator+(Increase,Increase operator+(Increase,n+;,复制构造局部对象,6.3.1 重载+与-,2023/10/27,13,友员函数重载+,#includeclass Increase public:
15、Increase()value=0;void display()const coutvaluen;friend Increase operator+(Increase,friend Increase operator+(Increase,Increase operator+(Increase,n+;,伪参数,6.3.1 重载+与-,2023/10/27,14,友员函数重载+,#includeclass Increase public:Increase()value=0;void display()const coutvaluen;friend Increase operator+(Increa
16、se,6.3.1 重载+与-,2023/10/27,15,7.3.2 重载赋值运算符,赋值运算符重载用于对象数据的复制 operator=必须重载为成员函数 重载函数原型为:类型,6.3.2 重载赋值运算符,2023/10/27,16,#include#includeclass Name public:Name(char*pN);Name(const Name/调用重载赋值算符函数,定义Name类的重载赋值函数,6.3.2 重载赋值运算符,2023/10/27,17,#include#includeclass Name public:Name(char*pN);Name(const Name/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 几个 典型 运算 重载 C+
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-6407102.html