您的位置:首页 > 编程语言 > C语言/C++

C++ template funcitons and classes

2009-02-20 00:02 295 查看
#include
//----------------------
//template function
//----------------------
template //this line is required
T power(T base,int e) //you can view T as int or double or sth here.T is an return type
{
T result = base;
if(e == 0) return(T)1;
if(e < 0) return(T)0;
while(--e) result*=base;
return result;
}
//-----------------------
//template class
//-----------------------
template //this line is required
class CThree
{
public:
CThree(T t1,T t2,T t3);
T Min(); //you can view T as int or double or sth here.T is an return type
T Max(); //you can view T as int or double or sth here.T is an return type
private:
T a,b,c; //you can view T as int or double or sth here.T is an return type
};
template //this line is required
T CThree::Min() //T is required here
{
T minab = a < b ?a:b;
return minab //this line is required
T CThree::Max() //T is required here
{
T maxab=a //this line is required
CThree::CThree(T t1,T t2,T t3):
a(t1),b(t2),c(t3)
{
return;
}
int main()
{
int i=power(5,4);
cout< obj1(2,5,4);
cout< obj2((float)8.52,(float)-6.75,(float)4.54);
cout< obj3(646600L,437847L,364873L);
cout<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: