1 / 14

第 13 章 模板

第 13 章 模板. 1. 3. 内容提要. 本章主要介绍模板的基本概念,介绍使用模板的必要性,模板的分类。 重点介绍函数模板的定义、函数模板的使用以及函数模板的重载。 介绍类模板的定义方法和类模板的调用方法。. 模板的基本概念. 所谓模板,就是将某段程序中的数据类型参数化,使得它能够处理某个范围内的数据类型 而不必为每种可能的类型都建立一个实例,从而避免了重复劳动,增强了程序的灵活性和有效性。. 使用模板的必要性. C++ 中的模板提供了重用源代码的方法。例如,设计一个求两参数最大值的函数,不使用模板时,需要定义四个函数:

sarah
Download Presentation

第 13 章 模板

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第13章 模板 1 3

  2. 内容提要 • 本章主要介绍模板的基本概念,介绍使用模板的必要性,模板的分类。 • 重点介绍函数模板的定义、函数模板的使用以及函数模板的重载。 • 介绍类模板的定义方法和类模板的调用方法。

  3. 模板的基本概念 • 所谓模板,就是将某段程序中的数据类型参数化,使得它能够处理某个范围内的数据类型 • 而不必为每种可能的类型都建立一个实例,从而避免了重复劳动,增强了程序的灵活性和有效性。

  4. 使用模板的必要性 • C++中的模板提供了重用源代码的方法。例如,设计一个求两参数最大值的函数,不使用模板时,需要定义四个函数: • int max (int a , int b){ return( a > b ) ? a , b; } • long max ( long a , long b ){ return ( a > b ) ? a , b; } • double max( double a , double b){ return ( a > b) ? a , b;} • char max( char a , char b ){ return( a > b) ? a , b; } • 如果使用模板,则只需要定义一个函数: • Template<class type>type max(type a , type b) • {return( a > b) ? a , b; }

  5. 使用模板的必要性 案例名称:使用模板的必要性 程序名称:proj13_01.cpp #include <iostream.h> template <class T> T max(T a, T b) { return a > b ? a : b; } void main() { cout << "max(20, 30) = " << max(20, 30) << endl; cout << "max('t', 'v') = " << max('t', 'v') << endl; cout << "max(10.1, 15.2) = " << max(10.1, 15.2) << endl; }

  6. 模板的分类 • C++程序由类和函数组成,模板也分为类模板(class template)和函数模板(function template)。 • 在说明了一个函数模板后,当编译系统发现有一个对应的函数调用时,将根据实参中的类型来确认是否匹配函数模板中对应的形参,然后生成一个重载函数。该重载函数的定义体与函数模板的函数定义体相同,它称之为模板函数(template function)。同样,在说明了一个类模板之后,可以创建类模板的实例,即生成模板类。

  7. 函数模板 • 函数模板的定义方法是: • template <<模板参数表>> • <返回类型> <函数名> ( <函数参数表>) • { <函数体> } • 其中,template是关键字,模板参数表用一对尖括号括起来,内有一个或多个模板参数,不能为空。

  8. 使用函数模板 案例名称:使用函数模板 程序名称:proj13_02.cpp #include <iostream.h> template <class T> T min(T a[] , int n) { int i; T minv = a[0]; for(i = 1 ; i < n ; i++) { if(minv > a[i]) minv = a[i]; } return minv; } void main() { int a[]={1,3,0,2,7,6,4,5,2}; double b[]={1.2,-3.4,6.8,9,8}; cout<<"a数组的最小值为:" <<min(a,9)<< endl; cout<<"b数组的最小值为:" <<min(b,4)<<endl; }

  9. 函数模板的重载 案例名称:使用函数模板 程序名称:proj13_03.cpp #include <iostream.h> template <class T> T min(T x, T y) { cout<<"调用min(T x, T y),最小值为"; return x > y ? y : x; } template <class T> T min(T a[] , int n) { int i; T minv = a[0]; for(i = 1 ; i < n ; i++) { if(minv > a[i]) minv = a[i]; } cout<<"调用min(T a[], int n),最小值为"; return minv; } void main() { int x = 8, y = 23; double a[5] ={ 2.342, 11.346, 8.93, 18.111 , 5.930}; cout << min(x,y) << endl; cout << min(a , 5) << endl; }

  10. 类模板 • 类模板和函数模板的定义格式基本相同 • template <<模板参数表>> • class <类名> • { • <类体> • }; • 其中,template是关键字,模板参数表用一对尖括号括起来,内有一个或多个模板参数,不能为空。

  11. 使用类模板 案例名称:使用类模板 程序名称:proj13_04.cpp #include <iostream.h> #include <string> using namespace std; template <class T> class Array { public: Array(T a, T b) { x = a; y = b; } T sum() { T tempsum = x + y; return tempsum; } private: T x; T y; }; void main() { Array <int> A (7.0 , 6.0); cout << "A的元素之和为"<< A.sum()<<endl; Array <double> B (1.0 , 6.0 ); cout << "B的元素之和为"<< B.sum()<<endl; }

  12. 小结 • 模板是C++的重要语法特征。本章需要了解使用模板的必要性,使用模板可以提高代码的重用性的原因。 • 熟悉模板分成两大类:函数和类模板。熟练掌握函数模板的定义和使用以及如何使用函数模板重载。 • 熟练掌握类模板的定义和使用。

  13. 本章习题 • 1、模板参数表中参数使用的符号分隔是___________。 • A) , B) ; • C) / D) 以上都不是 • 2、类模板规定了如何创建一个___________。 • A) 对象B) 类 • C) 实例D) 变量 • 3、类模板的模板参数___________。 • A) 只可以作为数据成员的类型。B) 只可以作为成员函数的返回类型 • C) 只可以作为成员函数参数类型D) 以上都可以 • 4、以上对模板的声明,正确的是___________。 • A) template<T> B) template <class T1, T2) • C) template<class T1, class T2) D) template <class T1; class T2) • 5、有类模板定义如下: • template <class Type> • class Dream • {… • }; • 下列类对象定义中,正确的是___________。 • A) Dream <Type> a; B) Dream <class int> a; • C) Dream <int> a; D) Dream <class Type> a;

  14. 本章习题 • 1、填写下面的空格,将程序代码补充完整。 • template < ___________> • ___________min(T x, T y) { • cout<<"调用min(T x, T y),最小值为"; • return ___________ • } • 2、C++模板分为___________和___________。 • 3、本质上复合数据类型中的vector和___________都是系统定义好的模板类。比如定义vector的时候,使用的语法是“___________”。

More Related