Using Templates to Create Several Versions of a Class - C++ template

C++ examples for template:template class

Description

Using Templates to Create Several Versions of a Class

Demo Code

#include <iostream>

using namespace std;

template <typename T>
class MyClass//ww  w.ja  va  2  s . c o  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> AnotherIntHolder;
    AnotherIntHolder.first = 100;
    AnotherIntHolder.second = 200;
    AnotherIntHolder.third = 300;

    MyClass<float> FloatHolder;
    FloatHolder.first = 3.1415;
    FloatHolder.second = 4.1415;
    FloatHolder.third = 5.1415;

    cout << IntHolder.first << endl;
    cout << AnotherIntHolder.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;
}

Result


Related Tutorials