您的位置:首页 > 其它

模板类 例子

2015-09-02 18:13 357 查看
TemplateDemo.h

#ifndef TEMPLATE_DEMO_HXX
#define TEMPLATE_DEMO_HXX

template<class Type> class Math{
public:
Math();
Type Add(Type a, Type b);
};

#endif


TemplateDemo.cpp

#include "TemplateDemo.h"

template<class Type> Math<Type>::Math(){};
template<class Type>Type Math<Type>::Add(Type a, Type b)
{
return a + b;
}

int main()
{
Math<int> demo1;
cout << demo1.Add(5, 5) << endl;

retrun 0;
}


.h文件中类名前的 template< class Type>几乎是固定的,

.cpp文件中也是,不过在类名后又多了< Type>.仔细看看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: