home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 2-2B.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  665b  |  43 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. const int STACK_SIZE = 10;
  7.  
  8. class Stack {
  9. private:
  10.     long items[STACK_SIZE];
  11.     int sp;
  12. public:
  13.     void initialize();
  14.     long top() const;
  15.     long pop();
  16.     void push(long);
  17. };
  18.  
  19. void Stack::initialize() {
  20.  sp = -1;
  21. }
  22.  
  23. long Stack::top() const {
  24.     return items[sp];
  25. }
  26.  
  27. long Stack::pop() {
  28.     return items[sp--];
  29. }
  30.  
  31. void Stack::push(long i) {
  32.     items[++sp] = i;
  33. }
  34.  
  35. int main()
  36. {
  37.     Stack q;
  38.     q.initialize();
  39.     q.push(1);
  40.     int i = q.top();
  41.     q.pop();
  42. }
  43.