几个典型运算符重载(C)(四).ppt
《几个典型运算符重载(C)(四).ppt》由会员分享,可在线阅读,更多相关《几个典型运算符重载(C)(四).ppt(55页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、2020/10/19,1,7.3 几个典型运算符重载,2020/10/19,2,7.3 几个典型运算符重载,数学类中常用的几个运算重载的特点和应用,6.3 几个典型运算符重载,2020/10/19,3,7.3.1 重载 + 与 -,设A Aobject ; 运算符 + + 和 - - 有两种方式: 前置方式: +Aobject -Aobject,后置方式: Aobject + Aobject -,成员函数 重载A : A operator+ () ; 解释为:Aobject . operator + + ( ) ; 友员函数 重载friend A operator+ (A ,成员函数 重载A
2、: A operator+ (int) ; 解释为: Aobject . operator + + ( 0 ) ; 友员函数 重载:friend A operator+ (A 解释为: operator+(Aobject, 0),伪参数,6.3.1 重载+与-,2020/10/19,4,成员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int
3、) ; / 后置 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.d
4、isplay( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; ,6.3.1 重载+与-,2020/10/19,5,成员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: u
5、nsigned 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
6、 = 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 重载+与-,2020/10/19,6,成员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutvalue
7、n; ; 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 ;
8、 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 + ( ) ;,Increase Increase : operator + ( ) value + ; return * this ; ,+ n ;,重载版本,6.3.1 重载+与-,2020/10/19,7,成员函数重载+,#inclu
9、de class Increase public : Increase ( ) 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.va
10、lue = 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 Increase : operator
11、 + ( int ) Increase temp; temp.value = value + ; return temp; ,n + ;,预定义版本,6.3.1 重载+与-,2020/10/19,8,成员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: unsigned value ; ; Increas
12、e 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 = +
13、 n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; ,Increase operator + ( int ) ;,Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; ,n + ;,重载版本,6.3.1 重载+与-,2020/10/19,9,成员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const co
14、utvaluen; ; 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
15、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 重载+与-,2020/10/19,10,友员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; friend
16、 Increase operator + ( Increase ,6.3.1 重载+与-,2020/10/19,11,友员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase ,friend Increase operator + ( Increase ,Increase operator + ( Increase ,+ n ;,通过引用参数 操作对象,6.3.1 重载+与-,20
17、20/10/19,12,友员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase ,friend Increase operator + ( Increase ,Increase operator + ( Increase ,n + ;,复制构造 局部对象,6.3.1 重载+与-,2020/10/19,13,友员函数重载+,#include class Increase publi
18、c : Increase ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase ,friend Increase operator + ( Increase ,Increase operator + ( Increase ,n + ;,伪参数,6.3.1 重载+与-,2020/10/19,14,友员函数重载+,#include class Increase public : Increase ( ) value=0; void display( ) const coutval
19、uen; ; friend Increase operator + ( Increase ,6.3.1 重载+与-,2020/10/19,15,7.3.2 重载赋值运算符,赋值运算符重载用于对象数据的复制 operator= 必须重载为成员函数 重载函数原型为: 类型 ,6.3.2 重载赋值运算符,2020/10/19,16,#include #include class Name public : Name ( char *pN ) ; Name( const Name / 调用重载赋值算符函数 ,定义Name类的重载赋值函数,6.3.2 重载赋值运算符,2020/10/19,17,#inc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 几个 典型 运算 重载
限制150内