Reimplement the class hierarchy of Exercise 5.1 so that the base Stack class implements the shared, type-independent members.
The reimplementation of the class hierarchy illustrates a concrete class hierarchy; that is, we replace our pure abstract Stack class with our implementation of LIFO_Stack, renaming it Stack. Although Stack serves as a base class, it also represents actual objects within our applications. Thus it is termed a concrete base class. Peekback_Stack is derived from Stack. In this implementation, it inherits all the members of Stack except peek(), which it overrides. Only the peek() member function and the destructor of Stack are virtual. The definitions of the member functions are the same and are not shown.
class Stack {
public:
Stack( int capacity = 0 ): _top( 0 )
{
if ( capacity )
_stack.reserve( capacity );
}
virtual ~Stack(){}
bool pop( elemType& );
bool push( const elemType& );
virtual bool peek( int, elemType& )
{ return false; }
int size() const { return _stack.size(); }
int top() const { return _top; }
bool empty() const { return ! _top; }
bool full() const { return size() >= _stack.max_size(); }
void print( ostream&=cout );
protected:
vector<elemType> _stack;
int _top;
};
class Peekback_Stack : public Stack {
public:
Peekback_Stack( int capacity = 0 )
: Stack( capacity ) {}
bool peek( int index, elemType &elem );
};
|