Now, let’s consider the driver (Fig. 18.2) that exercises the Stack
class template. The driver begins by instantiating object doubleStack
(line 9). This object is declared as a Stack<double>
(pronounced “Stack
of double
”). The compiler associates type double
with type parameter T
in the class template to produce the source code for a Stack
class with elements of type double
that actually stores its elements in a deque<double>
.
1 // Fig. 18.2: fig18_02.cpp
2 // Stack class template test program.
3 #include <iostream>
4 #include "Stack.h" // Stack class template definition
5 using namespace std;
6
7 int main()
8 {
9 Stack< double > doubleStack; // create a Stack of double
10 const size_t doubleStackSize = 5; // stack size
11 double doubleValue = 1.1; // first value to push
12
13 cout << "Pushing elements onto doubleStack
";
14
15 // push 5 doubles onto doubleStack
16 for ( size_t i = 0; i < doubleStackSize; ++i )
17 {
18 doubleStack.push( doubleValue );
19 cout << doubleValue << ' ';
20 doubleValue += 1.1;
21 } // end while
22
23 cout << "
Popping elements from doubleStack
";
24
25 // pop elements from doubleStack
26 while ( !doubleStack.isEmpty() ) // loop while Stack is not empty
27 {
28 cout << doubleStack.top() << ' '; // display top element
29 doubleStack.pop(); // remove top element
30 } // end while
31
32 cout << "
Stack is empty, cannot pop.
";
33
34 Stack< int > intStack; // create a Stack of int
35 const size_t intStackSize = 10; // stack size
36 int intValue = 1; // first value to push
37
38 cout << "
Pushing elements onto intStack
";
39
40 // push 10 integers onto intStack
41 for ( size_t i = 0; i < intStackSize; ++i )
42 {
43 intStack.push( intValue );
44 cout << intValue++ << ' ';
45 } // end while
46
47 cout << "
Popping elements from intStack
";
48
49 // pop elements from intStack
50 while ( !intStack.isEmpty() ) // loop while Stack is not empty
51 {
52 cout << intStack.top() << ' '; // display top element
53 intStack.pop(); // remove top element
54 } // end while
55
56 cout << "
Stack is empty, cannot pop." << endl;
57 } // 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
Lines 16–21 invoke push
(line 18) to place the double
values 1.1, 2.2, 3.3, 4.4 and 5.5 onto doubleStack
. Next, lines 26–30 invoke top
and pop
in a while
loop to remove the five values from the stack. Notice in the output of Fig. 18.2, that the values do pop
off in last-in, first-out order. When doubleStack
is empty, the pop
loop terminates.
Line 34 instantiates int
stack intStack
with the declaration
Stack< int > intStack;
(pronounced “intStack
is a Stack
of int
”). Lines 41–45 repeatedly invoke push
(line 43) to place values onto intStack
, then lines 50–54 repeatedly invoke top
and pop
to remove values from intStack
until it’s empty. Once again, notice in the output that the values pop
off in last-in, first-out order.
18.118.140.204