home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX17010.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  4.4 KB  |  144 lines

  1. // \EXAMPLES\EX17010.H      - Stack.H
  2.  
  3. //----------------------------------------------------------
  4. // Example of template container classes implenenting stacks.
  5. // This version includes exception handling and templates.
  6. //----------------------------------------------------------
  7. //----------------------------------------------------------
  8. //  Exception handling is supported only by
  9. //                      the IBM C++ Set/2 compiler
  10. //  Templates are not supported by
  11. //                      the Microcoft Visual C++ compiler
  12. //  Two versions of this program are included:
  13. //                      EX1701I.EXE - for IBM CSet II
  14. //                      EX1701B.EXE - for Borland Turbo C++
  15. //----------------------------------------------------------
  16. // Files in this example:
  17. // EX17010.H       this file -- Stack.H       base class stack
  18. // %F,15,EX17011.H%EX17011.H       AStack.H      derived array stack
  19. // %F,15,EX17011.CPP%EX17011.CPP     AStack.CPP
  20. // %F,15,EX17012.H%EX17012.H       LLStack.H     derived linked list stack
  21. // %F,15,EX17012.CPP%EX17012.CPP     LLStack.CPP
  22. // %F,15,EX17013.H%EX17013.H       Exception.H
  23. // %F,15,EX17014.H%EX17014.H       Iterator.H       iterator class
  24. // %F,15,EX1701.CPP%EX1701.CPP      main() with exception handling
  25. // %F,15,EX1701B.CPP%EX1701B.CPP     main() without exception handling
  26. //----------------------------------------------------------
  27. #ifndef INCL_EX17010_H
  28. #define INCL_EX17010_H
  29.  
  30. #include "EX17013.H"        // %F,15,EX17013.H%Exception.H
  31.  
  32. //---------------------------------------------------------
  33. // TEMPLATE: class Stack: to create a base class for a stack
  34. //                        which stores elements of type T
  35. //
  36. // COMMENTS: Notice all functions are pure virtuals
  37. //---------------------------------------------------------
  38. template<class T> class Stack
  39. {
  40. public:
  41.  
  42.    //------------------------------------------------------
  43.    // FUNCTION: Push pushes an element of type T onto the
  44.    //                stack
  45.    // PARAMETERS: T y is an element of type T
  46.    // RETURN VALUE: none
  47.    // EXCEPTIONS: throws an int if full
  48.    //------------------------------------------------------
  49.    virtual void
  50.    Push( const T y) = 0;
  51.  
  52.    //------------------------------------------------------
  53.    // FUNCTION: Pop pops an element of type T from the stack
  54.    // PARAMETERS: none
  55.    // RETURN VALUE: returns the top element of the stack
  56.    // EXCEPTIONS: throws an int if empty
  57.    //------------------------------------------------------
  58.    virtual T
  59.    Pop() = 0;
  60.  
  61.    //------------------------------------------------------
  62.    // FUNCTION: Full returns true if stack is full, false
  63.    //                otherwise
  64.    // PARAMETERS: none
  65.    // RETURN VALUE: int
  66.    //------------------------------------------------------
  67.    virtual int
  68.    Full() const = 0;
  69.  
  70.    //------------------------------------------------------
  71.    // FUNCTION: Empty returns true if stack is empty, false
  72.    //                 otherwise
  73.    // PARAMETERS: none
  74.    //
  75.    // RETURN VALUE: int
  76.    //------------------------------------------------------
  77.    virtual int
  78.    Empty() const = 0;
  79.  
  80.    //------------------------------------------------------
  81.    // FUNCTION: OnTop returns the top element of the stack
  82.    // PARAMETERS: none
  83.    // RETURN VALUE: T the top element of the stack
  84.    // EXCEPTIONS: throws an int if empty
  85.    //------------------------------------------------------
  86.    virtual T
  87.    OnTop() const = 0;
  88.  
  89.    //------------------------------------------------------
  90.    // FUNCTION: NumElem returns the number of elements in stack
  91.    // PARAMETERS: none
  92.    // RETURN VALUE: long the number of elements in the stack
  93.    //------------------------------------------------------
  94.    virtual long
  95.    NumElem() const = 0;
  96.  
  97.    class FullExcept : public Exception
  98.    {
  99.       char*
  100.       Name() const
  101.       {
  102.          return "Full Exception";
  103.       }
  104.  
  105.       long
  106.       ErrorNum() const
  107.       {
  108.          return 0x1;
  109.       }
  110.    };
  111.  
  112.    class EmptyExcept : public Exception
  113.    {
  114.       char*
  115.       Name() const
  116.       {
  117.          return "Full Exception";
  118.       }
  119.  
  120.       long
  121.       ErrorNum() const
  122.       {
  123.          return 0x1;
  124.       }
  125.    };
  126.  
  127.    class MemAlloc : public Exception
  128.    {
  129.       char*
  130.       Name() const
  131.       {
  132.          return "Full Exception";
  133.       }
  134.  
  135.       long
  136.       ErrorNum() const
  137.       {
  138.          return 0x1;
  139.       }
  140.    };
  141. };
  142.  
  143. #endif
  144.