Function Templates

A function template is a single function that can perform its operations with multiple data types

Function template definitions begin with:

template <class T>

The formal parameter(s) can be used to:

template <class T>
void printArray(T array[], const int count)
   {
   for (int k = 0; k < count; k++)
       cout << array[k] << " ";

   cout << endl;
   }

The printArray function has a single formal parameter called T, which is used to specify the data type of the array that will be printed.

When the function is invoked:

Assuming that ar is an array of integers, then

PrintArray(ar, 5);

would produce:

void printArray(int array[], const int count)
   {
   for (int k = 0; k < count; k++)
       cout << array[k] << " ";

   cout << endl;
   }

NOTES:

EXAMPLE:

template <class T>
void printArray(T [], const int);


int main()
   {
   int a[5] = {1, 2, 3, 4, 5};
   float b[7] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
   char c[6] = "HELLO";

   cout << "Array a contains: ";
   printArray(a, 5);

   cout << "Array b contains: ";
   printArray(b, 7);

   cout << "Array c contains: ";
   printArray(c, 6);

   return 0;
   }


template <class T>
void printArray(T array[], const int count)
  {
  for (int k = 0; k < count; k++)
     cout << array[k] << "  ";

  cout << endl;
  }


Array a contains: 1  2  3  4  5
Array b contains: 1.1  2.2  3.3  4.4  5.5  6.6  7.7
Array c contains: H  E  L  L  O

 

Class Templates

Function templates allowed us to create generic functions, while class templates allow us to create generic classes

Class template definitions begin with:

template <class T>

Throughout the class and method definitions, the generic formal parameter name(s) are used

template <class T>
class Node
   {
   public:
   friend LinkedList<T>;
   Node (T = 0, Node<T> * = NULL);   //constructor
   ~Node();                       //destructor
   ...                            // any other methods needed

   private:
   T data;                        //information
   Node<T> *link;                 //location of next node in the list
   };

Each method definition is preceded by:

Other changes to method definitions:

The class name is followed by: <formal_paramter_list>

template <class T>
Node<T>::Node(T newData, Node<T> *nextNode)
   {
   data = newData;
   link = nextNode;
   }

The generic formal parameter is used in the method header and within the method code. It is also used on a class that refers to another class which is a template.

template <class T>
class LinkedList
   {
   public:
   LinkedList();          //constructor
   ~LinkedList();         //destructor
   void reverse();        //reverses the nodes in a list
   ...                    // any other methods needed

   private:
   Node<T> *reverse(Node<T> *, Node<T> *);   //reverses the nodes in a list
   Node<T> *headPtr;               //location of next node in the list
   };


void LinkedList<T>::reverse()          Node<T> * LinkedList<T>::reverse(Node<T> *p, Node<T> *q)
  {                                      {
  headPtr = reverse(headPtr, NULL);      if (p == NULL)
  }                                        return q;
                                         else
                                           {
                                           Node<T> *save = p->link;  
                                           p->link = q;
                                           return reverse(save, p);
                                           }
                                         }

 

To instantiate an object of the class:

class_name<data_type> object_name;

EXAMPLE:

int main()
   {
   LinkedList<float> floatList;

   *** add nodes to the list ***
   
   floatList.reverse();
   return 0;
   }

NOTES: