Advertisement
Phr0zen_Penguin

GenStack.h - VTC C++ Template Header/Implementation

May 3rd, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. /**
  2.  * [GenStack.h]
  3.  * The (generic) Stack template declaration/specification.
  4.  */
  5.  
  6. #ifndef GENSTACK_H
  7. #define GENSTACK_H
  8.  
  9. #include <iostream>
  10.  
  11. template <class ItemType>
  12. class GenStack
  13. {
  14.     /**
  15.      * Everything under public can be accessed by all other programs or classes.
  16.      */
  17.     public:
  18.         /**
  19.          * Constructor(s):
  20.          */
  21.         GenStack();
  22.  
  23.  
  24.         /**
  25.          * Mutators:
  26.          */
  27.         void Push(ItemType);
  28.  
  29.         /**
  30.          * Accessors:
  31.          */
  32.         ItemType Pop(void);
  33.  
  34.         /**
  35.          * General Use:
  36.          */
  37.  
  38.     /**
  39.      * Everything under protected can be accessed only by the class itself or (derived) subclasses,
  40.      * as in the case of inheritance.
  41.      */
  42.     protected:
  43.  
  44.  
  45.     /**
  46.      * (Private) DATA MEMBERS/PROPERTIES:
  47.      *
  48.      * Everything under private can only be accessed by the class itself (the code in the
  49.      * implementation file).
  50.      */
  51.     private:
  52.         int         top;
  53.         ItemType    data[100];
  54. };
  55.  
  56. #endif // GENSTACK_H
  57.  
  58.  
  59. /**
  60.  * The (generic) Stack template definition/implementation.
  61.  */
  62. template <class ItemType>
  63. GenStack<ItemType>::GenStack()                  // (default) constructor
  64. {
  65.     top = -1;
  66. }
  67.  
  68.  
  69. template <class ItemType>
  70. void GenStack<ItemType>::Push(ItemType item)    // mutator
  71. {
  72.     top++;
  73.     data[top] = item;
  74. }
  75.  
  76.  
  77. template <class ItemType>
  78. ItemType GenStack<ItemType>::Pop(void)          // accessor
  79. {
  80.     ItemType    item(data[top]);
  81.  
  82.     top--;
  83.  
  84.     return item;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement