类模板的小结理论篇(转)

来源:互联网 发布:电信网络电视怎么收费 编辑:程序博客网 时间:2024/06/02 04:21

类模板的定义:

如同函数模板一样,使用类模板使用户可以为类定义一种模式,使得类中的某些数据成员、某些成员函数的参数、某些成员函数的返回值能取任意类型。类模板是对一批仅仅成员数据类型不同的类的抽象,程序员只要为这一批类所组成的整个类家族创建一个类模板,给出一套程序代码,就可以用来生成多种具体的类,(这类可以看作是类模板的实例),从而大大提高编程的效率。

定义类模板的一般形式是:
 template <类型名 参数名1,类型名参数名2,…>
 class 类名
 {
  类声明体
 };

 例如,template <class T>
 class Smemory
 {…
  public:
  void mput(T x);
  …
 }

 表示定义一个名为Smemory的类模板,其中带类型参数T。

在类模板的外部定义类成员函数的一般形式是:
 template <类型名 参数名1,类型名参数名2,…>
 函数返回值类型 类名<参数名 1 参数名 2,…>::成员函数名(形参表)
 {
  函数体
 }

 例如:template <class T>
  void Smemory<T>::mput(T x)
  {…}

  表示定义一个类模板Smemory的成员函数,函数名为mput,形参x的类型是T,函数无返回值。
  类模板是一个类家族的抽象,它只是对类的描述,编译程序不为类模板(包括成员函数定义)创建程序代码,但是通过对类模板的实例化可以生成一个具体的类以及该具体类的对象。

与函数模板不同的是:

函数模板的实例化是由编译程序在处理函数调用时自动完成的,而类模板的实例化必须由程序员在程序中显式地指定

其实例化的一般形式是:
  类名 <数据类型 1(或数据),数据类型 2(或数据)…> 对象名

 例如,Smemory<int> mol;
  表示将类模板Smemory的类型参数T全部替换成int 型,从而创建一个具体的类,并生成该具体类的一个对象mol。

由于类模板包含类型参数,因此又称为参数化的类。如果说类是对象的抽象,对象是类的实例,则类模板是类的抽象,类是类模板的实例。利用类模板可以建立含各种数据类型的类。


<span style="color:#660000;">#include <iostream>using namespace std;template <class numtype>//定义类模板class Compare{   public :   Compare(numtype a,numtype b)   {x=a;y=b;}   numtype max( )   {return (x>y)?x:y;}   numtype min( )   {return (x<y)?x:y;}   private :   numtype x,y;};int main( ){   Compare<int > cmp1(3,7);  //定义对象cmp1,用于两个整数的比较   cout<<cmp1.max( )<<" is the Maximum of two integer numbers."<<endl;   cout<<cmp1.min( )<<" is the Minimum of two integer numbers."<<endl<<endl;   Compare<float > cmp2(45.78,93.6);  //定义对象cmp2,用于两个浮点数的比较   cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;   cout<<cmp2.min( )<<" is the Minimum of two float numbers."<<endl<<endl;   Compare<char> cmp3('a','A');  //定义对象cmp3,用于两个字符的比较   cout<<cmp3.max( )<<" is the Maximum of two characters."<<endl;   cout<<cmp3.min( )<<" is the Minimum of two characters."<<endl;   return 0;}</span>
运行结果:



7 is the Maximum of two integer numbers.
3 is the Minimum of two integer numbers.


93.6 is the Maximum of two float numbers.
45.78 is the Minimum of two float numbers.


a is the Maximum of two characters.
A is the Minimum of two characters.
Press any key to continue



归纳以上的介绍,可以这样声明和使用类模板:
1) 先写出一个实际的类。由于其语义明确,含义清楚,一般不会出错。

2) 将此类中准备改变的类型名(如int要改变为float或char)改用一个自己指定的虚拟类型名(如上例中的numtype)。

3) 在类声明前面加入一行,格式为:
    template <class 虚拟类型参数>
如:
    template <class numtype> //注意本行末尾无分号
    class Compare
    {…}; //类体

4) 用类模板定义对象时用以下形式:
    类模板名<实际类型名> 对象名;
    类模板名<实际类型名> 对象名(实参表列);
如:
    Compare<int> cmp;
    Compare<int> cmp(3,7);

5) 如果在类模板外定义成员函数,应写成类模板形式:
   template <class 虚拟类型参数>
   函数类型 类模板名<虚拟类型参数>::成员函数名(函数形参表列) {…}

关于类模板的几点说明:
1) 类模板的类型参数可以有一个或多个,每个类型前面都必须加class,如:
    template <class T1,class T2>
    class someclass
    {…};
在定义对象时分别代入实际的类型名,如:
    someclass<int,double> obj;

2) 和使用类一样,使用类模板时要注意其作用域,只能在其有效作用域内用它定义对象。

3) 模板可以有层次,一个类模板可以作为基类,派生出派生模板类。




0 0
原创粉丝点击