Creating a Template Class - C++ template

C++ examples for template:template class

Description

Creating a Template Class

Demo Code

#include <iostream>

using namespace std;

template <typename T, int numberOfElements>
class MyArray//from   w  w w . ja  v a  2s .  c  o m
{
private:
    T m_Array[numberOfElements];

public:
    MyArray(): m_Array{}{

    }

    T& operator[](const unsigned int index){
        return m_Array[index];
    }
};

int main(int argc, char* argv[])
{
    const unsigned int ARRAY_SIZE{ 5 };
    MyArray<int, ARRAY_SIZE> myIntArray;
    for (unsigned int i{ 0 }; i < ARRAY_SIZE; ++i)
    {
        myIntArray[i] = i;
    }

    for (unsigned int i{ 0 }; i < ARRAY_SIZE; ++i)
    {
        cout << myIntArray[i] << endl;
    }

    cout << endl;

    MyArray<float, ARRAY_SIZE> myFloatArray;
    for (unsigned int i{ 0 }; i < ARRAY_SIZE; ++i)
    {
        myFloatArray[i] = static_cast<float>(i)+0.5f;
    }
    for (unsigned int i{ 0 }; i < ARRAY_SIZE; ++i)
    {
        cout << myFloatArray[i] << endl;
    }

    return 0;
}

Result


Related Tutorials