版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、chap10 模板 模板把函數(shù)或類(lèi)要處理的數(shù)據(jù)類(lèi)型參數(shù)化,表現(xiàn)為參數(shù)的多態(tài) 性,稱(chēng)為類(lèi)屬。 模板用于表達(dá)邏輯結(jié)構(gòu)相同,但具體數(shù)據(jù)元素類(lèi)型不同的數(shù)據(jù) 對(duì)象的通用行為。1chap10 模板10.1 什么是模板10.2 函數(shù)模板10.3 類(lèi)模板小結(jié)210.1 什么是模板類(lèi)屬 類(lèi)型參數(shù)化,又稱(chēng)參數(shù)模板 使得程序(算法)可以從邏輯功能上抽象,把被處理的對(duì)象(數(shù)據(jù))類(lèi)型作為參數(shù)傳遞C+提供兩種模板機(jī)制:函數(shù)模板類(lèi)模板310.1 什么是模板模板(函數(shù)模板和類(lèi)模板)模板函數(shù)模板類(lèi)對(duì)象模板、類(lèi)、對(duì)象和函數(shù)410.2 函數(shù)模板 考慮求兩參數(shù)之中大值函數(shù):max ( a , b )對(duì) a , b 的不同類(lèi)型,都有相
2、同的處理形式:return ( a b ) ? a : b ;用已有方法解決對(duì)不同數(shù)據(jù)類(lèi)型處理:(1)宏替換# define max ( a , b ) ( a b ? a : b )問(wèn)題 避開(kāi)類(lèi)型檢查(2)重載問(wèn)題 需要許多重載版本(3)使用函數(shù)模板510.2 函數(shù)模板 重載函數(shù)通?;诓煌臄?shù)據(jù)類(lèi)型實(shí)現(xiàn)類(lèi)似的操作 對(duì)不同數(shù)據(jù)類(lèi)型的操作完全相同,用函數(shù)模板實(shí)現(xiàn)更為簡(jiǎn)潔方便610.2.1 模板說(shuō)明template 聲明模板中使用的類(lèi)屬參數(shù)。形式為 10.2.1 模板說(shuō)明10.2.1 模板說(shuō)明template 聲明模板中使用的類(lèi)屬參數(shù)。形式為 關(guān)鍵字10.2.1 模板說(shuō)明10.2.1 模板說(shuō)明t
3、emplate 類(lèi)型形式參數(shù)的形式為:typename T1 , typename T2 , , typename Tn 或class T1 , class T2 , , class Tn 聲明模板中使用的類(lèi)屬參數(shù)。形式為 10.2.1 模板說(shuō)明類(lèi)型形式參數(shù)的形式為:typename T1 , typename T2 , , typename Tn 或class T1 , class T2 , , class Tn 10.2.1 模板說(shuō)明template 聲明模板中使用的類(lèi)屬參數(shù)。形式為 關(guān)鍵字10.2.1 模板說(shuō)明類(lèi)型形式參數(shù)的形式為:typename T1 , typename T2 ,
4、, typename Tn 或class T1 , class T2 , , class Tn 10.2.1 模板說(shuō)明template 聲明模板中使用的類(lèi)屬參數(shù)。形式為 類(lèi)屬參數(shù)10.2.1 模板說(shuō)明template template template 10.2.1 模板說(shuō)明template 聲明模板中使用的類(lèi)屬參數(shù)。形式為 例如10.2.1 模板說(shuō)明10.2.2 函數(shù)模板與模板函數(shù)template 類(lèi)型 函數(shù)名 ( 形式參數(shù)表 ) 語(yǔ)句序列 函數(shù)模板聲明 函數(shù)模板定義由模板說(shuō)明和函數(shù)定義組成 模板說(shuō)明的類(lèi)屬參數(shù)必須在函數(shù)定義中至少出現(xiàn)一次 函數(shù)參數(shù)表中可以使用類(lèi)屬類(lèi)型參數(shù),也可以使用一般類(lèi)型
5、參數(shù) 10.2.2 函數(shù)模板與模板函數(shù)#includeusing namespace std;template T Max( const T a, const T b ) return ab ? a : b ; int main() cout Max( 3, 5 ) is Max( 3, 5 ) endl ; cout Max( y, e) is Max( y, e ) endl ; cout Max( 10.3, 0.5 ) is Max( 10.3, 0.5 ) endl ;例10-1 簡(jiǎn)單函數(shù)模板應(yīng)用10.2.2 函數(shù)模板與模板函數(shù)函數(shù)模板10.2.2 函數(shù)模板與模板函數(shù)例10-1 簡(jiǎn)單
6、函數(shù)模板應(yīng)用#includeusing namespace std;template T Max( const T a, const T b ) return ab ? a : b ; int main() cout Max( 3, 5 ) is Max( 3, 5 ) endl ; cout Max( y, e) is Max( y, e ) endl ; cout Max( 10.3, 0.5 ) is Max( 10.3, 0.5 ) endl ;#includeusing namespace std;template T Max( const T a, const T b ) retu
7、rn ab ? a : b ; int main() cout Max( 3, 5 ) is Max( 3, 5 ) endl ; cout Max( y, e) is Max( y, e ) endl ; cout Max( 10.3, 0.5 ) is Max( 10.3, 0.5 ) b ? a : b ; 由實(shí)參類(lèi)型實(shí)例化10.2.2 函數(shù)模板與模板函數(shù)例10-1 簡(jiǎn)單函數(shù)模板應(yīng)用#includeusing namespace std;template T Max( const T a, const T b ) return ab ? a : b ; int main() cout M
8、ax( 3, 5 ) is Max( 3, 5 ) endl ; cout Max( y, e) is Max( y, e ) endl ; cout Max( 10.3, 0.5 ) is Max( 10.3, 0.5 ) b ? a : b ; 由實(shí)參類(lèi)型實(shí)例化char max ( char a , char b ) return a b ? a : b ; 10.2.2 函數(shù)模板與模板函數(shù)例10-1 簡(jiǎn)單函數(shù)模板應(yīng)用#includeusing namespace std;template T Max( const T a, const T b ) return ab ? a : b ;
9、int main() cout Max( 3, 5 ) is Max( 3, 5 ) endl ; cout Max( y, e) is Max( y, e ) endl ; cout Max( 10.3, 0.5 ) is Max( 10.3, 0.5 ) b ? a : b ; 由實(shí)參類(lèi)型實(shí)例化char max ( char a , char b ) return a b ? a : b ; double max ( double a , double b ) return a b ? a : b ; 10.2.2 函數(shù)模板與模板函數(shù)例10-1 簡(jiǎn)單函數(shù)模板應(yīng)用#includeusing
10、namespace std;template T Max( const T a, const T b ) return ab ? a : b ; int main() cout Max( 3, 5 ) is Max( 3, 5 ) endl ; cout Max( y, e) is Max( y, e ) endl ; cout Max( 10.3, 0.5 ) is Max( 10.3, 0.5 ) b ? a : b ; char max ( char a , char b ) return a b ? a : b ; double max ( double a , double b )
11、return a b ? a : b ; 編譯器生成的模板函數(shù)程序執(zhí)行時(shí)匹配不同的版本10.2.2 函數(shù)模板與模板函數(shù)例10-1 簡(jiǎn)單函數(shù)模板應(yīng)用template void SortBubble ( ElementType *a , int size ) int i, work ; ElementType temp ; for (int pass = 1; pass size; pass + ) work = 1; for ( i = 0; i ai+1 ) temp = ai ; ai = ai+1 ; ai+1 = temp ; work = 0 ; if ( work ) break ;
12、 例10-2 冒泡排序法的函數(shù)模板 10.2.2 函數(shù)模板與模板函數(shù)template void SortBubble ( ElementType *a , int size ) int i, work ; ElementType temp ; for (int pass = 1; pass size; pass + ) work = 1; for ( i = 0; i ai+1 ) temp = ai ; ai = ai+1 ; ai+1 = temp ; work = 0 ; if ( work ) break ; 模板聲明10.2.2 函數(shù)模板與模板函數(shù)例10-2 冒泡排序法的函數(shù)模板 t
13、emplate void SortBubble ( ElementType *a , int size ) int i, work ; ElementType temp ; for (int pass = 1; pass size; pass + ) work = 1; for ( i = 0; i ai+1 ) temp = ai ; ai = ai+1 ; ai+1 = temp ; work = 0 ; if ( work ) break ; 類(lèi)屬參數(shù)10.2.2 函數(shù)模板與模板函數(shù)例10-2 冒泡排序法的函數(shù)模板 template void SortBubble ( ElementTy
14、pe *a , int size ) int i, work ; ElementType temp ; for (int pass = 1; pass size; pass + ) work = 1; for ( i = 0; i ai+1 ) temp = ai ; ai = ai+1 ; ai+1 = temp ; work = 0 ; if ( work ) break ; 普通類(lèi)型參數(shù)10.2.2 函數(shù)模板與模板函數(shù)例10-2 冒泡排序法的函數(shù)模板 template void SortBubble ( ElementType *a , int size ) int i, work ;
15、ElementType temp ; for (int pass = 1; pass size; pass + ) work = 1; for ( i = 0; i ai+1 ) temp = ai ; ai = ai+1 ; ai+1 = temp ; work = 0 ; if ( work ) break ; 類(lèi)屬類(lèi)型變量10.2.2 函數(shù)模板與模板函數(shù)例10-2 冒泡排序法的函數(shù)模板 template void SortBubble ( ElementType *a , int size ) int i, work ; ElementType temp ; for (int pass
16、= 1; pass size; pass + ) work = 1; for ( i = 0; i ai+1 ) temp = ai ; ai = ai+1 ; ai+1 = temp ; work = 0 ; if ( work ) break ; 排序算法10.2.2 函數(shù)模板與模板函數(shù)例10-2 冒泡排序法的函數(shù)模板 10.2.3 重載函數(shù)模板有些特殊情況需要函數(shù)模板參與重載例如template T max ( T a , T b ) return a b ? a : b ; void f ( int i , char c ) max ( i , i ) ;/ ok max ( c ,
17、c ) ;/ ok max ( i , c ) ;/ error,無(wú)法匹配 max ( c , i ) ;/ error 模板類(lèi)型不能提供類(lèi)型的隱式轉(zhuǎn)換10.2.3 重載函數(shù)模板template T max ( T a , T b ) return a b ? a : b ; int max ( int a , char b )/ 模板函數(shù)重載版本 return a b ? a : b ; 10.2.3 重載函數(shù)模板void f ( int i , char c ) max ( i , i ) ;/ ok max ( c , c ) ;/ ok max ( i , c ) ;/ ok ,由系統(tǒng)
18、提供隱式轉(zhuǎn)換 max ( c , i ) ;/ ok 10.2.3 重載函數(shù)模板#includeusing namespace std ;#include template T Max( const T a, const T b ) return ab ? a : b ; template T Max( const T a, const T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; int Max( const int a , const char b ) return ab ? a : b ; int main (
19、) cout Max( 3, a ) is Max( 3, a ) endl ; cout Max(9.3, 0.5) is Max(9.3, 0.5) endl ; cout Max(9, 5, 23) is Max(9, 5, 23) endl ; 例10-3 重載函數(shù)模板示例template T Max( const T a, const T b ) return ab ? a : b ; 函數(shù)模板10.2.3 重載函數(shù)模板#includeusing namespace std ;template T Max( const T a, const T b ) return ab ? a :
20、 b ; template T Max( const T a, const T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; int Max( const int a , const char b ) return ab ? a : b ; int main ( ) cout Max( 3, a ) is Max( 3, a ) endl ; cout Max(9.3, 0.5) is Max(9.3, 0.5) endl ; cout Max(9, 5, 23) is Max(9, 5, 23) endl ; templ
21、ate T Max( const T a, const T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; template T Max( const T a, const T b ) return ab ? a : b ; 重載函數(shù)模板10.2.3 重載函數(shù)模板例10-3 重載函數(shù)模板示例#includeusing namespace std ;template T Max( const T a, const T b ) return ab ? a : b ; template T Max( const T a, cons
22、t T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; int Max( const int a , const char b ) return ab ? a : b ; int main ( ) cout Max( 3, a ) is Max( 3, a ) endl ; cout Max(9.3, 0.5) is Max(9.3, 0.5) endl ; cout Max(9, 5, 23) is Max(9, 5, 23) endl ; template T Max( const T a, const T b , co
23、nst T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; 用普通函數(shù)重載函數(shù)模板int Max( const int a , const char b ) return ab ? a : b ; template T Max( const T a, const T b ) return ab ? a : b ; 10.2.3 重載函數(shù)模板例10-3 重載函數(shù)模板示例#includeusing namespace std ;template T Max( const T a, const T b ) return ab ? a : b ; temp
24、late T Max( const T a, const T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; int Max( const int a , const char b ) return ab ? a : b ; int main ( ) cout Max( 3, a ) is Max( 3, a ) endl ; cout Max(9.3, 0.5) is Max(9.3, 0.5) endl ; cout Max(9, 5, 23) is Max(9, 5, 23) endl ; 10.2.3 重載函數(shù)模板例
25、10-3 重載函數(shù)模板示例#includeusing namespace std ;template T Max( const T a, const T b ) return ab ? a : b ; template T Max( const T a, const T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; int Max( const int a , const char b ) return ab ? a : b ; int main ( ) cout Max( 3, a ) is Max( 3, a ) en
26、dl ; cout Max(9.3, 0.5) is Max(9.3, 0.5) endl ; cout Max(9, 5, 23) is Max(9, 5, 23) endl ; 10.2.3 重載函數(shù)模板例10-3 重載函數(shù)模板示例#includeusing namespace std ;template T Max( const T a, const T b ) return ab ? a : b ; template T Max( const T a, const T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ;
27、int Max( const int a , const char b ) return ab ? a : b ; int main ( ) cout Max( 3, a ) is Max( 3, a ) endl ; cout Max(9.3, 0.5) is Max(9.3, 0.5) endl ; cout Max(9, 5, 23) is Max(9, 5, 23) endl ; 10.2.3 重載函數(shù)模板例10-3 重載函數(shù)模板示例#includeusing namespace std ;template T Max( const T a, const T b ) return ab
28、 ? a : b ; template T Max( const T a, const T b , const T c) T t ; t = Max(a, b) ; return Max ( t, c ) ; int Max( const int a , const char b ) return ab ? a : b ; int main ( ) cout Max( 3, a ) is Max( 3, a ) endl ; cout Max(9.3, 0.5) is Max(9.3, 0.5) endl ; cout Max(9, 5, 23) is Max(9, 5, 23) endl ;
29、 10.2.3 重載函數(shù)模板例10-3 重載函數(shù)模板示例10.2.3 重載函數(shù)模板 尋找和使用最符合函數(shù)名和參數(shù)類(lèi)型的函數(shù),若找到則調(diào)用它; 否則,尋找一個(gè)函數(shù)模板,將其實(shí)例化產(chǎn)生一個(gè)匹配的模板函數(shù),若找到 則調(diào)用它; 否則,尋找可以通過(guò)類(lèi)型轉(zhuǎn)換進(jìn)行參數(shù)匹配的重載函數(shù),若找到則調(diào)用它 如果按以上步驟均未能找到匹配函數(shù),則調(diào)用錯(cuò)誤。 如果調(diào)用有多于一個(gè)的匹配選擇,則調(diào)用匹配出現(xiàn)二義性。匹配約定10.2.3 重載函數(shù)模板 類(lèi)模板用于實(shí)現(xiàn)類(lèi)所需數(shù)據(jù)的類(lèi)型參數(shù)化 類(lèi)模板在表示如數(shù)組、表、圖等數(shù)據(jù)結(jié)構(gòu)顯得特別重要, 這些數(shù)據(jù)結(jié)構(gòu)的表示和算法不受所包含的元素類(lèi)型的影響10.3 類(lèi)模板10.3 類(lèi)模板類(lèi)模板
30、由模板說(shuō)明和類(lèi)說(shuō)明構(gòu)成 template 類(lèi)聲明例如 templateclass TClass / TClass的成員函數(shù) private : Type DateMember ; /;類(lèi)屬參數(shù)必須至少在類(lèi)說(shuō)明中出現(xiàn)一次 10.3.1 類(lèi)模板與模板類(lèi)10.3 類(lèi)模板templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protect
31、ed : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element
32、index = value ; 例10-4 一個(gè)數(shù)組類(lèi)模板 10.3 類(lèi)模板templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1
33、 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; templateclass Array public : Array ( int s ) ; virtual Array () ; virtual cons
34、t T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value
35、) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& valu
36、e) element index = value ; templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 數(shù)據(jù)成員是 T 類(lèi)型指針 templateclass Array public : Array ( in
37、t s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template
38、const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array
39、: Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; 類(lèi)模板的成員函數(shù)是 函數(shù)模板 10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int
40、 index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(
41、int index, const T& value) element index = value ; template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, cons
42、t T& value) element index = value ; 函數(shù)返回常引用 函數(shù)調(diào)用不能做左值修改 10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(in
43、t s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; #includeusing namespace std ;#include
44、Array.hint main() Array IntAry( 5 ) ; int i ; for ( i = 0; i 5; i + ) IntAry.Enter ( i, i ) ; cout Integer Array : n ; for ( i = 0; i 5; i + ) cout IntAry.Entry(i) t ; coutendl ; Array DouAry( 5 ) ; for ( i = 0; i 5; i + ) DouAry.Enter ( i, (i+1)*0.35 ) ; cout Double Array : n ; for ( i = 0; i 5; i
45、+ ) cout DouAry.Entry(i) t ; coutendl;10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) si
46、ze = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; #includeusing namespace std ;#include Array.hint main()
47、Array IntAry( 5 ) ; int i ; for ( i = 0; i 5; i + ) IntAry.Enter ( i, i ) ; cout Integer Array : n ; for ( i = 0; i 5; i + ) cout IntAry.Entry(i) t ; coutendl ; Array DouAry( 5 ) ; for ( i = 0; i 5; i + ) DouAry.Enter ( i, (i+1)*0.35 ) ; cout Double Array : n ; for ( i = 0; i 5; i + ) cout DouAry.En
48、try(i) t ; coutendl;10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size
49、 = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; #includeusing namespace std ;#include Array.hint main() Array IntAry( 5 )
50、; int i ; for ( i = 0; i 5; i + ) IntAry.Enter ( i, i ) ; cout Integer Array : n ; for ( i = 0; i 5; i + ) cout IntAry.Entry(i) t ; coutendl ; Array DouAry( 5 ) ; for ( i = 0; i 5; i + ) DouAry.Enter ( i, (i+1)*0.35 ) ; cout Double Array : n ; for ( i = 0; i 5; i + ) cout DouAry.Entry(i) t ; coutend
51、l;class Array public : virtual const int & Entry( int index ) const ; virtual void Enter( int index, const int & value ) ; protected : int size ; int * element ; ;10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtu
52、al void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template v
53、oid Array : Enter(int index, const T& value) element index = value ; #includeusing namespace std ;#include Array.hint main() Array IntAry( 5 ) ; int i ; for ( i = 0; i 5; i + ) IntAry.Enter ( i, i ) ; cout Integer Array : n ; for ( i = 0; i 5; i + ) cout IntAry.Entry(i) t ; coutendl ; Array DouAry(
54、5 ) ; for ( i = 0; i 5; i + ) DouAry.Enter ( i, (i+1)*0.35 ) ; cout Double Array : n ; for ( i = 0; i 5; i + ) cout DouAry.Entry(i) t ; coutendl;class Array public : virtual const int & Entry( int index ) const ; virtual void Enter( int index, const int & value ) ; protected : int size ; int * eleme
55、nt ; ;10.3 類(lèi)模板例10-4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element
56、 = new T size ; template Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; #includeusing namespace std ;#include Array.hint main() Array IntAry( 5 ) ; int i ; for
57、( i = 0; i 5; i + ) IntAry.Enter ( i, i ) ; cout Integer Array : n ; for ( i = 0; i 5; i + ) cout IntAry.Entry(i) t ; coutendl ; Array DouAry( 5 ) ; for ( i = 0; i 5; i + ) DouAry.Enter ( i, (i+1)*0.35 ) ; cout Double Array : n ; for ( i = 0; i 5; i + ) cout DouAry.Entry(i) t ; coutendl;10.3 類(lèi)模板例10-
58、4 一個(gè)數(shù)組類(lèi)模板 templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; te
59、mplate Array : Array() delete element ; template const T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; #includeusing namespace std ;#include Array.hint main() Array IntAry( 5 ) ; int i ; for ( i = 0; i 5; i +
60、) IntAry.Enter ( i, i ) ; cout Integer Array : n ; for ( i = 0; i 5; i + ) cout IntAry.Entry(i) t ; coutendl ; Array DouAry( 5 ) ; for ( i = 0; i 5; i + ) DouAry.Enter ( i, (i+1)*0.35 ) ; cout Double Array : n ; for ( i = 0; i 5; i + ) cout DouAry.Entry(i) t ; coutendl;class Array public : virtual c
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 識(shí)字3《口耳目手足》(教案)2024-2025學(xué)年 統(tǒng)編版語(yǔ)文一年級(jí)上冊(cè)
- 藥理學(xué)智慧樹(shù)知到答案2024年麗水學(xué)院
- Illustrator 實(shí)戰(zhàn)設(shè)計(jì) 課件 項(xiàng)目5圖形字體設(shè)計(jì)
- 語(yǔ)文A版第七冊(cè)語(yǔ)文全冊(cè)教案
- 公司股權(quán)融資商業(yè)計(jì)劃書(shū)
- 2024年航空制造和材料專(zhuān)用設(shè)備相關(guān)公司行業(yè)營(yíng)銷(xiāo)方案
- 2024年燃?xì)獗硐嚓P(guān)公司行業(yè)營(yíng)銷(xiāo)方案
- 公路工程造價(jià)考卷二試卷
- 鋼筋工程施工介紹
- 自考離散數(shù)學(xué)命題演算筆記
- 《我們不亂扔》課件
- 避雷針保護(hù)半徑一覽表
- 關(guān)于開(kāi)展超聲介入技術(shù)的可行性報(bào)告
- 生鮮加工倉(cāng)儲(chǔ)物流中心建設(shè)方案
- 助產(chǎn)士在孕產(chǎn)婦營(yíng)養(yǎng)與生活方式指導(dǎo)中的助產(chǎn)安全
- 婦幼保健院人事部制度概覽(崗前培訓(xùn))
- 英漢互譯單詞練習(xí)打印紙
- 醫(yī)院體檢管理系統(tǒng)需求說(shuō)明
- GB/T 2882-2023鎳及鎳合金管
- 高考模擬主題作文“熟悉的勞動(dòng)者”寫(xiě)作訓(xùn)練
- 2023年羽毛球賽閉幕式上的領(lǐng)導(dǎo)致辭
評(píng)論
0/150
提交評(píng)論