幾個(gè)典型運(yùn)算符重載C四_第1頁(yè)
幾個(gè)典型運(yùn)算符重載C四_第2頁(yè)
幾個(gè)典型運(yùn)算符重載C四_第3頁(yè)
幾個(gè)典型運(yùn)算符重載C四_第4頁(yè)
幾個(gè)典型運(yùn)算符重載C四_第5頁(yè)
已閱讀5頁(yè),還剩50頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、2021-11-417.3 7.3 幾個(gè)典型運(yùn)算符重載幾個(gè)典型運(yùn)算符重載 2021-11-42 數(shù)學(xué)類中常用的幾個(gè)運(yùn)算重載的特點(diǎn)和應(yīng)用 6.3 幾個(gè)典型運(yùn)算符重載2021-11-437.3.1 7.3.1 重載重載 + 與與 - 設(shè)a aobject ;運(yùn)算符 + + 和 - - 有兩種方式:前置方式:前置方式: +aobject -aobject后置方式:后置方式: aobject + aobject - 成員函數(shù)成員函數(shù) 重載a : a operator+ () ; 解釋為:aobject . operator + + ( ) ; 友員函數(shù)友員函數(shù) 重載friend a operator+

2、 (a &) ; 解釋為: operator + + ( aobject ) ; 成員函數(shù)成員函數(shù) 重載a : a operator+ (int) ; 解釋為: aobject . operator + + ( 0 ) ; 友員函數(shù)友員函數(shù) 重載:friend a operator+ (a &, int) ; 解釋為: operator+(aobject, 0) 偽參數(shù)6.3.1 重載+與-2021-11-44成員函數(shù)重載成員函數(shù)重載+#includeclass increase public : increase ( ) value=0; void display( ) co

3、nst 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 ( i

4、nt 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 重載+與-2021-11-45成員函數(shù)重載成員函數(shù)重載+#includeclass increase public : increase ( ) value=0; void display( ) const coutvaluen; ; incre

5、ase 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 + )

6、 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 ;預(yù)定義版本6.3.1 重載+與-2021-11-46成員函數(shù)重載成員函數(shù)重載+#includeclass increase

7、 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.value = value + ; retu

8、rn 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 + ( ) ;increase increase : operator + ( ) value + ; return *

9、this ; + n ;重載版本6.3.1 重載+與-2021-11-47成員函數(shù)重載成員函數(shù)重載+#includeclass increase public : increase ( ) value=0; void display( ) const coutvaluen; ; increase operator + ( ) ; / 前置 increase operator + ( int ) ; / 后置 private: unsigned value ;increase increase : operator + ( ) value + ; return *this ; increase

10、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( ) ; increas

11、e operator + ( int ) ;increase increase : operator + ( int ) increase temp; temp.value = value + ; return temp; n + ;預(yù)定義版本6.3.1 重載+與-2021-11-48成員函數(shù)重載成員函數(shù)重載+#includeclass increase public : increase ( ) value=0; void display( ) const coutvaluen; ; increase operator + ( ) ; / 前置 increase operator + ( i

12、nt ) ; / 后置 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

13、isplay( ) ; for ( i = 0 ; i 10 ; i + ) b = + 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 重載+與-2021-11-49成員函數(shù)重載成員函數(shù)重載+#includeclass increase public : increas

14、e ( ) 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 mai

15、n( ) 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 重載+與-2021-11-410友員函數(shù)重載友員函數(shù)重載+#includeclass increase public : increase ( ) value=0; void di

16、splay( ) const coutvaluen; ; friend increase operator + ( increase & ) ; / 前置 friend increase operator + ( increase &, int ) ; / 后置 private: unsigned value ;increase operator + ( increase & a ) a.value + ; return a ; increase operator + ( increase & a, int ) increase temp(a); a.value

17、 + ; 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 重載+與-2021-11-411友員函數(shù)重載友員函數(shù)重載+#includeclass increase public : inc

18、rease ( ) value=0; void display( ) const coutvaluen; ; friend increase operator + ( increase & ) ; / 前置 friend increase operator + ( increase &, int ) ; / 后置 private: unsigned value ;increase operator + ( increase & a ) a.value + ; return a ; increase operator + ( increase & a, int )

19、 increase temp(a); a.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( ) ; friend increase operator + ( increase &

20、) ;increase operator + ( increase & a ) a.value + ; return a ; + n ;通過引用參數(shù)操作對(duì)象6.3.1 重載+與-2021-11-412友員函數(shù)重載友員函數(shù)重載+#includeclass increase public : increase ( ) value=0; void display( ) const coutvaluen; ; friend increase operator + ( increase & ) ; / 前置 friend increase operator + ( increase &a

21、mp;, int ) ; / 后置 private: unsigned value ;increase operator + ( increase & a ) a.value + ; return a ; increase operator + ( increase & a, int ) increase temp(a); a.value + ; return temp; void main( ) increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout

22、 a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; friend increase operator + ( increase &, int ) ;increase operator + ( increase & a, int ) increase temp(a); a.value + ; return temp; n + ;復(fù)制構(gòu)造局部對(duì)象6.3.1 重載+與-2021-11-413友員函數(shù)重載友員函數(shù)重載+#in

23、cludeclass increase public : increase ( ) value=0; void display( ) const coutvaluen; ; friend increase operator + ( increase & ) ; / 前置 friend increase operator + ( increase &, int ) ; / 后置 private: unsigned value ;increase operator + ( increase & a ) a.value + ; return a ; increase oper

24、ator + ( increase & a, int ) increase temp(a); a.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( ) ; friend incre

25、ase operator + ( increase &, int ) ;increase operator + ( increase & a, int ) increase temp(a); a.value + ; return temp; n + ;偽參數(shù)6.3.1 重載+與-2021-11-414友員函數(shù)重載友員函數(shù)重載+#includeclass increase public : increase ( ) value=0; void display( ) const coutvaluen; ; friend increase operator + ( increase

26、& ) ; / 前置 friend increase operator + ( increase &, int ) ; / 后置 private: unsigned value ;increase operator + ( increase & a ) a.value + ; return a ; increase operator + ( increase & a, int ) increase temp(a); a.value + ; return temp; void main( ) increase a , b , n ; for ( int i = 0

27、 ; 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 重載+與-2021-11-4157.3.2 7.3.2 重載賦值運(yùn)算符重載賦值運(yùn)算符 賦值運(yùn)算符重載用于對(duì)象數(shù)據(jù)的復(fù)制 operator= 必須重載為成員函數(shù) 重載函數(shù)原型為:類型類型 & 類名類名 : operator= ( const 類名類名 &a

28、mp; ) ; 6.3.2 重載賦值運(yùn)算符2021-11-416#include#includeclass name public : name ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name& operator=( const name& ) ; / 重載賦值算符 name() ; protected : char *pname ; int size ; ;void main() name obj1( zhangsan ) ; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( nona

29、me ) ; obj3 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 6.3.2 重載賦值運(yùn)算符2021-11-417#include#includeclass name public : name ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name& operator=( const name& ) ; / 重載賦值算符 name() ; protected : char *pname ; int size ; ;void main() name obj1(

30、zhangsan ) ; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( noname ) ; obj3 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 初始化對(duì)象時(shí)調(diào)用復(fù)制構(gòu)造函數(shù)6.3.2 重載賦值運(yùn)算符2021-11-418#include#includeclass name public : name ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name& operator=( const name& ) ; / 重載賦值算符

31、 name() ; protected : char *pname ; int size ; ;void main() name obj1( zhangsan ) ; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( noname ) ; obj3 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 修改對(duì)象時(shí)調(diào)用重載賦值運(yùn)算符函數(shù)6.3.2 重載賦值運(yùn)算符2021-11-419定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) name:name ( char *pn ) cout constructing p

32、n endl ; pname = new char strlen( pn ) + 1 ; if( pname != 0 ) strcpy( pname,pn ) ; size = strlen( pn ) ; name:name( const name & obj ) /復(fù)制構(gòu)造函數(shù) cout copying obj.pname into its own blockn; pname = new charstrlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname, obj.pname ) ; size = obj.size;name

33、 & name:operator= ( const name & obj ) / 重載賦值算符 delete pname ; pname = new char strlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname , obj.pname ) ; size = obj.size ; return *this ;name: name() cout destructing pname sizelihaiobj6obj.size6.3.2 重載賦值運(yùn)算符2021-11-420name:name ( char *pn ) co

34、ut constructing pn endl ; pname = new char strlen( pn ) + 1 ; if( pname != 0 ) strcpy( pname,pn ) ; size = strlen( pn ) ; name:name( const name & obj ) /復(fù)制構(gòu)造函數(shù) cout copying obj.pname into its own blockn; pname = new charstrlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname, obj.pname ) ; siz

35、e = obj.size;name & name:operator= ( const name & obj ) / 重載賦值算符 delete pname ; pname = new char strlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname , obj.pname ) ; size = obj.size ; return *this ;name: name() cout destructing pname size6obj.size6.3.2 重載賦值運(yùn)算符2021-11-421name:name ( char

36、 *pn ) cout constructing pn endl ; pname = new char strlen( pn ) + 1 ; if( pname != 0 ) strcpy( pname,pn ) ; size = strlen( pn ) ; name:name( const name & obj ) /復(fù)制構(gòu)造函數(shù) cout copying obj.pname into its own blockn; pname = new charstrlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname, obj.pnam

37、e ) ; size = obj.size;name & name:operator= ( const name & obj ) / 重載賦值算符 delete pname ; pname = new char strlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname , obj.pname ) ; size = obj.size ; return *this ;name: name() cout destructing pname size6obj.size6.3.2 重載賦值運(yùn)算符2021-11-422name:na

38、me ( char *pn ) cout constructing pn endl ; pname = new char strlen( pn ) + 1 ; if( pname != 0 ) strcpy( pname,pn ) ; size = strlen( pn ) ; name:name( const name & obj ) /復(fù)制構(gòu)造函數(shù) cout copying obj.pname into its own blockn; pname = new charstrlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname,

39、 obj.pname ) ; size = obj.size;name & name:operator= ( const name & obj ) / 重載賦值算符 delete pname ; pname = new char strlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname , obj.pname ) ; size = obj.size ; return *this ;name: name() cout destructing pname size6obj.size6.3.2 重載賦值運(yùn)算符2021-11-4

40、23name:name ( char *pn ) cout constructing pn endl ; pname = new char strlen( pn ) + 1 ; if( pname != 0 ) strcpy( pname,pn ) ; size = strlen( pn ) ; name:name( const name & obj ) /復(fù)制構(gòu)造函數(shù) cout copying obj.pname into its own blockn; pname = new charstrlen( obj.pname ) + 1 ; if ( pname != 0 ) strcp

41、y( pname, obj.pname ) ; size = obj.size;name & name:operator= ( const name & obj ) / 重載賦值算符 delete pname ; pname = new char strlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname , obj.pname ) ; size = obj.size ; return *this ;name: name() cout destructing pname size6obj.size66.3.2 重載賦值運(yùn)算

42、符2021-11-424name:name ( char *pn ) cout constructing pn endl ; pname = new char strlen( pn ) + 1 ; if( pname != 0 ) strcpy( pname,pn ) ; size = strlen( pn ) ; name:name( const name & obj ) /復(fù)制構(gòu)造函數(shù) cout copying obj.pname into its own blockn; pname = new charstrlen( obj.pname ) + 1 ; if ( pname !=

43、 0 ) strcpy( pname, obj.pname ) ; size = obj.size;name & name:operator= ( const name & obj ) / 重載賦值算符 delete pname ; pname = new char strlen( obj.pname ) + 1 ; if ( pname != 0 ) strcpy( pname , obj.pname ) ; size = obj.size ; return *this ;name: name() cout destructing pname size6obj.size6.3

44、.2 重載賦值運(yùn)算符2021-11-425#include#includeclass name public : name ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name& operator=( const name& ) ; / 重載賦值算符 name() ; protected : char *pname ; int size ; ;void main() name obj1( zhangsan ) ; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( noname ) ; obj3

45、 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 6.3.2 重載賦值運(yùn)算符2021-11-426#include#includeclass name public : name ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name& operator=( const name& ) ; / 重載賦值算符 name() ; protected : char *pname ; int size ; ;void main() name obj1( zhangsan )

46、; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( noname ) ; obj3 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 構(gòu)造對(duì)象6.3.2 重載賦值運(yùn)算符2021-11-427#include#includeclass name public : name ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name& operator=( const name& ) ; / 重載賦值算符 name() ; protected :

47、 char *pname ; int size ; ;void main() name obj1( zhangsan ) ; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( noname ) ; obj3 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 復(fù)制構(gòu)造對(duì)象6.3.2 重載賦值運(yùn)算符2021-11-428#include#includeclass name public : name ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name&am

48、p; operator=( const name& ) ; / 重載賦值算符 name() ; protected : char *pname ; int size ; ;void main() name obj1( zhangsan ) ; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( noname ) ; obj3 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 構(gòu)造對(duì)象6.3.2 重載賦值運(yùn)算符2021-11-429#include#includeclass name public : n

49、ame ( char *pn ) ; name( const name & ) ; /復(fù)制構(gòu)造函數(shù) name& operator=( const name& ) ; / 重載賦值算符 name() ; protected : char *pname ; int size ; ;void main() name obj1( zhangsan ) ; name obj2 = obj1 ;/ 調(diào)用復(fù)制構(gòu)造函數(shù) name obj3( noname ) ; obj3 = obj2 = obj1 ;/ 調(diào)用重載賦值算符函數(shù) 定義定義name類的重載賦值函數(shù)類的重載賦值函數(shù) 析構(gòu)賦值

50、操作后的對(duì)象6.3.2 重載賦值運(yùn)算符2021-11-4307.3.3 7.3.3 重載運(yùn)算符重載運(yùn)算符和和()() 運(yùn)算符 和 () 是二元運(yùn)算符 和 () 只能用成員函數(shù)重載,不能用友元函數(shù)重載 6.3.3 重載運(yùn)算符和()2021-11-4311 1重載下標(biāo)算符重載下標(biāo)算符 重載格式類類 : 類型 operator ( 類型 ) ; 運(yùn)算符用于訪問數(shù)據(jù)對(duì)象的元素6.3.3 重載運(yùn)算符和()2021-11-4321 1重載下標(biāo)算符重載下標(biāo)算符 重載格式類類 : 類型 operator ( 類型 ) ; 運(yùn)算符用于訪問數(shù)據(jù)對(duì)象的元素定義重載函數(shù)的類名6.3.3 重載運(yùn)算符和()2021-11

51、-4331 1重載下標(biāo)算符重載下標(biāo)算符 重載格式類 : 類型類型 operator 類型 ; 運(yùn)算符用于訪問數(shù)據(jù)對(duì)象的元素函數(shù)返回類型6.3.3 重載運(yùn)算符和()2021-11-4341 1重載下標(biāo)算符重載下標(biāo)算符 重載格式類 : 類型 operator ( 類型 ) ; 運(yùn)算符用于訪問數(shù)據(jù)對(duì)象的元素函數(shù)名6.3.3 重載運(yùn)算符和()2021-11-4351 1重載下標(biāo)算符重載下標(biāo)算符 重載格式類 : 類型 operator ( 類型類型 ) ; 運(yùn)算符用于訪問數(shù)據(jù)對(duì)象的元素右操作數(shù)為符合原語(yǔ)義,用 int6.3.3 重載運(yùn)算符和()2021-11-4361 1重載下標(biāo)算符重載下標(biāo)算符 重載格

52、式類 : 類型 operator() 類型 ; 運(yùn)算符用于訪問數(shù)據(jù)對(duì)象的元素例例 設(shè) x 是類 x 的一個(gè)對(duì)象,則表達(dá)式x y 可被解釋為x . operator ( y )顯式聲明一個(gè)參數(shù)6.3.3 重載運(yùn)算符和()2021-11-4372 2重載函數(shù)調(diào)用符重載函數(shù)調(diào)用符 ()() / / 用重載用重載()()算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象#include class f public : double operator ( ) ( double x , double y ) ; ;double f : operator ( ) ( double x , double y )

53、return x * x + y * y ; void main ( ) f f ; cout f ( 5.2 , 2.5 ) endl ;6.3.3 重載運(yùn)算符和()2021-11-4382 2重載函數(shù)調(diào)用符重載函數(shù)調(diào)用符 ()() / / 用重載用重載()()算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象#include class f public : double operator ( ) ( double x , double y ) ; ;double f : operator ( ) ( double x , double y ) return x * x + y * y ; vo

54、id main ( ) f f ; cout f ( 5.2 , 2.5 ) endl ;f . operator() (5.2, 2.5)6.3.3 重載運(yùn)算符和()2021-11-4392 2重載函數(shù)調(diào)用符重載函數(shù)調(diào)用符 ()() / 用重載()算符實(shí)現(xiàn)數(shù)學(xué)函數(shù)的抽象#include class f public : double operator ( ) ( double x , double y ) ; ;double f : operator ( ) ( double x , double y ) return x * x + y * y ; void main ( ) f f ;

55、cout f ( 5.2 , 2.5 ) endl ;memfunmemfunf.memfun (5.2,2.5) 比較比較定義普通成員函數(shù)定義普通成員函數(shù)6.3.3 重載運(yùn)算符和()2021-11-440istream 和 ostream 是 c+ 的預(yù)定義流類cin 是 istream 的對(duì)象,cout 是 ostream 的對(duì)象運(yùn)算符 由 istream 重載為提取操作,用于輸入基本類型數(shù)據(jù)程序員重載 ,用于輸出和輸入用戶自定義的數(shù)據(jù)類型 7.3.4 7.3.4 重載流插入和流提取運(yùn)算符重載流插入和流提取運(yùn)算符 6.3.4 重載流插入和流提取運(yùn)算符2021-11-441#include#

56、includeclass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector a( k ) ; cout a ; cout output the elements of vector a :n ; cout a ;為為

57、vector類重載流插入運(yùn)算符和提取運(yùn)算符類重載流插入運(yùn)算符和提取運(yùn)算符 6.3.4 重載流插入和流提取運(yùn)算符2021-11-442#include#includeclass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; ve

58、ctor a( k ) ; cout a ; cout output the elements of vector a :n ; cout a ;為為vector類重載流插入運(yùn)算符和提取運(yùn)算符類重載流插入運(yùn)算符和提取運(yùn)算符 重載幾個(gè)運(yùn)算符6.3.4 重載流插入和流提取運(yùn)算符2021-11-443#include#includeclass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & in

59、put, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector a( k ) ; cout a ; cout output the elements of vector a :n ; cout a ;為為vector類重載流插入運(yùn)算符和提取運(yùn)算符類重載流插入運(yùn)算符和提取運(yùn)算符 標(biāo)準(zhǔn)流類6.3.4 重載流插入和流提取運(yùn)算符2021-11-444#include#includeclass vector public : vector( int size =1 ) ; vector() ;

60、int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector a( k ) ; cout a ; cout output the elements of vector a :n ; cout a ;為為vector類重載流插入運(yùn)算符和提取運(yùn)算符類重載流插入運(yùn)算符和提取運(yùn)算符 使用預(yù)定義版本6.3.4 重載流插入和流提取運(yùn)算符20

61、21-11-445#include#includeclass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector a( k ) ; cout a ; cout output the elements of vector a :n ; cout (a)6.3.4 重載流插入和流提取運(yùn)算符2021-11-446#include#includeclass vector public : vector( int size =1 ) ; vector() ; int & oper

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論