topical media & game development

talk show tell print

basic-program-solutions-07-Soln7-7.c

? / basic-program-solutions-07-Soln7-7.c


  // Soln7_7.cpp
  
  include <iostream>                   // For stream input/output
  
  using std::cout;
  using std::endl;
  
  class CStack
  {
  public:
     CStack() : next(0) {}
     void push(int i);
     int pop();
     void print();
  private:
     int list[100];
     int next;
  };
  
  void CStack::push(int i)
  {
     if (next < 99)
        list[next++] = i;
  }
  
  int CStack::pop()
  {
     return list[--next];
  }
  
  void CStack::print()
  {
     cout << '[';
     for(int i=next-1 ; i>=0 ; i--)
        cout << ' '<< list[i];
     cout << " ]\n";
  }
  
  int main()
  {
     CStack s;
  
     s.print();
  
     s.push(5);
     s.push(10);
     s.push(8);
  
     s.print();
  
     cout << "top of stack=" << s.pop() << endl;
  
     s.print();
  
     return 0;
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.