9模板ppt课件

上传人:壹****1 文档编号:568767529 上传时间:2024-07-26 格式:PPT 页数:83 大小:778KB
返回 下载 相关 举报
9模板ppt课件_第1页
第1页 / 共83页
9模板ppt课件_第2页
第2页 / 共83页
9模板ppt课件_第3页
第3页 / 共83页
9模板ppt课件_第4页
第4页 / 共83页
9模板ppt课件_第5页
第5页 / 共83页
点击查看更多>>
资源描述

《9模板ppt课件》由会员分享,可在线阅读,更多相关《9模板ppt课件(83页珍藏版)》请在金锄头文库上搜索。

1、华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006第第第第9 9 9 9章章章章 模板模板模板模板 模板把函数或类要处理的数据类型参数化,表现为参数的多态模板把函数或类要处理的数据类型参数化,表现为参数的多态 性,称为类属。性,称为类属。 模板用于表达逻辑结构相同,但具体数据元素类型不同的数据模板用于表达逻辑结构相同,但具体数据元素类型不同的数据 对象的通用行为。对象的通用行为。2.华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006第第第第9 9 9 9章章章章 模板

2、模板模板模板3.华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.1 9.1 9.1 9.1 什么是模板什么是模板什么是模板什么是模板类属 类型参数化,又称参数模板 使得程序(算法)可以从逻辑功能上抽象,把被处理的对象(数据)类型作为参数传递C+提供两种模板机制:函数模板类模板4.华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.1 9.1 9.1 9.1 什么是模板什么是模板什么是模板什么是模板模板、类、对象和函数模板、类、对象和函数模板、类、对象和函数模板、类、对象和函数5.华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭

3、如 20062006华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.2 9.2 9.2 9.2 函数模板函数模板函数模板函数模板 考虑求两参数之中大值函数:max ( a , b )对 a , b 的不同类型,都有相同的处理形式:return ( a b ) ? a : b ;用已有方法解决对不同数据类型处理:(1)宏替换# define max ( a , b ) ( a b ? a : b )问题 避开类型检查(2)重载问题 需要许多重载版本(3)使用函数模板7.华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.2 9.2 9.

4、2 9.2 函数模板函数模板函数模板函数模板 重载函数通常基于不同的数据类型实现类似的操作 对不同数据类型的操作完全相同,用函数模板实现更为简洁方便8.华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.2.1 模板说明template 声明模板中使用的类属参数。形式为 9.2.1 9.2.1 模板说明模板说明华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.2.1 模板说明template 声明模板中使用的类属参数。形式为 关键字关键字9.2.1 9.2.1 模板说明模板说明华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如

5、200620069.2.1 模板说明template 类型形式参数的形式为:typename T1 , typename T2 , , typename Tn 或class T1 , class T2 , , class Tn 声明模板中使用的类属参数。形式为 9.2.1 9.2.1 模板说明模板说明华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006类型形式参数的形式为:typename T1 , typename T2 , , typename Tn 或class T1 , class T2 , , class Tn 9.2.1 模板说明template 声明模板中

6、使用的类属参数。形式为 关键字关键字9.2.1 9.2.1 模板说明模板说明华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006类型形式参数的形式为:typename T1 , typename T2 , , typename Tn 或class T1 , class T2 , , class Tn 9.2.1 模板说明template 声明模板中使用的类属参数。形式为 类属参数类属参数9.2.1 9.2.1 模板说明模板说明华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template template template 9.2.1 模板

7、说明template 声明模板中使用的类属参数。形式为 例如例如9.2.1 9.2.1 模板说明模板说明华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.2.2 函数模板与模板函数template 类型 函数名 ( 形式参数表 ) 语句序列 函数模板声明函数模板声明 函数模板定义由模板说明和函数定义组成 模板说明的类属参数必须在函数定义中至少出现一次 函数参数表中可以使用类属类型参数,也可以使用一般类型参数 9.2.2 9.2.2 函数模板与模板函数函数模板与模板函数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include te

8、mplate T max ( T a , T b ) return a b ? a : b ; void main ( ) cout max ( 3 , 5 ) is max ( 3 , 5 ) endl ; cout max ( y , e ) is max ( y , e ) endl ; cout max ( 9.3 , 0.5 ) is max ( 9.3 , 0.5 ) endl ;例例9-1 简单函数模板应用简单函数模板应用9.2.2 函数模板与模板函数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include template template

9、T max ( T a , T b )T max ( T a , T b ) return a b ? a : b ; return a b ? a : b ; void main ( ) cout max ( 3 , 5 ) is max ( 3 , 5 ) endl ; cout max ( y , e ) is max ( y , e ) endl ; cout max ( 9.3 , 0.5 ) is max ( 9.3 , 0.5 ) endl ;函数模板函数模板9.2.2 函数模板与模板函数例例9-1 简单函数模板应用简单函数模板应用华南理工大学计算机学院华南理工大学计算机学院 周

10、霭如周霭如 20062006#include template template T max ( T a , T b )T max ( T a , T b ) return a b ? a : b ; return a b ? a : b ; void main ( ) cout max ( 3 , 5 ) is max ( 3 , 5 ) endl ; cout max ( y , e ) is max ( y , e ) endl ; cout max ( 9.3 , 0.5 ) is max ( 9.3 , 0.5 ) b ? a : b ; 由实参类型实例化由实参类型实例化9.2.2

11、函数模板与模板函数例例9-1 简单函数模板应用简单函数模板应用华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include template template T max ( T a , T b )T max ( T a , T b ) return a b ? a : b ; return a b ? a : b ; void main ( ) cout max ( 3 , 5 ) is max ( 3 , 5 ) endl ; cout max ( y , e ) is max ( y , e ) endl ; cout max ( 9.3 , 0.5 )

12、is max ( 9.3 , 0.5 ) b ? a : b ; 由实参类型实例化由实参类型实例化char max ( char a , char b ) return a b ? a : b ; 9.2.2 函数模板与模板函数例例9-1 简单函数模板应用简单函数模板应用华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include template template T max ( T a , T b )T max ( T a , T b ) return a b ? a : b ; return a b ? a : b ; void main ( ) cout

13、 max ( 3 , 5 ) is max ( 3 , 5 ) endl ; cout max ( y , e ) is max ( y , e ) endl ; cout max ( 9.3 , 0.5 ) is max ( 9.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 ; 9.2.2 函数模板与模板函数例例9-1 简单函数模板应用简单函数模板应用华南理

14、工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include template template T max ( T a , T b )T max ( T a , T b ) return a b ? a : b ; return a b ? a : b ; void main ( ) cout max ( 3 , 5 ) is max ( 3 , 5 ) endl ; cout max ( y , e ) is max ( y , e ) endl ; cout max ( 9.3 , 0.5 ) is max ( 9.3 , 0.5 ) b ? a : b ; c

15、har max ( char a , char b ) return a b ? a : b ; double max ( double a , double b ) return a b ? a : b ; 编译器生成的编译器生成的模板函数模板函数程序执行时程序执行时匹配不同的版本匹配不同的版本9.2.2 函数模板与模板函数例例9-1 简单函数模板应用简单函数模板应用华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void SortBubble ( ElementType *a , int size ) int i, work ; Element

16、Type 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 ; 例例9-2 9-2 冒泡排序法的函数模板冒泡排序法的函数模板 9.2.2 函数模板与模板函数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void SortBubble ( ElementType *a , int size ) int i, wo

17、rk ; 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 ; 模板声明模板声明9.2.2 函数模板与模板函数例例9-2 9-2 冒泡排序法的函数模板冒泡排序法的函数模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void SortBubble ( ElementType *a ,

18、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 ; 类属参数类属参数9.2.2 函数模板与模板函数例例9-2 9-2 冒泡排序法的函数模板冒泡排序法的函数模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void SortBubble

19、 ( 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 ; 普通类型参数普通类型参数9.2.2 函数模板与模板函数例例9-2 9-2 冒泡排序法的函数模板冒泡排序法的函数模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006

20、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 ; 类属类型变量类属类型变量9.2.2 函数模板与模板函数例例9-2 9-2 冒泡排序法的函数模板冒泡排序法的函数模板 华南理工大学计算机学院华南理

21、工大学计算机学院 周霭如周霭如 20062006template 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 ; 排序算法排序算法9.2.2 函数模板与模板函数例例9-2 9-2 冒泡排序法的函数模板冒泡排序法

22、的函数模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.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 , c ) ;/ ok max ( i , c ) ;/ error,无法匹配 max ( c , i ) ;/ error 模板类型不能提供模板类型不能提供类型的隐式转换类型的隐式转换9.2.3 9.2.3 重载函数模板重载函数模板华南理

23、工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template T max ( T a , T b ) return a b ? a : b ; intint max ( intint a , intint b )/ 模板函数重载版本模板函数重载版本 return a b ? a : b ; 9.2.3 重载函数模板void f ( int i , char c ) max ( i , i ) ;/ ok max ( c , c ) ;/ ok max ( i , c ) ;max ( i , c ) ;/ ok ,由系统提供隐式转换由系统提供隐式转换 max ( c

24、, i ) ;max ( c , i ) ;/ ok 9.2.3 9.2.3 重载函数模板重载函数模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include #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 ) re

25、turn ab ? a : b ; void 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 ; 例例9-3 9-3 重载函数模板示例重载函数模板示例函数模板函数模板9.2.3 重载函数模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include #include template T Max( const T a, const T b ) re

26、turn 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 ; void 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

27、) endl ; 重载函数模板重载函数模板9.2.3 重载函数模板例例9-3 9-3 重载函数模板示例重载函数模板示例华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include #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 cha

28、r b ) return ab ? a : b ; void 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 ; 用普通函数用普通函数重载函数模板重载函数模板9.2.3 重载函数模板例例9-3 9-3 重载函数模板示例重载函数模板示例华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include #include template T Max( con

29、st 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 ; void 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,

30、5, 23) is Max(9, 5, 23) endl ; 9.2.3 重载函数模板例例9-3 9-3 重载函数模板示例重载函数模板示例华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include #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 ,

31、 const char b ) return ab ? a : b ; void 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 ; 9.2.3 重载函数模板例例9-3 9-3 重载函数模板示例重载函数模板示例华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include #include template T Max( const T a, cons

32、t 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 ; void 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 Ma

33、x(9, 5, 23) endl ; 9.2.3 重载函数模板例例9-3 9-3 重载函数模板示例重载函数模板示例华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#include #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

34、b ) return ab ? a : b ; void 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 ; 9.2.3 重载函数模板例例9-3 9-3 重载函数模板示例重载函数模板示例华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620069.2.3 重载函数模板 寻找和使用最符合函数名和参数类型的函数,若找到则调用它; 否则,寻找一个函数模板,将其实例化产生

35、一个匹配的模板函数,若找到 则调用它; 否则,寻找可以通过类型转换进行参数匹配的重载函数,若找到则调用它 如果按以上步骤均未能找到匹配函数,则调用错误。 如果调用有多于一个的匹配选择,则调用匹配出现二义性。匹配约定匹配约定9.2.3 9.2.3 重载函数模板重载函数模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006 类模板用于实现类所需数据的类型参数化 类模板在表示如数组、表、图等数据结构显得特别重要, 这些数据结构的表示和算法不受所包含的元素类型的影响9.3 9.3 9.3 9.3 类模

36、板类模板类模板类模板9.3 类模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006类模板由模板说明和类说明构成 template 类声明例如 templateclass TClass / TClass的成员函数 private : Type DateMember ; /;类属参数必须至少类属参数必须至少在类说明中出现一次在类说明中出现一次 9.3.1 9.3.1 类模板与模板类类模板与模板类9.3 类模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass Array public : Array ( int s )

37、 ; 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 const

38、 T& Array : Entry ( int index ) const return element index ; template void Array : Enter(int index, const T& value) element index = value ; 例例9-4 一个数组类模板一个数组类模板 9.3 类模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int

39、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 const T& Array : Entry ( int index ) const return eleme

40、nt index ; template void Array : Enter(int index, const T& value) 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 ; ;9.3 类模板例例9-

41、4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass 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 =

42、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 ; templateclass Array public : Array ( int s ) ; virtual Array

43、() ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;数据成员是数据成员是 T T 类型指针类型指针 9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass Array public : Array ( int s ) ; virtual Array () ; virtua

44、l 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 const T& Array : Entry ( int inde

45、x ) 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 : Entry ( int index ) const retur

46、n element index ; template void Array : Enter(int index, const T& value) element index = value ; 类模板的成员函数是类模板的成员函数是 函数模板函数模板 9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index ) const ;

47、 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 const T& Array : Entry ( int index ) const return element index ; temp

48、late void Array : Enter(int index, const T& value) element index = value ; #include #include Array.hvoid 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

49、 = 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;9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass Array public : Array ( int s ) ; virtual Array () ; virtual const T& Entry( int index )

50、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 const T& Array : Entry ( int index ) const return element index

51、 ; template void Array : Enter(int index, const T& value) element index = value ; #include #include Array.hvoid main() Array 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(

52、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;9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templatetemplateclass Arrayclass Array public : public : Array ( int s ) ; Array ( int s

53、) ; virtual Array () ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; virtual void Enter( int index, const T & value ) ; protected : protected : int size ; int size ; T * element ; T * elem

54、ent ; ; ;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 index = value ; #inclu

55、de #include Array.hvoid main() Array 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 ; f

56、or ( i = 0; i 5; i + ) cout DouAry.Entry(i) t ; coutendl;class Array public : virtual const intint & Entry( int index ) const ; virtual void Enter( int index, const intint & value ) ; protected : int size ; intint * element ; ;9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templat

57、etemplateclass Arrayclass Array public : public : Array ( int s ) ; Array ( int s ) ; virtual Array () ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; virtual void Enter( int index, const

58、T & value ) ; protected : protected : int size ; int size ; T * element ; 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 ; tem

59、plate void Array : Enter(int index, const T& value) element index = value ; #include #include Array.hvoid main() Array 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 ) ;

60、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 intint & Entry( int index ) const ; virtual void Enter( int index, const intint & value ) ; protected : int size ; intint * el

61、ement ; ;9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass 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

62、) 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 ; #include #include Array.hvoid main() Arra

63、y 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 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.

64、Entry(i) t ; coutendl;9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templatetemplateclass Arrayclass Array public : public : Array ( int s ) ; Array ( int s ) ; virtual Array () ; virtual Array () ; virtual const T& Entry( int index ) const ; virtual const T& Entry( int index ) c

65、onst ; virtual void Enter( int index, const T & value ) ; virtual void Enter( int index, const T & value ) ; protected : protected : int size ; int size ; T * element ; T * element ; ; ;template Array:Array(int s) if ( s 1 ) size = s ; else size = 1 ; element = new T size ; template Array : Array()

66、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 ; #include #include Array.hvoid main() Array IntAry( 5 ) ; int i ; for ( i = 0; i 5; i + ) IntAry.Enter ( i, i ) ; cout Integer Arr

67、ay : n ; for ( i = 0; i 5; i + ) cout IntAry.Entry(i) t ; coutendl ; Array 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 const doubledouble & Entry( int index

68、 ) const ; virtual void Enter( int index, const doubledouble & value ) ; protected : int size ; doubledouble * element ; ;9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templatetemplateclass Arrayclass Array public : public : Array ( int s ) ; Array ( int s ) ; virtual Array () ;

69、virtual Array () ; virtual const T& Entry( int index ) const ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; virtual void Enter( int index, const T & value ) ; protected : protected : int size ; int size ; T * element ; T * element ; ; ;template Array

70、: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 index = value ; #include #include Array.hvoid

71、 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 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 + )

72、cout DouAry.Entry(i) t ; coutendl;class Array public : virtual const doubledouble & Entry( int index ) const ; virtual void Enter( int index, const doubledouble & value ) ; protected : int size ; doubledouble * element ; ;9.3 类模板例例9-4 一个数组类模板一个数组类模板 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006函数的形式参数类型可以是

73、类模板或类模板的引用 对应的实际参数是该类模板实例化的模板类对象 当一个函数拥有类模板参数时,这个函数必定是函数模板 9.3.2 9.3.2 类模板作函数参数类模板作函数参数9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , int index ) cout x.Entry( index ) endl ; 一个用一个用 Array 作参数的函数模板作参数的函数模板 调用函数模板调用函数模板 Array DouAry( 5 ) ;Tfun(DouAry,3); 9.3.

74、2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , int index ) cout x.Entry( index ) endl ; 一个用一个用 Array 作参数的函数模板作参数的函数模板 调用函数模板调用函数模板 Array DouAry( 5 ) ;Tfun(DouAry,3); 建立对象9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , i

75、nt index ) cout x.Entry( index ) endl ; class Array public : Array ( int s ) ; virtual Array () ; virtual const double & Entry( int index ) const ; virtual void Enter( int index, const double & value ) ; private: int size ; double * element ; ; 一个用一个用 Array 作参数的函数模板作参数的函数模板 调用函数模板调用函数模板 Array DouAry

76、( 5 ) ;Tfun(DouAry,3); 建立对象生成模板类生成模板类9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , int index ) cout x.Entry( index ) endl ; class Array public : Array ( int s ) ; virtual Array () ; virtual const double & Entry( int index ) const ; virtual void Enter( int

77、index, const double & value ) ; private: int size ; double * element ; ; 一个用一个用 Array 作参数的函数模板作参数的函数模板 调用函数模板调用函数模板 Array DouAry( 5 ) ;Tfun(DouAry,3); 调用构造函数,实例化模板类,建立对象 建立对象9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , int index ) cout x.Entry( index ) en

78、dl ; 一个用一个用 Array 作参数的函数模板作参数的函数模板 调用函数模板调用函数模板 Array DouAry( 5 ) ;Tfun(DouAry,3); class Array public : Array ( int s ) ; virtual Array () ; virtual const double & Entry( int index ) const ; virtual void Enter( int index, const double & value ) ; private: int size ; double * element ; ; 调用构造函数,实例化模板

79、类,建立对象 建立对象建立对象建立对象9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , int index ) cout x.Entry( index ) endl ; 一个用一个用 Array 作参数的函数模板作参数的函数模板 调用函数模板调用函数模板 Array DouAry( 5 ) ;Tfun(DouAry,3); class Array public : Array ( int s ) ; virtual Array () ; virtual const

80、double & Entry( int index ) const ; virtual void Enter( int index, const double & value ) ; private: int size ; double * element ; ; 调用构造函数,实例化模板类,建立对象 建立对象 调用函数9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , int index ) cout x.Entry( index ) endl ; 一个用一个用 A

81、rray 作参数的函数模板作参数的函数模板 调用函数模板调用函数模板 Array DouAry( 5 ) ;Tfun(DouAry,3); class Array public : Array ( int s ) ; virtual Array () ; virtual const double & Entry( int index ) const ; virtual void Enter( int index, const double & value ) ; private: int size ; double * element ; ; 调用构造函数,实例化模板类,建立对象 建立对象 调

82、用函数virtual const double & Entry( int index ) const ;void Tfun( const Array & x , int index ) cout x.Entry( index ) endl ; 实例化为实例化为模板函数模板函数9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template void Tfun( const Array & x , int index ) cout x.Entry( index ) endl ; 一个用一个用 Array 作参数的函数模板作参数的函数模板 调

83、用函数模板调用函数模板 Array DouAry( 5 ) ;Tfun(DouAry,3); class Array public : Array ( int s ) ; virtual Array () ; virtual const double & Entry( int index ) const ; virtual void Enter( int index, const double & value ) ; private: int size ; double * element ; ; 调用构造函数,实例化模板类,建立对象 建立对象 调用函数virtual const double

84、 & Entry( int index ) const ;调用调用模板函数模板函数9.3.2 类模板作函数参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006一个类模板在类层次结构中既可以是基类也可以是派生类: 类模板可以从模板类派生 类模板可以从非模板类派生 模板类可以从类模板派生 非模板类可以从类模板派生9.3.3 9.3.3 在类层次中的类模板在类层次中的类模板9.3.3 在类层次中的类模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006templateclass Array public : Array ( int s ) ; v

85、irtual Array () ; virtual const T& Entry( int index ) const ; virtual void Enter( int index, const T & value ) ; protected : int size ; T * element ; ;template class BoundArray : public Array public : BoundArray ( int low = 0, int height = 1 ) ; virtual const T& Entry ( int index ) const ; virtual v

86、oid Enter ( int index , const T& value ) ; private: int min ; ; 从类模板从类模板Array派生一个安全数组类模板派生一个安全数组类模板BoundArray9.3.3 在类层次中的类模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006 类模板派生普通类,在定义派生类时要对基类的抽象类参数实例化 从普通类派生模板类,意味着派生类添加了抽象类数据成员9.3.3 9.3.3 在类层次中的类模板在类层次中的类模板9.3.3 在类层次中的类模板华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 200620

87、06#includetemplate/定义类模板定义类模板class A public : A( T x ) t = x ; void out() cout t endl ; protected : T t ; ;从类模板从类模板A派生普通类派生普通类B9.3.3 在类层次中的类模板class B: public AA/派生一般类派生一般类 public : B ( int a, double x ) : A A ( a ) y = x ; void out() A A : out() ; cout y endl ; protected : double y ;void main() A a(

88、 123 ) ; a.out() ; B b ( 789, 5.16 ) ; b.out() ;实例化基类实例化基类抽象类型参数抽象类型参数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006在类模板中可以声明各种友员关系 一个函数或函数模板可以类是或类模板的友员 一个类或类模板可以是类或类模板的友员类 声明这种模板之间的友员关系符号比较烦琐9.3.4 9.3.4 类模板与友员类模板与友员华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template class X / friend void f1();friend void f1();

89、函数f1成为类模板X实例化的每个模板类的友员函数template class X / friend void f2( X & );friend void f2( X & ); 对特定类型(如double),使模板函数f2(X&)成为X的友员9.3.4 9.3.4 类模板与友员类模板与友员华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template class X / friend void A:f3();friend void A:f3(); A类的成员函数f3成为类模板X实例化的每个模板类的友员函数template class X / friend void

90、B:f4( X & );friend void B:f4( X & ); 对特定类型(如double),使模板类B的成员函数f4(X&)成为模板类X的友员9.3.4 9.3.4 类模板与友员类模板与友员华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template class X / friend class Y;friend class Y; Y类的每个成员函数成为类模板X实例化的每个模板类的友员函数9.3.4 9.3.4 类模板与友员类模板与友员9.3.3 在类层次中的类模板template class X / friend class Z;friend cl

91、ass Z; 对特定类型(如double),使模板类Z所有成员函数成为模板类X的友员华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#includetemplateclass Complex public: Complex( T r =0, T i =0 ); Complex(T a) Real = a ; Image = 0 ; void print() const; friend Complex operator+ ( const Complex & c1, const Complex & c2 ); friend Complex operator- ( con

92、st Complex & c1, const Complex & c2 ); friend Complex operator- ( const Complex & c ); private: T Real, Image ;void main() Complex c1( 2.5,3.7 ), c2( 4.2, 6.5 ) ; Complex c ; c = c1 - c2 ; c.print() ; c = c2 + c2 ; c.print() ; c = - c1 ; c.print() ;例例9-5 为复数类模板用定义重载运算为复数类模板用定义重载运算符友员函数符友员函数华南理工大学计算机

93、学院华南理工大学计算机学院 周霭如周霭如 20062006#includetemplateclass Complex public: Complex( T r =0, T i =0 ); Complex(T a) Real = a ; Image = 0 ; void print() const; friend Complex operator+ ( const Complex & c1, const Complex & c2 ); friend Complex operator- ( const Complex & c1, const Complex & c2 ); friend Compl

94、ex operator- ( const Complex & c ); private: T Real, Image ;void main() Complex c1( 2.5,3.7 ), c2( 4.2, 6.5 ) ; Complex c ; c = c1 - c2 ; c.print() ; c = c2 + c2 ; c.print() ; c = - c1 ; c.print() ;例例9-5 为复数类模板用定义重载运算为复数类模板用定义重载运算符友员函数符友员函数friend Complex operator+ ( const Complex & c1, const Complex

95、 & c2 );friend Complex operator+ ( const Complex & c1, const Complex & c2 );friend Complex operator- ( const Complex & c1, const Complex & c2 );friend Complex operator- ( const Complex & c1, const Complex & c2 );friend Complex operator- ( const Complex & c );friend Complex operator- ( const Complex

96、& c );templatetemplateclass Complexclass Complex华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006template Complex:Complex( T r, T i ) Real = r ; Image = i ; template Complex operator +( const Complex & c1, const Complex & c2 ) T r = c1.Real + c2.Real ; T i = c1.Image+c2.Image ; return Complex( r, i ) ;template

97、 Complex operator - ( const Complex & c1, const Complex & c2 ) T r = c1.Real - c2.Real ; T i = c1.Image - c2.Image ; return Complex( r, i ) ;templateComplex operator- ( const Complex & c ) return Complex( -c.Real, -c.Image ); template void Complex:print()const cout ( Real , Image ) endl; 例例9-5 为复数类模

98、板用定义重载运算为复数类模板用定义重载运算符友员函数符友员函数template Complex:Complex( T r, T i ) Real = r ; Image = i ; template Complex operator +( const Complex & c1, const Complex & c2 ) T r = c1.Real + c2.Real ; T i = c1.Image+c2.Image ; return Complex( r, i ) ;template Complex operator - ( const Complex & c1, const Complex

99、 & c2 ) T r = c1.Real - c2.Real ; T i = c1.Image - c2.Image ; return Complex( r, i ) ;templateComplex operator- ( const Complex & c ) return Complex( -c.Real, -c.Image ); template void Complex:print()const cout ( Real , Image ) endl; 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#includetemplateclass Comple

100、x public: Complex( T r =0, T i =0 ); Complex(T a) Real = a ; Image = 0 ; void print() const; friend Complex operator+ ( const Complex & c1, const Complex & c2 ); friend Complex operator- ( const Complex & c1, const Complex & c2 ); friend Complex operator- ( const Complex & c ); private: T Real, Imag

101、e ;void main() Complex c1( 2.5,3.7 ), c2( 4.2, 6.5 ) ; Complex c ; c = c1 - c2 ; c.print() ; c = c2 + c2 ; c.print() ; c = - c1 ; c.print() ;例例9-5 为复数类模板用定义重载运算为复数类模板用定义重载运算符友员函数符友员函数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006 从类模板实例化的每个模板类有自己的类模板数据成员,该模板类的所有对象共享一个staticstatic数据成员 和非模板类的staticstatic数据成员一

102、样,模板类的staticstatic数据成员也应该在文件范围定义和初始化 每个模板类有自己的类模板的staticstatic数据成员副本9.3.5 9.3.5 类模板与类模板与staticstatic成员成员华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#includeconst double pi=3.14159;template class Circle T radius ; static int total;/类模板的静态数据成员 public : Circle(T r=0) radius = r ; total+; void Set_Radius( T r

103、 ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * pi * radius ; double Get_Area() return pi * radius * radius ; static int ShowTotal();/类模板的静态成员函数 ;template int Circle:total=0;template int Circle:ShowTotal() return total; 例例9-6 为圆类模板定义静态成员为圆类模板定义静态成员华南理工大学计算机学院华南理工大学计

104、算机学院 周霭如周霭如 20062006#includeconst double pi=3.14159;template class Circle T radius ; static int total;/类模板的静态数据成员 public : Circle(T r=0) radius = r ; total+; void Set_Radius( T r ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * pi * radius ; double Get_Area() return p

105、i * radius * radius ; static int ShowTotal();/类模板的静态成员函数 ;template int Circle:total=0;template int Circle:ShowTotal() return total; 例例9-6 为圆类模板定义静态成员为圆类模板定义静态成员template class Circletemplate class Circle华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#includeconst double pi=3.14159;template class Circle T radi

106、us ; static int total;/类模板的静态数据成员 public : Circle(T r=0) radius = r ; total+; void Set_Radius( T r ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * pi * radius ; double Get_Area() return pi * radius * radius ; static int ShowTotal();/类模板的静态成员函数 ;template int Circle:to

107、tal=0;template int Circle:ShowTotal() return total; 例例9-6 为圆类模板定义静态成员为圆类模板定义静态成员static int total;static int total;template int Circle:total=0;template int Circle:total=0;Circle(T r=0) radius = r ; total+;total+; 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006#includeconst double pi=3.14159;template class Cir

108、cle T radius ; static int total;/类模板的静态数据成员 public : Circle(T r=0) radius = r ; total+; void Set_Radius( T r ) radius = r ; double Get_Radius() return radius ; double Get_Girth() return 2 * pi * radius ; double Get_Area() return pi * radius * radius ; static int ShowTotal();/类模板的静态成员函数 ;template int

109、 Circle:total=0;template int Circle:ShowTotal() return total; 例例9-6 为圆类模板定义静态成员为圆类模板定义静态成员static int ShowTotal();static int ShowTotal();int Circle:ShowTotal() return total; int Circle:ShowTotal() return total; 华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006例例9-6 为圆类模板定义静态成员为圆类模板定义静态成员void main() Circle A, B

110、;/建立了2个对象 A.Set_Radius( 16 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth = A.Get_Girth() endl ; cout A.Area = A.Get_Area() endl ; B.Set_Radius( 105 ) ; cout B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; coutTotal1=Circle:ShowTotal()end

111、l;/显示建立的对象数 coutendl; Circle X(6.23), Y(10.5), Z(25.6);/建立了3个对象 cout X.Radius = X.Get_Radius() endl ; cout X.Girth = X.Get_Girth() endl ; cout X.Area = X.Get_Area() endl ; cout Y.radius = Y.Get_Radius() endl ; cout Y.Girth= Y.Get_Girth() endl ; cout Y.Area = Y.Get_Area() endl ; cout Z.Girth= Z.Get_G

112、irth() endl ; cout Z.Area = Z.Get_Area() endl ; coutTotal2=Circle:ShowTotal()endl; /显示建立的对象数 Circle A, B ; A.Set_Radius( 16 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth = A.Get_Girth() endl ; cout A.Area = A.Get_Area() endl ; B.Set_Radius( 105 ) ; cout B.radius = B.Get_Radius() endl ; cout

113、 B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; coutTotal1=Circle:ShowTotal()endl; A.ShowTotal()或或 B.ShowTotal()华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006例例9-6 为圆类模板定义静态成员为圆类模板定义静态成员void main() Circle A, B ;/建立了2个对象 A.Set_Radius( 16 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth

114、 = A.Get_Girth() endl ; cout A.Area = A.Get_Area() endl ; B.Set_Radius( 105 ) ; cout B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; coutTotal1=Circle:ShowTotal()endl;/显示建立的对象数 coutendl; Circle X(6.23), Y(10.5), Z(25.6);/建立了3个对象 cout X.Radius = X

115、.Get_Radius() endl ; cout X.Girth = X.Get_Girth() endl ; cout X.Area = X.Get_Area() endl ; cout Y.radius = Y.Get_Radius() endl ; cout Y.Girth= Y.Get_Girth() endl ; cout Y.Area = Y.Get_Area() endl ; cout Z.Girth= Z.Get_Girth() endl ; cout Z.Area = Z.Get_Area() endl ; coutTotal2=Circle:ShowTotal()endl

116、; /显示建立的对象数 Circle X(6.23), Y(10.5), Z(25.6); cout X.Radius = X.Get_Radius() endl ; cout X.Girth = X.Get_Girth() endl ; cout X.Area = X.Get_Area() endl ; cout Y.radius = Y.Get_Radius() endl ; cout Y.Girth= Y.Get_Girth() endl ; cout Y.Area = Y.Get_Area() endl ; cout Z.Girth= Z.Get_Girth() endl ; cout

117、 Z.Area = Z.Get_Area() endl ; coutTotal2=Circle:ShowTotal()endl; X.ShowTotal()或或 Y.ShowTotal()华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006例例9-6 为圆类模板定义静态成员为圆类模板定义静态成员void main() Circle A, B ;/建立了2个对象 A.Set_Radius( 16 ) ; cout A.Radius = A.Get_Radius() endl ; cout A.Girth = A.Get_Girth() endl ; cout A.Area

118、 = A.Get_Area() endl ; B.Set_Radius( 105 ) ; cout B.radius = B.Get_Radius() endl ; cout B.Girth= B.Get_Girth() endl ; cout B.Area = B.Get_Area() endl ; coutTotal1=Circle:ShowTotal()endl;/显示建立的对象数 coutendl; Circle X(6.23), Y(10.5), Z(25.6);/建立了3个对象 cout X.Radius = X.Get_Radius() endl ; cout X.Girth =

119、 X.Get_Girth() endl ; cout X.Area = X.Get_Area() endl ; cout Y.radius = Y.Get_Radius() endl ; cout Y.Girth= Y.Get_Girth() endl ; cout Y.Area = Y.Get_Area() endl ; cout Z.Girth= Z.Get_Girth() endl ; cout Z.Area = Z.Get_Area() endl ; coutTotal2=Circle:ShowTotal()endl; /显示建立的对象数华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006 模板是C+类型参数化的多态工具。C+提供函数模板和类模板。 模板定义以模板说明开始。类属参数必须在模板定义中至少出现一次。 同一个类属参数可以用于多个模板。 类属参数可用于函数的参数类型、返回类型和声明函数中的变量。 模板由编译器根据实际数据类型实例化,生成可执行代码。实例化的函数。 模板称为模板函数;实例化的类模板称为模板类。 函数模板可以用多种方式重载。 类模板可以在类层次中使用 。小结小结小结小结华南理工大学计算机学院华南理工大学计算机学院 周霭如周霭如 20062006

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号