fork download
  1. //Purpose: Header file for StackType. Containing all declerations and prototypes
  2. #include <stdexcept>
  3.  
  4. struct StackException : virtual std::exception
  5. {
  6. protected: StackException() {}
  7. };
  8. struct StackFullException : StackException
  9. {
  10. char const* what() const throw() { return "StackFullException"; }
  11. };
  12. struct StackEmptyException : StackException
  13. {
  14. char const* what() const throw() { return "StackEmptyException"; }
  15. };
  16.  
  17.  
  18. template <class itemType>
  19. class StackType
  20. {
  21. public:
  22. StackType (int max);
  23. StackType ();
  24. bool IsEmpty() const;
  25. bool IsFull () const;
  26. void Push (itemType newItem);
  27. void Pop ();
  28. itemType Top() const;
  29. ~StackType (); // Destructor
  30.  
  31. private:
  32. int top; // key:top of the stack
  33. int maxStack; // max number of stack items
  34. itemType* list; // pointer to dynamically allocated memory
  35. };
  36.  
  37. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  38. /*Implementation (StackStype.cpp)
  39. StackType prototype functions*/
  40.  
  41. template <class itemType>
  42. StackType<itemType>::StackType(int max)
  43. {
  44. maxStack = max;
  45. top = -1;
  46. list = new itemType[maxStack];
  47. }
  48.  
  49. template <class itemType>
  50. StackType<itemType>::StackType()
  51. {
  52. maxStack = 200;
  53. top = -1;
  54. list = new itemType[maxStack];
  55. }
  56.  
  57. template <class itemType>
  58. bool StackType<itemType>::IsEmpty() const
  59. {
  60. return(top == -1);
  61. }
  62.  
  63. template <class itemType>
  64. bool StackType<itemType>::IsFull() const
  65. {
  66. return(top == maxStack - 1);
  67. }
  68.  
  69. template <class itemType>
  70. void StackType<itemType>::Push(itemType newItem)
  71. {
  72. if(IsFull())
  73. {
  74. throw StackFullException();
  75. }
  76. top++;
  77. list[top] = newItem;
  78. }
  79.  
  80. template <class itemType>
  81. void StackType<itemType>::Pop()
  82. {
  83. if(IsEmpty())
  84. {
  85. throw StackEmptyException();
  86. }
  87. top--;
  88. }
  89.  
  90. template <class itemType>
  91. itemType StackType<itemType>::Top() const
  92. {
  93. if(IsEmpty())
  94. {
  95. throw StackEmptyException();
  96. }
  97. return list[top];
  98. }
  99.  
  100. template <class itemType>
  101. StackType<itemType>::~StackType()
  102. {
  103. delete [] list;
  104. }
  105.  
  106. ///////////////////////////////////////
  107. // sample main.cpp
  108.  
  109. #include <iostream>
  110. int main(void)
  111. {
  112. try
  113. {
  114. StackType<int> stack(5);
  115. stack.Push(5);
  116. stack.Push(2);
  117. stack.Push(3);
  118. stack.Push(4);
  119. stack.Push(1);//<-----Still Ok!
  120. stack.Push(0);//<-----throw FullStack
  121. } catch (const StackException& e)
  122. {
  123. std::cerr << "Received a StackException: what()? " << e.what() << std::endl;
  124. }
  125. }
  126.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
Standard output is empty