OOPortal 





OO Encapsulation  «Prev  Next»

Adding a member function - Exercise

Objective:Add a member function to ch_stack that prints the contents of the stack.

Instructions

Below you will find the code for the struct ch_stack.
Add a print member function to this struct that will print out the contents of the stack.

The struct ch_stack


This code is also available in a file named ch_stack.cpp, which can be found in the compressed course download file available on the Resources page.


const intmax_len = 40;

structch_stack {
//data representation
   char s[max_len]; 
   int  top;
   enum{ EMPTY = -1, FULL = max_len - 1 };

//operations represented as member functions
   void  reset() { top = EMPTY; }
   void  push(char c) {
      assert(top != FULL);
      top++;
      s[top] = c;
   }
   charpop() {
      assert(top!= EMPTY);
      return s[top--];
   }
   chartop_of() {
      assert(top!= EMPTY);
      return s[top];
   }
   boolempty()
     { return (top == EMPTY); }
   boolfull()
     { return (top == FULL); }
};
Paste the source code for the member function below and click the Submit button when you are ready to submit this exercise.