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

  1. // \EXAMPLES\EX15021.CPP
  2. //--------------------------------------------------------------
  3. // member functions of the template ASTACK
  4. //   The Microsoft Visual C++ compiler does not support templates.
  5. //   This program does not compile under Microsoft Visual C++.
  6. //--------------------------------------------------------------
  7.  
  8. //  files in this example:
  9. // %F,15,EX15021.H%EX15021.H       def'n of templates ASTACK and ASTACKITR
  10. // %F,15,EX15022.H%EX15022.H       definition of String class
  11. // %F,15,EX1502.CPP%EX1502.CPP      main() - exercise templates
  12. // EX15021.CPP     this file
  13.  
  14. //--------------------------------------------------------------
  15. #ifndef INCL_EX15021_CPP
  16. #define INCL_EX15021_CPP
  17.  
  18. //--------------------------------------------------------------
  19. #include <process.h>        // for abort()
  20. #include "EX15021.H"        // %F,15,EX17010B.H%Stack.H
  21.  
  22. //--------------------------------------------------------------
  23. // constructor
  24. //--------------------------------------------------------------
  25. template<class T>
  26. AStack<T>::AStack( unsigned long capacity)
  27.    : size (0), free(0), pArray( (T *)NULL)
  28. { pArray = new T[capacity];  // allocate array used to store data
  29.    if ( pArray == (T *)NULL)
  30.    { cout << "Memory allocation error: " << endl;
  31.       abort();   // aborts if memory error
  32.    }
  33.    size = free = capacity;   // initialize size and free;
  34. }
  35.  
  36.  
  37. //--------------------------------------------------------------
  38. // destructor
  39. //--------------------------------------------------------------
  40. template<class T>
  41. AStack<T>::~AStack()
  42. { delete[] pArray;  // deallocate the array
  43. }
  44.  
  45.  
  46. //--------------------------------------------------------------
  47. // function to push (add) item to stack
  48. //--------------------------------------------------------------
  49. template<class T> void
  50. AStack<T>::Push( const T y)
  51. {  // print message if stack full
  52.    if ( Full())
  53.       cout << "Stack Full: " << endl;
  54.    else
  55.    {
  56.      pArray[size -free] = y; // store data a next pos
  57.      free--;            // decrement the number of free elements
  58.    }
  59. }
  60.  
  61.  
  62. //--------------------------------------------------------------
  63. // function to pop (remove) item from stack
  64. //--------------------------------------------------------------
  65. template<class T> T
  66. AStack<T>::Pop()
  67. {  // terminate execution if stack empty
  68.    if ( Empty())
  69.    { cout << "Stack Empty:  " << endl;
  70.       abort();
  71.    }
  72.    // return top element and remove element from stack
  73.    return pArray[(size - free++) - 1];
  74. }
  75.  
  76. #endif
  77.