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;
}
最后执行结果如下图: