//函数模板的定义:
int abs(int n){
returnn n<0?-n:n;
}
double abs(double n){
return n<0?-n:n;
}
//**********************************************
template <class t>t abs(t n){
return n<0?-N:N;
}
//**********************************************
template <class type1,class type2>typp1 max(tpye1 a,type b){
return type1(a>b?a:b);
}
//**********************************************
class指代的是调入函数时传递的任何数据类型。
//**********************************************
//类模板的定义
class IntList{
public:
int SetItem(int index,const int &Item);
int GetItem(int index,int &Item);
private:
int Buffer[100];
};
//define a class template
template<class t,int i>class IntList{
public:
int SetItem(int index,const t &Item);
int GetItem(int index,t &Item);
private:
t Buffer[100];
}
//
template <class t,int i>int CList<t,i>::SetItem(int index,const t &Item)
{
if(index<0||index>i-1)
return 0;
Buffer[index]=Item;
return 1;
}
//
//声称类的实例
//CList(int,100)IntList;
/*
模板是特殊类型的函数或定义,可以生成整组相关函数或类,适应特定的数据类型。
函数模板定义接受各种不同类型参数的一般函数,编译器遇到函数调用时,自动生成适用于所传入的数据类型的函数版本。
定义了一个函数模板方便取代了一组重组函数的定义.
在函数模板的定义,类型蚕食 表示函数调用传递的数据项类型。
*/