simpleTemplate

This is a simple example to use template class simpleTemplate. Basically, there are two ways to use it.

(1) Same file
The first and simpler way is to declare and define template class in a single file. For example, main.cpp declares, implements, and instantiates a template class simpleTemplate within the same file.

(2) Separate files
The second and practical way is to use separate files to declare, define, and instantiate the template class. This is trickier than the first option because of link error.

main.cpp

#include <iostream>

using namespace std;

template <typename E>
class simpleTemplate
{
public:
simpleTemplate();
~simpleTemplate();

void memberFn1();

private:
int memberVar1;
};

template <typename E>
simpleTemplate<E>::simpleTemplate()
{
cout << "Constructor is called." << endl;
}

template <typename E>
simpleTemplate<E>::~simpleTemplate()
{
cout << "Destructor is called." << endl;
}

template <typename E>
void
simpleTemplate<E>::memberFn1()
{
cout << "memberFn1 is called." << endl;
}

int main(int argc, char** argv)
{
cout << "Hellow, world" << endl;

simpleTemplate<int> simpleTemplateObj;
simpleTemplateObj.memberFn1();

return 0;
}