home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tmplt.zip / TMPLT0.CPP < prev    next >
Text File  |  1994-03-21  |  3KB  |  85 lines

  1. /*----------------------------------------------------------------------------*/
  2. /* tmplt0.cpp                                                                 */
  3. /*                                                                            */
  4. /* template example - create a stack template, push some letters onto it,     */
  5. /*                    then pop them off.                                      */
  6. /*                                                                            */
  7. /* (c) Larry Morley, 1994                                                     */
  8. /*----------------------------------------------------------------------------*/
  9.  
  10. #include <stdio.h>
  11.  
  12. int main(void);
  13.  
  14. /*----------------------------------------------------------------------------*/
  15. /* NOTE:                                                                      */
  16. /* If the member functions were not defined inline, they would have to be     */
  17. /* declared as, for example:                                                  */
  18. /*                                                                            */
  19. /*       template <class ATYPE> int Stack<ATYPE> :: push(ATYPE item)          */
  20. /*                                                                            */
  21. /*----------------------------------------------------------------------------*/
  22.  
  23. template<class ATYPE>
  24. class Stack
  25. {
  26.    private:
  27.  
  28.       ATYPE TheStack[100];  // use a fixed size for demonstration purposes
  29.       int   sp;
  30.  
  31.    public:
  32.  
  33.       /*-------------------------*/
  34.  
  35.       stack()
  36.       {
  37.          sp = -1;  // initialize the stack ptr
  38.       }
  39.  
  40.       /*-------------------------*/
  41.  
  42.       int push(ATYPE item)  // push an item onto the stack
  43.       {
  44.          if (sp < (sizeof(TheStack) / sizeof(ATYPE))- 1)
  45.          {
  46.             TheStack[++sp] = item;
  47.             return sp;
  48.          }
  49.          else
  50.             return 0;
  51.       }
  52.  
  53.       /*-------------------------*/
  54.  
  55.       ATYPE pop()  // pop an item off of the stack
  56.       {
  57.          if (sp)
  58.             return TheStack[sp--];
  59.          else
  60.             return (ATYPE) 0;
  61.       }
  62.  
  63.       /*-------------------------*/
  64.  
  65. };
  66.  
  67. /*----------------------------------------------------------------------------*/
  68.  
  69. int main()
  70. {
  71.    Stack<char> MyStack;
  72.  
  73.    MyStack.push('a');
  74.    MyStack.push('b');
  75.    MyStack.push('c');
  76.  
  77.    printf("%c, ", MyStack.pop());
  78.    printf("%c, ", MyStack.pop());
  79.    printf("%c.\n",MyStack.pop());
  80.  
  81.    return 0;
  82. }
  83.  
  84. /*----------------------------------------------------------------------------*/
  85.