1 2 3 4 5 6 7 8 9 10 11

The new, more general, template class looks much the same, but with the addition of
template <class T>
which indicates that Stack is a template that won't be complete until the missing class Tis filled in. Tis a dummy name (a "template argument"), since any symbol will do. Notice that both the pushand the popoperations need to know about T.
template <class T>
void Stack<T> :: push(T x)
{
.....
}
Notwithstanding the <class T>notation, Stack does not have to be a class, any C++ type will do. For example,

Stack<int> Istack; Stack<double> Dstack; Stack<char> Cstack; Stack<Map*> Mstack;

// stack of integers // stack of doubles // stack of char values // stack of map pointers

And for the push operation we would need something like:

Istack.push(10); Dstack.push(3.4); Cstack.push('x');

// put a 10 onto a int stack // put 3.4 onto a double stack // put an 'x' on a char stack

Thus from this you can see that C++ is quite a big and powerful language, which should lead to smaller, easier to debug and maintain programs! You now have enough to handle Assignment 3, though I am sure that you will find Allen Supynuk's online notes most helpful, since there you will find some worked examples.

March 14, 1998

Page 4

C201/TAM