CSDN博客

img _CPU

using template in class

发表于2004/9/30 11:31:00  1166人阅读

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

阅读全文
0 0

相关文章推荐

img
取 消
img