C++ class templates are used to write generic classes. A generic class defines a family of classes which are defined using the same code but which can be parameterized by different types, and by some sorts of constant parameters.

For example, here is a simple fixed-size stack class :-

template<class T, int SIZE>
class Stack {
public:
  Stack() : m_next(0) {}
  void Push(T& item) {m_stack[m_next++] = item; }
  T Pop() {return m_stack[--m_next]; }
private:
  T m_stack[SIZE];
  int m_next;
};

This class can be instantiated for any type T which has a copy constructor

Log in or register to write something here or to contact authors.