C++ Class template Create Several Versions of a Class
#include <iostream> using namespace std; template <typename T> class MyClass/*www.j a v a 2s . co m*/ { public: T first; T second; T third; T sum() { return first + second + third; } }; int main() { MyClass<int> IntHolder; IntHolder.first = 10; IntHolder.second = 20; IntHolder.third = 30; MyClass<int> my2; my2.first = 100; my2.second = 200; my2.third = 300; MyClass<float> FloatHolder; FloatHolder.first = 3.1415; FloatHolder.second = 4.1415; FloatHolder.third = 5.1415; cout << IntHolder.first << endl; cout << my2.first << endl; cout << FloatHolder.first << endl; MyClass<int> *hold; for (int loop = 0; loop < 10; loop++) { hold = new MyClass<int>; hold->first = loop * 100; hold->second = loop * 110; hold->third = loop * 120; cout << hold->sum() << endl; delete hold; } return 0; }