A function template is a single function that can perform its operations with multiple data types
Function template definitions begin with:
keyword template
list of formal parameters to the function template (enclosed in angle brackets < > and preceded by the keyword class)
template <class T>
The formal parameter(s) can be used to:
specify the data type(s) of the argument(s) passed to the function
specify the return type of the function
declare variables within the function
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:
the type of printArray's first argument is substituted for T throughout the template definition
a complete function template for printing an array of a specified data type is created
the new function is compiled
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:
Every formal parameter in the template definition must appear in the function's parameter list at least once
The name of a formal parameter can be used only once in the formal parameter list of the template definition
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
Function templates allowed us to create generic functions, while class templates allow us to create generic classes
Class template definitions begin with:
keyword template
list of formal parameters to the function template (enclosed in angle brackets < > and preceded by the keyword class)
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:
keyword template
list of formal parameters to the function template (enclosed in angle brackets < > and preceded by the keyword class)
which is the same as the one that precedes the class definition
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:
May have more than one formal parameter
Each must be preceded by the keyword class
Each separated by a comma