GCO4020/CSC428 - Advanced Object Oriented Techniques In C++
Week 4
Stack class,
rather than using a "raw" Array:
Stack
class enforces the "last-in-first-out" semantics of a
stack, ensuring that elements beneath the top element
cannot be "accidentally" accessed out of turn.
Stack class can actually
reduce code size, since every "pop" of a "raw array" would
require (at least) two statements: an operator[] on the last
element of the array, followed by a RemoveLast().
Stack class
(for example, to replace class Array with one of the
STL¤ classes, say deque)
(End of solution)
Date wrapper class might be implemented by
encapsulating an unsigned int or unsigned long.
PriorityStack might require two arrays,
corresponding to "high" and "low" priority items.
(End of solution)
Stack::Top() or Stack::Pop() when the stack
is empty. But both these attempts will result in Array::operator[]
being called with an invalid (negative) index, which will in turn
result in an Array::BoundsError exception being thrown. In other
words, both versions of Stack "inherit" the error handling behaviour
of their underlying Array implementation¤.
Of course, it may be considered desirable in the inheritance version to
make Array::BoundsError publicly (not privately) accessible in Stack
(that is, provide another using declaration), so as to be able to write:
template <class ElementType> class Stack : private Array<ElementType> { public: using Array<ElementType>::Count; using Array<ElementType>::BoundsError;
void Push(const ElementType& elem);
const ElementType& Top(void) const;
ElementType Pop(void); };
Stack<int> stack;
try { stack.Pop } catch (Stack<int>::BoundsError error) { // HANDLE EXCEPTION }
rather than being forced to catch an Array<int>::BoundsError.
Last updated: Fri Feb 18 11:17:31 2000