CMPT 225 Lab Exercise 5: C++ Template Classes


Creating a Template

C++ allows a programmer to create templates for functions and classes. These are definitions which can be instantiated, at compile time, with different types in the variable and function definitions. Using templates allows a programmer to create one definition of a class, which can then be used to create different instances which store or use data of different types. For example, you can create a template stack class, which can be instantiated as a stack of ints, a stack of strings, or even a stack of stacks. If you've been reading the textbook, you have probably seen them being used several times. The C++ Standard Template Library is a collection of templates, including template classes for many of the ADTs studied in this course. It is described briefly on page 45 of the course text.

In this exercise you will convert a seqence class, which stores a sequence of int values (and is implemented with a simple linked list), to a template sequence class which can be instantiated to instances of different types. Thus, with the class, you can create sequences of ints, sequences of doubles, etc. You will then write a small program to demonstrate the class.

Begin by downloading the files Seq.h, Seq.cpp, template_test.cpp, and Makefile. (Note that the makefile won't work as is.) You can compile Seq.cpp and template_test.cpp separately (using g++ -c) to create Seq.o and template_test.o, and then link them (with g++ -o test Seq.o template_test.o) to create an executable test. You can also add lines to template_test.cpp, to demonstrate further the existing Seq class.

Your task is to convert the Seq class to a template class, and then make a version of template_test.cpp that demonstrates that it works.

Convert the class to a template class

To do this, you must:
  1. Put the entire class implementation into the Seq.h file. (You need to move the implementations from Seq.cpp to Seq.h);
  2. Change the class definition and each function definition to a template, by prefacing each with the line template <typename T>.
  3. Re-write the class qualifier (Seq::) that appears before the function name in each function implementation as Seq<T>::
  4. Replace all occurrences of int in type declarations for variables which are sequence elements to T. (There might be some int variable which are not sequence elements, so should not be changed.)
For example here is the add method before and after these changes:
void Seq::add(int x){
    Node *p = new Node; //temporary node
    // Assign appropriate values to the new node
    p -> data = x;
    p -> next = first;
    // Make first point to the new node
    first = p;	
}
and the "templated" version
template <class Type>
void Seq<T>::add(T x){
    Node *p = new Node; //temporary node
    // Assign appropriate values to the new node
    p -> data = x;
    p -> next = first;
    // Make first point to the new node
    first = p;	
}

The Test Program

To use the Seq template class in a program, when you declare a variable of type Seq, you need specify what type it is to be, for example:

Modify template_test.cpp to use the Seq template class to make an int sequence, a string sequence, and one other type of sequence. The Makefile should work for compiling the version of template_test which uses the template class. Use output (with cout and the display function) to clearly demonstrate that the class works. The program should use each of the main Seq functions (add, insertAt, remove) at least once on each type, and use the display function (along with other output to the screen to make clear what is being shown) as many times as needed to demonstrate that the functions work. Do not make your test program more complex than is needed to accomplish this, though.

When you have done this, submit a .zip file containing your Seq.h, Makefile and template_test.cpp files online. Submit your solution no later than November 24.