Today I studied using template in a class.The following is the code:
1.Header File: template.h:
#include <string>
using namespace std;
template <class T>class Cal
{
public:
Cal(){}
T Calculate(T&,T&);
};
2.CPP File: template.cpp
#include <iostream.h>
#include <stdio.h>
#include <string>
#include "template.h"
using namespace std;
template <class T>T Cal<T>::Calculate(T &t1,T &t2)
{
return t1+t2;
}
main()
{
Cal<int> *c=new Cal<int>();
int a=100;
int b=200;
cout<<c->Calculate(a,b)<<endl;
return 0;
}
最后执行结果如下图:
博主分享了使用C++类模板的学习内容,给出了头文件template.h和源文件template.cpp的代码,展示了类模板的定义和使用,最后给出了执行代码的结果。
4万+

被折叠的 条评论
为什么被折叠?



