版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、第 四 章 運算符重載,4.1 重載運算符 4.2 自由存儲 4.3 類型轉換,4.1.1 概述 4.1.2 一元和二元運算符 4.1.3 用成員函數(shù)重載運算符 4.1.4 用友員函數(shù)重載運算符,4.1.5 重載 + + 和 - - 4.1.6 重載賦值運算符 4.1.7 重載 ( ) 和 ,4.1 重載運算符,4.1.1 概述,1. 運算符重載的需要,運算符重載使得用戶自定義的數(shù)據(jù)以一種更簡潔的方式工作,例如: int x , y y = x + y ;,matrix m1 , m2 ;/ 矩陣類對象 m2 = Madd ( m1 , m2 ) ; / 調(diào)用函數(shù)計算兩個矩陣的和,comple
2、x c1 , c2 ; / 復數(shù)類對象 c1 = Cadd (c1 , c2 ) ;/ 調(diào)用函數(shù)計算兩個復數(shù)的和,能表示為 c1 = c1 + c2 ; ?,能表示為 m1 = m1 + m2 ; ?,4.1.1 概述,2. C+ 中可以被重載的運算符,+-*/% public: counter ( ) value = 0; ; void operator + + ( ) ; void operator - - ( ) ; unsigned int operator ( ) ( ) ; ; void counter : operator + + ( ) if ( value 0 ) value
3、 - - ; unsigned int counter : operator ( ) ( ) return value ; ,main ( ) counter my_counter ; for ( int i = 0 ; i 12 ; i + + ) my_counter + + ; cout “n my_counter=” my_counter ( ) ; my_counter - - ; my_counter - - ; cout “n my_counter=” my_counter ( ) ; ,/ ex1 三個算符的重載(P90-92) #include class counter u
4、nsigned int value ; public: counter ( ) value = 0; ; void operator + + ( ) ; void operator - - ( ) ; unsigned int operator ( ) ( ) ; ; void counter : operator + + ( ) if ( value 0 ) value - - ; unsigned int counter : operator ( ) ( ) return value ; ,main ( ) counter my_counter ; for ( int i = 0 ; i
5、12 ; i + + ) my_counter + + ; cout “n my_counter=” my_counter ( ) ; my_counter - - ; my_counter - - ; cout “n my_counter=” my_counter ( ) ; ,使用 系統(tǒng)預定義版本,/ ex1 三個算符的重載(P90-92) #include class counter unsigned int value ; public: counter ( ) value = 0; ; void operator + + ( ) ; void operator - - ( ) ; u
6、nsigned int operator ( ) ( ) ; ; void counter : operator + + ( ) if ( value 0 ) value - - ; unsigned int counter : operator ( ) ( ) return value ; ,main ( ) counter my_counter ; for ( int i = 0 ; i 12 ; i + + ) my_counter + + ; cout “n my_counter=” my_counter ( ) ; my_counter - - ; my_counter - - ;
7、cout “n my_counter=” my_counter ( ) ; ,使用 系統(tǒng)預定義版本,/ ex1 三個算符的重載(P90-92) #include class counter unsigned int value ; public: counter ( ) value = 0; ; void operator + + ( ) ; void operator - - ( ) ; unsigned int operator ( ) ( ) ; ; void counter : operator + + ( ) if ( value 0 ) value - - ; unsigned i
8、nt counter : operator ( ) ( ) return value ; ,main ( ) counter my_counter ; for ( int i = 0 ; i 12 ; i + + ) my_counter + + ; cout “n my_counter=” my_counter ( ) ; my_counter - - ; my_counter - - ; cout “n my_counter=” my_counter ( ) ; ,使用 系統(tǒng)預定義版本, “=”運算符重載不能被派生類繼承 由于重載可以對定義算符新的含義,要注意程序的可讀性 例如:用運算符“
9、 + ”表示減運算;用“ * ”表示除運算 違反程序閱讀習慣,注意:,4.1.2 一元和二元運算符,運算符重載時,運算符函數(shù)只能定義為兩種方式: 類的成員函數(shù) 友員函數(shù) 這兩種方式非常相似,關鍵區(qū)別在于 成員函數(shù)具有 this 指針 友員函數(shù)沒有 this 指針,1. 一元運算符,一元運算符,不論是前綴還是后綴,都需要一個操作數(shù): aa 及 aa,1. 一元運算符,一元運算符,不論是前綴還是后綴,都需要一個操作數(shù): aa 及 aa,1. 一元運算符,一元運算符,不論是前綴還是后綴,都需要一個操作數(shù): aa 及 aa,可以解釋為: aa . operator ( ) 或 operator ( a
10、a ),1. 一元運算符,一元運算符,不論是前綴還是后綴,都需要一個操作數(shù): aa 及 aa,可以解釋為: aa . operator ( ) 或 operator ( aa ),一元運算符函數(shù) operator 所需的一個操作數(shù)由對象 aa 通過this 指針隱含地傳遞,因此參數(shù)表為空。這時,運算符函 數(shù)用類的成員函數(shù)表示,1. 一元運算符,一元運算符,不論是前綴還是后綴,都需要一個操作數(shù): aa 及 aa,可以解釋為: aa . operator ( ) 或 operator ( aa ),一元運算符函數(shù) operator 所需的一個操作數(shù)在參數(shù)表中 由對象顯式地提供,適用于沒有this指
11、針的情況。這時,運算符 函數(shù)用類的友員函數(shù)表示,一元運算符函數(shù) operator 所需的一個操作數(shù)由對象 aa 通過this 指針隱含地傳遞,因此參數(shù)表為空。這時,運算符函 數(shù)用類的成員函數(shù)表示,1. 一元運算符,一元運算符,不論是前綴還是后綴,都需要一個操作數(shù): aa 及 aa,不管用成員函數(shù)或用友員函數(shù)重載運算符, 對運算符的使用方法都是相同的,2. 二元運算符,同樣,對任意二元運算符: aa bb,可以解釋為: aa . operator ( bb ) 或 operator ( aa , bb ),2. 二元運算符,同樣,對任意二元運算符: aa bb,可以解釋為: aa . opera
12、tor ( bb ) 或 operator ( aa , bb ),二元運算符函數(shù) operator 所需的一個操作數(shù)由對象 aa 通過 this 指針隱含地傳遞。因此它只有一個參數(shù)。這時,運算符 函數(shù)用類的成員函數(shù)表示,2. 二元運算符,同樣,對任意二元運算符: aa bb,可以解釋為: aa . operator ( bb ) 或 operator ( aa , bb ),二元運算符函數(shù) operator 所需的一個操作數(shù)由對象 aa 通過 this 指針隱含地傳遞。因此它只有一個參數(shù)。這時,運算符 函數(shù)用類的成員函數(shù)表示,二元運算符函數(shù) operator 所需的兩個操作數(shù)在參數(shù)表中 由對
13、象 aa 和 bb顯式地提供,適用于沒有this指針的情況。這時, 運算符函數(shù)用類的友員函數(shù)表示,2. 二元運算符,同樣,對任意二元運算符: aa bb,不管用成員函數(shù)或用友員函數(shù)重載運算符, 對運算符的使用方法都是相同的,4.1.3 用成員函數(shù)重載運算符,例:創(chuàng)建一個名為 three_d 的簡單類,three 類含有三維空間 中的一對象坐標。該程序相對于 three_d 類對“ + ”和“ = ” 運算符進行重載。,/ex2(例4-1P94-96) # include class three_d int x , y , z ;/ 3_d coordinates public : three_
14、d operator + ( three_d t ) ;/ 重載“+” three_d operator = ( three_d t ) ; / 重載“=” three_d operator + ( ) ; / 重載“+” void show ( ) ; void assign ( int mx , int my , int mz ) ; ;,three_d three_d : operator + ( three_d t )/ 重載“+” three_d temp ; temp . x = x + t . x ; temp . y = y + t . y ; temp . z = z + t
15、 . z ; return temp ; three_d three_d : operator = ( three_d t )/ 重載“=” x = t . x ; y = t . y ; z = t . z ; return * this ; three_d three_d : operator + + ( )/ 重載一元運算符“+ +” x + + ; y + + ; z + + ; return * this ; void three_d : show ( )/ 顯示 x, y, z 坐標 cout x “ , ” y “ , ” z “n” ; void three_d : assig
16、n ( int mx , int my , int mz )/ 指定坐標 x = mx ; y = my ; z = mz ; ,three_d three_d : operator + ( three_d t )/ 重載“+” three_d temp ; temp . x = x + t . x ; temp . y = y + t . y ; temp . z = z + t . z ; return temp ; three_d three_d : operator = ( three_d t )/ 重載“=” x = t . x ; y = t . y ; z = t . z ; r
17、eturn * this ; three_d three_d : operator + + ( )/ 重載一元運算符“+ +” x + + ; y + + ; z + + ; return * this ; void three_d : show ( )/ 顯示 x, y, z 坐標 cout x “ , ” y “ , ” z “n” ; void three_d : assign ( int mx , int my , int mz )/ 指定坐標 x = mx ; y = my ; z = mz ; ,重載二元算符參數(shù)表只明確一個變元 另一個變元用 this 指針隱含傳遞,three_d
18、 three_d : operator + ( three_d t )/ 重載“+” three_d temp ; temp . x = x + t . x ; temp . y = y + t . y ; temp . z = z + t . z ; return temp ; three_d three_d : operator = ( three_d t )/ 重載“=” x = t . x ; y = t . y ; z = t . z ; return * this ; three_d three_d : operator + + ( )/ 重載一元運算符“+ +” x + + ;
19、y + + ; z + + ; return * this ; void three_d : show ( )/ 顯示 x, y, z 坐標 cout x “ , ” y “ , ” z “n” ; void three_d : assign ( int mx , int my , int mz )/ 指定坐標 x = mx ; y = my ; z = mz ; ,隱含 this - x,three_d three_d : operator + ( three_d t )/ 重載“+” three_d temp ; temp . x = x + t . x ; temp . y = y +
20、t . y ; temp . z = z + t . z ; return temp ; three_d three_d : operator = ( three_d t )/ 重載“=” x = t . x ; y = t . y ; z = t . z ; return * this ; three_d three_d : operator + + ( )/ 重載一元運算符“+ +” x + + ; y + + ; z + + ; return * this ; void three_d : show ( )/ 顯示 x, y, z 坐標 cout x “ , ” y “ , ” z “n
21、” ; void three_d : assign ( int mx , int my , int mz )/ 指定坐標 x = mx ; y = my ; z = mz ; ,返回類類型,便于作復雜運算,例如 a + b + c 事實上該函數(shù)可以返回任意正確 C+ 類型,three_d three_d : operator + ( three_d t )/ 重載“+” three_d temp ; temp . x = x + t . x ; temp . y = y + t . y ; temp . z = z + t . z ; return temp ; three_d three_d
22、 : operator = ( three_d t )/ 重載“=” x = t . x ; y = t . y ; z = t . z ; return * this ; three_d three_d : operator + + ( )/ 重載一元運算符“+ +” x + + ; y + + ; z + + ; return * this ; void three_d : show ( )/ 顯示 x, y, z 坐標 cout x “ , ” y “ , ” z “n” ; void three_d : assign ( int mx , int my , int mz )/ 指定坐標
23、 x = mx ; y = my ; z = mz ; ,賦值運算符修改第一個操作數(shù)(this 所指對象) 返回對象類型,使得連續(xù)賦值合法 a = b = c = d ;,three_d three_d : operator + ( three_d t )/ 重載“+” three_d temp ; temp . x = x + t . x ; temp . y = y + t . y ; temp . z = z + t . z ; return temp ; three_d three_d : operator = ( three_d t )/ 重載“=” x = t . x ; y =
24、t . y ; z = t . z ; return * this ; three_d three_d : operator + + ( )/ 重載一元運算符“+ +” x + + ; y + + ; z + + ; return * this ; void three_d : show ( )/ 顯示 x, y, z 坐標 cout x “ , ” y “ , ” z “n” ; void three_d : assign ( int mx , int my , int mz )/ 指定坐標 x = mx ; y = my ; z = mz ; ,“+”運算符沒有對象明確傳送給函數(shù) 通過 t
25、his 指針隱含傳送,main ( ) three_d a , b , c ; a . assign ( 1, 2 , 3 ) ; b . assign ( 10 , 10 , 10 ) ; a . show ( ) ; b . show ( ) ; c = a + b ;/ 將 a 和 b 加在一起 c . show ( ) ; c = a + b + c ; / 將 a 、b、c 加在一起 c . show ( ) ; c = b = a ;/ 多重賦值 c . show ( ) ; b . show ( ) ; c + + ;/ 對 c 遞加 c . show ( ) ; ,表達式引起對
26、算符函數(shù)調(diào)用的對象是 a, 算符右邊的對象b作為參數(shù)傳遞給函數(shù),可以解釋為 a . operator + ( b) ;,4.1.4 用友員重載運算符,有時候,用成員函數(shù)重載運算符會碰到麻煩。例如: class Complex int Real ;int Imag ; public : Complex ( int a ) Real = a ; Imag = 0 ; Complex ( int a , int b ) Real = a ; Imag = b ; Complex operator + ( Complex ) ; . ; int f ( ) Complex z ( 2 , 3 ) ,
27、k ( 3 , 4 ) ; z = z + 27 ; z = 27 + z ; . ,4.1.4 用友員重載運算符,有時候,用成員函數(shù)重載運算符會碰到麻煩。例如: class Complex int Real ;int Imag ; public : Complex ( int a ) Real = a ; Imag = 0 ; Complex ( int a , int b ) Real = a ; Imag = b ; Complex operator + ( Complex ) ; . ; int f ( ) Complex z ( 2 , 3 ) , k ( 3 , 4 ) ; z =
28、 z + 27 ;/ ok z = 27 + z ; . ,表達式 z + 27 可被解釋為 z . operator + ( 27 ) z 是復數(shù)對象,使用“ + ”的 重載版本; 由于重載算符函數(shù)要求的 右操作數(shù)也為復數(shù),系統(tǒng)通過 構造函數(shù) Complex ( int a ) 將 整數(shù) 27 轉換為 Complex 類常 量Complex ( 27 ),4.1.4 用友員重載運算符,有時候,用成員函數(shù)重載運算符會碰到麻煩。例如: class Complex int Real ;int Imag ; public : Complex ( int a ) Real = a ; Imag = 0
29、 ; Complex ( int a , int b ) Real = a ; Imag = b ; Complex operator + ( Complex ) ; . ; int f ( ) Complex z ( 2 , 3 ) , k ( 3 , 4 ) ; z = z + 27 ;/ ok z = 27 + z ;/ error . ,表達式 27 + z 可被解釋為 27 . operator + ( z ) 該式子毫無意義。 27不是 Complex 類對象, 不能調(diào)用算符重載函數(shù)與對象 z 相加 ! 此時,成員函數(shù)重載的算 符“ + ”不支持交換律。,4.1.4 用友員函數(shù)重載
30、運算符,在第一個參數(shù)需要隱式轉換的情形下,使用友員函數(shù)重載算符是正確的選擇。 由于友員函數(shù)沒有隱含 this 指針,用友員函數(shù)重載算符時,所需操作數(shù)都必須在參數(shù)表顯式聲明,所以很容易實現(xiàn)類型的隱式轉換。,4.1.4 用友員重載運算符,把重載運算符函數(shù)改為友員函數(shù): class Complex int Real ;int Imag ; public : Complex ( int a ) Real = a ; Imag = 0 ; Complex ( int a , int b ) Real = a ; Imag = b ; friend Complex operator + ( Complex
31、 , Complex ) ; . ; int f ( ) Complex z ( 2 , 3 ) , k ( 3 , 4 ) ; z = z + 27 ;/ ok z = 27 + z ;/ ok . ,/ ex3 使用友員函數(shù)重載運算符 #include class complex public : complex ( double r =0 , double i = 0 ) ; void print ( ) const ; friend complex operator + ( const complex ,complex : complex ( double r , double i )
32、 real = r ; imag = i ; complex operator + ( const complex ,main ( ) complex c1 ( 2.5 , 3.7 ) , c2 ( 4.2 , 6.5 ) ; complex c ; c = c1 - c2 ;/ c = operator - ( c1, c2 ) c . print ( ) ; c = c1 + c2 ; / c = operator + ( c1, c2 ) c . print ( ) ; c = - c1 ;/ c = operator - (c1) c . print ( ) ; ,注意 這里使用了對象
33、賦值 但程序沒有定義重載算符“=”函數(shù),用友員函數(shù)重載像“+”這樣的運算符時,有時會碰到問題。 例如,在 ex2 中,類 three_d 用成員函數(shù)重載“+ +”的版本是: three_d three_d : operator + + ( ) x + + ; y + + ; z + + ; return * this ; / ok , 修改了this 指針所指對象,使用友員的問題,用成員函數(shù)重載一元算符時,所需要的唯一變元通過 this 指針傳遞,對 this 所指向的數(shù)據(jù)的任何改變都會影響到激活運算符函數(shù)的對象,1. 但若定義友員函數(shù) friend operator + + ( ) 版本:
34、three_d three_d : operator + + ( three_d opl ) opl . x + + ; opl . y + + ; opl . z + + ; return opl ; ,使用友員的問題,問題 C+ 的函數(shù)是傳值的,對 opl 的所有修改都無法傳到函數(shù)體外,不會影響被調(diào)用的對象,2. 用指向激活對象的指針定義友員函數(shù): three_d three_d : operator + + ( three_d * opl ) opl - x + + ; opl - y + + ; opl - z + + ; return *opl ; ,使用友員的問題,問題 C+ 不知
35、道如何激活該函數(shù),下述代碼無法編譯: three_d ob ( 1 , 2 , 3 ) ; / error,二義性 對 ob 的地址進行遞加? 還是 將對象 ob 遞加?,3. 使用引用參數(shù): three_d three_d : operator + + ( three_d ,使用友員的問題,下述代碼是正確的: three_d ob ( 1 , 2 , 3 ) ; ob + + ;/ ok,可見,如果一個運算符的操作要修改類的對象的狀態(tài),要重載為友員函數(shù)時,應該使用引用參數(shù),4.1.4 用友員函數(shù)重載運算符, 若一運算符的操作需要修改類對象狀態(tài)時,則它應該是成員函數(shù)。需要左值操作數(shù)的運算符(如
36、 =,*=,+)應該用成員函數(shù)重載。 定義成友員時要用引用參數(shù)。 如果運算符的操作數(shù)(尤其是第一個操作數(shù))希望有隱式轉換,則重載算符時必須用友員函數(shù) 不能用友員函數(shù)重載的運算符是 =(),4.1.5 重載 + + 和 - -,C語言中,運算符 + + 和 - - 有兩種方式: 前綴方式:+ + i ;- - i ;,后綴方式:i + + ;i - - ;,一元 成員函數(shù) 重載:aa . operator + + ( ) ; 友員函數(shù) 重載:operator + + ( X 友員函數(shù) 重載:operator + + ( X ,4.1.5 重載 + + 和 - -,例如:用成員函數(shù)重載 class
37、 X public : operator + + ( ) ;/ 前綴方式 operator + + ( int ) ;/ 后綴方式 void f ( X a ) + + a ;/ a . operator + + ( ) a + + ;/ a . operator + + ( 0 ) a . operator + + ( ) ;/ 顯式調(diào)用,意為 + + a ; a . operator + + ( 0 ) ; / 顯式調(diào)用,意為 a + + ; ,4.1.5 重載 + + 和 - -,例如:用友員函數(shù)重載 class Y public : friend operator + + ( Y ,4
38、.1.5 重載 + + 和 - -, 重載運算符 + +(或 - -)后,可以用兩種方式使用: a + + ;/ 隱式調(diào)用 a . operator + + ( 0 ) ; / 顯式調(diào)用,討論, 有時,它們的使用方式不等價: a + + 3 ;/ error 但 operator + + ( a , 3 ) ; / ok 或 a . operator + + ( 3 ) ;/ ok 合法,等價于a + = 3 ;,/ ex4 #include class Increase public : Increase ( ) value=0; void display( ) const coutvalu
39、en; ; 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
40、= 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( ) ; ,成員函數(shù)重載 + +,/ ex5 #include class Increase public : Increase ( ) value = 0 ; void display ( ) const coutvaluen; ; frien
41、d Increase operator + + ( Increase ,友員函數(shù)重載 + +,4.1.6 重載賦值運算符,1.重載格式 X /3_d coordinates public : three_d ,能夠正確使用賦值號語義 賦值表達式左邊的表達式(對象)是左值,/ ex7 class three_d int x , y , z ;/3_d coordinates public : three_d operator + ( ) ; void show ( ) ; void assign ( int mx , int my , int mz ) ; ; x = t . x ; y = t . y ; z = t . z ; return * this ; / 其它成員函數(shù)定義 main ( ) three_d a , b ; a . assign ( 1 , 2 , 3 ) ; b. assign ( 10 , 10 , 10 ) ; a . show ( ) ; b. sho
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030人工智能醫(yī)療診斷市場供需算法準確性分析及醫(yī)療AI投資
- 2025-2030交通運輸規(guī)劃行業(yè)市場分析及發(fā)展方向與投資管理策略研究報告
- 鑒定報告檔案管理制度
- 信息科檔案管理制度
- 店鋪檔案管理制度
- 百電梯技術檔案管理制度
- 內(nèi)部管理制度檔案管理
- 市直單位檔案管理制度
- 空調(diào)檔案管理制度
- 養(yǎng)老保險檔案管理制度
- 2025年湖南理工職業(yè)技術學院單招(計算機)測試模擬題庫必考題
- 2025黑龍江大慶市工人文化宮招聘工作人員7人考試歷年真題匯編帶答案解析
- 2026年內(nèi)蒙古化工職業(yè)學院單招職業(yè)適應性考試必刷測試卷附答案解析
- 財務數(shù)字化轉型與業(yè)財數(shù)據(jù)深度融合實施路徑方案
- 后勤保障醫(yī)院運維成本智能調(diào)控
- 循證護理在兒科護理中的實踐與應用
- 少兒無人機課程培訓
- 麻醉睡眠門診科普
- 電力絕緣膠帶施工方案
- 預防性試驗收費標準全解析(2025版)
- 三一旋挖打斜樁施工方案
評論
0/150
提交評論