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 int>max_len = 40;
struct ch_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;
  }
  char>pop() {
    assert(top!= EMPTY);
    return s[top--];
  }
  char>top_of() {
    assert(top!= EMPTY);
    return s[top];
  }
  bool>empty()
   { return (top == EMPTY); }
  bool>full()
   { 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.