Class Templates, Midterm 2 Review

Tom Kelliher, CS23

Apr. 16, 1997

Class Templates

Class Templates:

A mechanism which allows the designer to add a parameter to a class definition. This parameter is ordinarily used to allow the creation of, for example, list of any type from a single list class.

Example Class Declaration

template <class T>
class Array
{
 private:
   T *data;    // Pointer to the array.
   int size;   // Size of the array.

 public:
   Array(int s);
   ~Array() { delete [] data; }
   int Set(const T val, int pos);
   int Get(T &val, int pos);
   int Size(void) { return size; }   // Hopefully obvious.
};

Example Class Definition

/**********************************************************************
 * Array<T>::Array(int s)
 *
 * Create an array of type T of size s.  Terminate if creation not
 * possible.
 **********************************************************************/

template <class T>
Array<T>::Array(int s) : size(s)
{
   data = new T[s];
   assert(data != NULL);
}


/**********************************************************************
 * Array<T>::Set(const T val, int pos)
 *
 * Set the pos'th element of the array to val.
 * Return 1 if pos is valid, otherwise return 0.
 **********************************************************************/

template <class T>
int Array<T>::Set(const T val, int pos)
{
   if (pos < 0 || pos >= size)
      return 0;

   data[pos] = val;
   return 1;
}


/**********************************************************************
 * Array<T>::Get(const T val, int pos)
 *
 * Store the value of the pos'th element of the array in val.
 * Return 1 if pos is valid, otherwise return 0.
 **********************************************************************/

template <class T>
int Array<T>::Get(T &val, int pos)
{
   if (pos < 0 || pos >= size)
      return 0;

   val = data[pos];
   return 1;
}

Example Program

/**********************************************************************
 * main.cc
 * A small program to demonstrate the use of a template-based array
 * class.
 **********************************************************************/


#include <iostream.h>

#pragma implementation
#include "array.h"


/**********************************************************************
 * main()
 **********************************************************************/

int main()
{
   Array<int> a(5);     // array of 5 ints.
   int i;
   Array<char*> b(2);   // array of 2 char*'s.
   char *s;

   a.Set(12, 0);
   a.Set(35, 1);

   a.Get(i, 0);
   cout << i << endl;
   a.Get(i, 1);
   cout << i << endl;
   cout << "Size: " << a.Size() << endl;

   cout << "\n==========\n\n";

   b.Set("Hello", 0);
   b.Set("World", 1);

   b.Get(s, 0);
   cout << s << endl;
   b.Get(s, 1);
   cout << s << endl;
   cout << "Size: " << b.Size() << endl;

   return 0;
}

Implementation Details

  1. Definitions provided in same file as declarations.

  2. #pragma interface

  3. #pragma implementation

Midterm 2 Review

  1. Responsible for Chapters 6, 7, and 8.

  2. Not responsible for Class Templates and Overloaded Operators.

  3. Solve three of four problems (two and one of two).

  4. One small programming problem.

  5. The other problems may ask you to write small bits of code

  6. The other problems may ask you to interpret code. (E.g., the polymorphism example from last time.)



Thomas P. Kelliher
Tue Apr 15 20:11:48 EDT 1997
Tom Kelliher