OO Encapsulation  «Prev 

Testing private and public Operations

Here is a program that will test the operations of the struct ch_stack:

#include <iostream>
//Reverse a string with a ch_stack.

int main()
 ch_stack  s;
 char  str[max_len] = { "My name is Don Knuth!"};
 int  i = 0;
 cout<< str << endl;
 s.reset(); //s.top = EMPTY;would be illegal
 while (str[i])
   if (!s.full())
 while (!s.empty())   //print the reverse
   cout << s.pop();
   cout << endl;
 return 0;

The output from this test program is:
My name is Don Knuth!
!htunK noD si eman yM

As the comment in main() states, access to the hidden variable top is controlled.
It can be changed by the member function reset(), but cannot be accessed directly. Also, notice how the variable s is passed to each member function using the structure member operator form.