#include #include #include "Stack" using namespace std; #ifndef BOUNDEDSTACK #define BOUNDEDSTACK template class BoundedStack : public Stack { /***** Function Members *****/ public: /* --- Constructor --- * * Precondition: A BoundedStack has been declared. * Receive: a limit on the size of the bounded stack * Postcondition: The BoundedStack has been constructed * as an empty stack and with its myLimit * data member initialized to limit (default 0). **************************************************************/ BoundedStack(int limit = 0); /* --- Add a value to the stack if there is room--- * * Receive: The BoundedStack containing this function * (implicitly) * A value to be added to a BoundedStack * Pass back: The BoundedStack (implicitly), with value * added at its top, provided there is room * Output: Overflow message if no room for value **************************************************************/ void push(const StackElement & value); /***** Data Members *****/ protected: int myLimit; }; // end of class template declaration //--- Definition of Constructor --- template inline BoundedStack:: BoundedStack(int limit) : Stack() { myLimit = limit; } //--- Definition of push() --- template inline void BoundedStack:: push(const StackElement & value) { if (size() < myLimit) stack::push(value); else cerr << "*** Bounded stack overflow ***\n"; } #endif