using template in class

来源:互联网 发布:excel求竖排数据平均数 编辑:程序博客网 时间:2024/06/10 01:51

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

原创粉丝点击