类模板
允许使用函数模板,对于功能相同而数据类型不同的一些函数,不必一一定义各个函数,可以定义一个可对任何类型变量进行操作的函数模板,在调用函数时,系统会根据实参的类型,取代函数模板中的类型参数,得到具体的函数。这样可以简化程序设计。
类模板一般定义形式:
template
<class虚拟类型参数>//声明一个模板,虚拟类型名为numtype
class 类模板名
{ 类体定义 }
请将此类模板和类定义作一比较,可以看到有两处不同:
(1)声明类模板时要增加一行:
template<class虚拟类型参数>
(2)原有的类型名换成虚拟类型参数名numtype。在建立类对象时,如果将实际类型指定为int型,编译系统就会用int取代所有的numtype,如果指定为float型,就用float取代所有的numtype。这样就能实现"一类多用"。
(3)由于类模板包含类型参数,因此又称为参数化的类。如果说类是对象的抽象,对象是类的实例,则类模板是类的抽象,类是类模板的实例。利用类模板可以建立含各种数据类型的类。
(4)类模板的引用:类模板名<实际类型名> 对象名(实参表列);
例14 声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。
#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;} //引用C语言中条件运算符
numtype
min()
{return
(x<y)?x:y;}
private:
numtype
x,y; };
int
main()
{
Compare<int> cmp1(3,7); //定义对象cmpl,用于两个整数的比较
cout<<cmp1.max()<<"
is the Maximum of two inteder numbers."<<endl;
cout<<cmp1.min()<<"
is the Minimum of two inteder 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;}
运行结果如下:
7
is the Maximum Of two integers
3
is the Minimum Of two integers.
93.6
is the Maximum Of two float numbers.
45.78
is the Minimum OftWO float numbers.
a
is the Maximum Of two characters.
A
is the Minimum Of two characters.
可以这样声明和使用类模板:
(1)先写出一个实际的类(如本节开头的Compare
int)。由于其语义明确,含义清楚,一般不会出错。
(2)将此类中准备改变的类型名(如int要改变为float或char)改用一个自己指定的虚拟类型名(如上例中的numtype)。
(3)在类声明前面加入一行,格式为
template<class虚拟类型参数>
如 template<classnumtype> //注意本行末尾无分号
class
Compare
{…};//类体
(4)用类模板定义对象时用以下形式:
类模板名<实际类型名>对象名;
类模板名<实际类型名> 对象名(实参表列);
(5)如果在类模板外定义成员函数,应写成类模板形式:
template<class虚拟类型参数>
函数类型类模板名<虚拟类型参数>::成员函数名(函数形参表列){…}
说明:
(1)类模板的类型参数可以有一个或多个,每个类型前面都必须加class,如
template<class
Tl,class
T2>
class
someclass
{…};
在定义对象时分别代人实际的类型名,如
someelass<int,double>
obj;
(2)和使用类一样,使用类模板时要注意其作用域,只能在其有效作用域内用它定义对象。如果类模板是在A文件开头定义的,则A文件范围内为有效作用域,可以在其中的任何地方使用类模板,但不能在B文件中用类模板定义对象。
(3)模板可以有层次,一个类模板可以作为基类,派生出派生模板类。
#include <iostream>
using namespace std;
template<class numtype> //定义类模板
class Compare
{
public:
Compare(numtype a,numtype b)
{
x=a;
y=b;
}
numtype max();
numtype min();
private:
numtype x,y;
};
template<class numtype>
numtype Compare<numtype>::max()
{
return (x>y)?x:y;
}
template<class numtype>
numtype Compare<numtype>::min()
{
return (x<y)?x:y;
}
int main()
{
Compare<int> cmp1(3,7); //定义对象cmpl,用于两个整数的比较
cout<<cmp1.max()<<" is the Maximum of two inteder numbers."<<endl;
cout<<cmp1.min()<<" is the Minimum of two inteder 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;
}