/***********
函数模板
************/
#include <iostream.h>
template <class T> //用实际数据类型替换类属参数T
T ssub (T a, T b)
{
return a-b;
}
int issub (int a, int b)
{
return a-b;
}
float fssub (float a, float b)
{
return a-b;
}
void main()
{
cout<<"issub(2,3)="<<issub(2,3)<<endl;
cout<<"fssub(2.5,3.3)="<<fssub(2.5,3.3)<<endl;
cout<<"ssub(2,3)="<<ssub(2,3)<<endl;
cout<<"ssub(2.5,3.3)="<<ssub(2.5,3.3)<<endl;
}
/***********
类模板
************/
#include <iostream.h>
template <class T>
class A
{
private:
T x;
T y;
public:
A(T a, T b)
{
x=a;y=b;
}
void display()
{
cout<<x<<"+"<<y<<"i"<<endl;
}
};
void main()
{
A<int>f1(2,4); //用int替换类属参数T
A<float>f2(3.2,5.4); //用float替换类属参数T
f1.display();
f2.display();
}