previous | start | next

## Templates

- A class template is a mechanism that allows us to create classes whose data fields have arbitrary type.
- We have already seen an example: the vector template.

  >     vector<int> v_i;
  >     vector<double> v_d;
  >     vector<Employee> v_e;

- To define a template, denote the arbitrary type with a type parameter as show in syntax 17.3 (next slide), and add the line `template<typename T>` before the class definition.

  >     template<typename T>
  >     class Pair
  >     {
  >     public:
  >        Pair(T a, T b);
  >        T get_first() const;
  >        T get_second() const;
  >     private:
  >        T first;
  >        T second;
  >     };

previous | start | next