previous | start | next

Template class Member Implementations

The member functions, constructors, destructors of a template class should be implemented in the .h file rather than a .cpp file.



#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>

using namespace std;

template <class T>
class Array
{
  ...
};

// Add all the member function implementations here!
// after the class declaration.

template <class T>
Array<T>::Array()
{
  arr_size = 0;
  arr_capacity = 10;
  arr = new T[arr_capacity];
}
...

template <class T>
void Array<T>::add(const T& x)
{
  if ( arr_size == arr_capacity ) {
     grow(2*arr_capacity);
  }
  arr[arr_size] = x;
  arr_size++;
}

#endif


previous | start | next