华工课件C程序设计基础第三版chap10模板_第1页
华工课件C程序设计基础第三版chap10模板_第2页
华工课件C程序设计基础第三版chap10模板_第3页
华工课件C程序设计基础第三版chap10模板_第4页
华工课件C程序设计基础第三版chap10模板_第5页
已阅读5页,还剩78页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、chap10 模板 模板把函数或类要处理的数据类型参数化,表现为参数的多态 性,称为类属。 模板用于表达逻辑结构相同,但具体数据元素类型不同的数据 对象的通用行为。1chap10 模板10.1 什么是模板10.2 函数模板10.3 类模板小结210.1 什么是模板类属 类型参数化,又称参数模板 使得程序(算法)可以从逻辑功能上抽象,把被处理的对象(数据)类型作为参数传递C+提供两种模板机制:函数模板类模板310.1 什么是模板模板(函数模板和类模板)模板函数模板类对象模板、类、对象和函数410.2 函数模板 考虑求两参数之中大值函数:max ( a , b )对 a , b 的不同类型,都有相

2、同的处理形式:return ( a b ) ? a : b ;用已有方法解决对不同数据类型处理:(1)宏替换# define max ( a , b ) ( a b ? a : b )问题 避开类型检查(2)重载问题 需要许多重载版本(3)使用函数模板510.2 函数模板 重载函数通常基于不同的数据类型实现类似的操作 对不同数据类型的操作完全相同,用函数模板实现更为简洁方便610.2.1 模板说明template 声明模板中使用的类属参数。形式为 10.2.1 模板说明10.2.1 模板说明template 声明模板中使用的类属参数。形式为 关键字10.2.1 模板说明10.2.1 模板说明t

3、emplate 类型形式参数的形式为:typename T1 , typename T2 , , typename Tn 或class T1 , class T2 , , class Tn 声明模板中使用的类属参数。形式为 10.2.1 模板说明类型形式参数的形式为:typename T1 , typename T2 , , typename Tn 或class T1 , class T2 , , class Tn 10.2.1 模板说明template 声明模板中使用的类属参数。形式为 关键字10.2.1 模板说明类型形式参数的形式为:typename T1 , typename T2 ,

4、, typename Tn 或class T1 , class T2 , , class Tn 10.2.1 模板说明template 声明模板中使用的类属参数。形式为 类属参数10.2.1 模板说明template template template 10.2.1 模板说明template 声明模板中使用的类属参数。形式为 例如10.2.1 模板说明10.2.2 函数模板与模板函数template 类型 函数名 ( 形式参数表 ) 语句序列 函数模板声明 函数模板定义由模板说明和函数定义组成 模板说明的类属参数必须在函数定义中至少出现一次 函数参数表中可以使用类属类型参数,也可以使用一般类型

5、参数 10.2.2 函数模板与模板函数#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 简单函数模板应用10.2.2 函数模板与模板函数函数模板10.2.2 函数模板与模板函数例10-1 简单

6、函数模板应用#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 ; 由实参类型实例化10.2.2 函数模板与模板函数例10-1 简单函数模板应用#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 ; 由实参类型实例化char max ( char a , char b ) return a b ? a : b ; 10.2.2 函数模板与模板函数例10-1 简单函数模板应用#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 ; 由实参类型实例化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 函数模板与模板函数例10-1 简单函数模板应用#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 ; 编译器生成的模板函数程序执行时匹配不同的版本10.2.2 函数模板与模板函数例10-1 简单函数模板应用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 冒泡排序法的函数模板 10.2.2 函数模板与模板函数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 函数模板与模板函数例10-2 冒泡排序法的函数模板 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 ; 类属参数10.2.2 函数模板与模板函数例10-2 冒泡排序法的函数模板 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 ; 普通类型参数10.2.2 函数模板与模板函数例10-2 冒泡排序法的函数模板 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 ; 类属类型变量10.2.2 函数模板与模板函数例10-2 冒泡排序法的函数模板 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 函数模板与模板函数例10-2 冒泡排序法的函数模板 10.2.3 重载函数模板有些特殊情况需要函数模板参与重载例如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,无法匹配 max ( c , i ) ;/ error 模板类型不能提供类型的隐式转换10.2.3 重载函数模板template T max ( T a , T b ) return a b ? a : b ; int max ( int a , char b )/ 模板函数重载版本 return a b ? a : b ; 10.2.3 重载函数模板void f ( int i , char c ) max ( i , i ) ;/ ok max ( c , c ) ;/ ok max ( i , c ) ;/ ok ,由系统

18、提供隐式转换 max ( c , i ) ;/ ok 10.2.3 重载函数模板#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 重载函数模板示例template T Max( const T a, const T b ) return ab ? a : b ; 函数模板10.2.3 重载函数模板#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 ; 重载函数模板10.2.3 重载函数模板例10-3 重载函数模板示例#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 ) ; 用普通函数重载函数模板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 重载函数模板例10-3 重载函数模板示例#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 重载函数模板例

25、10-3 重载函数模板示例#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 重载函数模板例10-3 重载函数模板示例#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 重载函数模板例10-3 重载函数模板示例#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 重载函数模板例10-3 重载函数模板示例10.2.3 重载函数模板 寻找和使用最符合函数名和参数类型的函数,若找到则调用它; 否则,寻找一个函数模板,将其实例化产生一个匹配的模板函数,若找到 则调用它; 否则,寻找可以通过类型转换进行参数匹配的重载函数,若找到则调用它 如果按以上步骤均未能找到匹配函数,则调用错误。 如果调用有多于一个的匹配选择,则调用匹配出现二义性。匹配约定10.2.3 重载函数模板 类模板用于实现类所需数据的类型参数化 类模板在表示如数组、表、图等数据结构显得特别重要, 这些数据结构的表示和算法不受所包含的元素类型的影响10.3 类模板10.3 类模板类模板

30、由模板说明和类说明构成 template 类声明例如 templateclass TClass / TClass的成员函数 private : Type DateMember ; /;类属参数必须至少在类说明中出现一次 10.3.1 类模板与模板类10.3 类模板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 一个数组类模板 10.3 类模板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 类模板例10-4 一个数组类模板 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 类模板例10-4 一个数组类模板 数据成员是 T 类型指针 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 ; 类模板的成员函数是 函数模板 10.3 类模板例10-4 一个数组类模板 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 ; 函数返回常引用 函数调用不能做左值修改 10.3 类模板例10-4 一个数组类模板 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 类模板例10-4 一个数组类模板 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 类模板例10-4 一个数组类模板 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 类模板例10-4 一个数组类模板 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 类模板例10-4 一个数组类模板 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 类模板例10-

58、4 一个数组类模板 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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论