Notice that the code in function main
of Fig. 18.2 is almost identical for both the doubleStack
manipulations in lines 9–32 and the intStack
manipulations in lines 34–56. This presents another opportunity to use a function template. Figure 18.3 defines function template testStack
(lines 10–39) to perform the same tasks as main
in Fig. 18.2—push
a series of values onto a Stack<T>
and pop
the values off a Stack<T>
.
1 // Fig. 18.3: fig18_03.cpp
2 // Passing a Stack template object
3 // to a function template.
4 #include <iostream>
5 #include <string>
6 #include "Stack.h" // Stack class template definition
7 using namespace std;
8
9 // function template to manipulate Stack< T >
10 template< typename T >
11 void testStack(
12 Stack< T > &theStack, // reference to Stack< T >
13 const T &value, // initial value to push
14 const T &increment, // increment for subsequent values
15 size_t size, // number of items to push
16 const string &stackName ) // name of the Stack< T > object
17 {
18 cout << "
Pushing elements onto " << stackName << '
';
19 T pushValue = value;
20
21 // push element onto Stack
22 for ( size_t i = 0; i < size; ++i )
23 {
24 theStack.push( pushValue ); // push element onto Stack
25 cout << pushValue << ' ';
26 pushValue += increment;
27 } // end while
28
29 cout << "
Popping elements from " << stackName << '
';
30
31 // pop elements from Stack
32 while ( !theStack.isEmpty() ) // loop while Stack is not empty
33 {
34 cout << theStack.top() << ' ';
35 theStack.pop(); // remove top element
36 } // end while
37
38 cout << "
Stack is empty. Cannot pop." << endl;
39 } // end function template testStack
40
41 int main()
42 {
43 Stack< double > doubleStack;
44 const size_t doubleStackSize = 5;
45 testStack( doubleStack, 1.1, 1.1, doubleStackSize, "doubleStack" );
46
47 Stack< int > intStack;
48 const size_t intStackSize = 10;
49 testStack( intStack, 1, 1, intStackSize, "intStack" );
50 } // end main
Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5
Popping elements from doubleStack
5.5 4.4 3.3 2.2 1.1
Stack is empty, cannot pop
Pushing elements onto intStack
1 2 3 4 5 6 7 8 9 10
Popping elements from intStack
10 9 8 7 6 5 4 3 2 1
Stack is empty, cannot pop
Function template testStack
uses T
(specified at line 10) to represent the data type stored in the Stack<T>
. The function template takes five arguments (lines 12–16):
• the Stack<T>
to manipulate
• a value of type T
that will be the first value push
ed onto the Stack<T>
• a value of type T
used to increment the values push
ed onto the Stack<T>
• the number of elements to push onto the Stack<T>
• a string
that represents the name of the Stack<T>
object for output purposes
Function main
(lines 41–50) instantiates an object of type Stack<double>
called doubleStack
(line 43) and an object of type Stack<int>
called intStack
(line 47) and uses these objects in lines 45 and 49. The compiler infers the type of T
for testStack
from the type used to instantiate the function’s first argument (i.e., the type used to instantiate doubleStack
or intStack
).