Activity 14-2 - Templates for Classes

The syntax for class templates is basically the same as that for function templates:

template<class T>

This statement will be placed before the template definition.  The type parameter T is used in the class definition just like any other type.  Here is an example:

// Class for a pair of values of type T:
template<class T>
class Pair
        Pair( );
        Pair(T first_value, T second_value);
        void set_element(int position, T value);

        T_get_element(int position) const;

        T first;
        T second;'

An object of this class contains a pair of values of type T.  Once the class template is defined, we can declare objects of this class.  The declaration must specify what type is to be filled in for T.   For example,

Pair<int>  score;  // declares the object score to record a pair of integers
pair<char> seat; // declares the object seat to record a pair of characters

The two objects can be used like any other objects.  For example,

score.set_element(1, 3);  // Sets the score for the first team to 3
score.set_element(2, 0);  // Sets the score for the second team to 0

As you can see in the above class member function definitions are also templates.

Here are the definitions for the member functions set_element and the constructor Pair with two arguments.

// Use iostream and cstdlib
template<class T>
void Pair<T>::set_element(int position, T value)
        if(position == 1)
            first = value;
        else if(position == 2)
            second = value;
            cout << "Error: Illegal pair position. \n";

template<class T>
Pair<T>::Pair(T first_value, T second_value) : first(first_value), second(second_value)
    // empty body

Important note:  The class name before the scope resolution operator is Pair<T>, not simply Pair.

The name of a class template may be used as the type for a function parameter:

For example,
int add_up(const Pair<int>& the_pair);

returns the sum of the two integers in the_pair.  You can use a class template within a function template.

template<class T>
T add_up(const Pair<T>& the_pair);

Type Definitions
You can specialize a class template by giving a type argument to the class name, as in the following example:


The specialized class name, like Pair<int>, can then be used just like any class name.  It can be used to declare other objects or to specify the type of a formal parameter. You can also define a new class that has the same meaning  as a specialized class template name, such as Pair<int>.  The syntax for such a defined type name is:

typedef Class_Name<Type_Argument>  New_Type_Name;

The type name PairOfInt can now be used to declare objects of type Pair<int>:

PairOfInt pair1, pair2;

The type name PairOfInt can also be used to specify the type of a formal parameter.

Exercise 14.2
Write the definitions for the member functions get_element for the class template Pair and the constructor Pair( ) with no argument.  Add to the class a member function called display_pairs that displays the pairs.  Then write a complete program that uses the class to read a pair of integers and a pair of strings and assign them to the appropriate objects, grades and names. Here is an example of an input pair:
first pairs:
87  90
second pairs:
Tom  Martha