|
|
The author of the Stack library has determined that there are two errors which can occur in the use of Stack: stack underflow, for example, trying to access an element of an empty stack, and overflow, for example, trying to add an element to a full stack. The library writer must declare and instantiate two Objections which correspond to these errors. Objections can be declared as static data members of the Stack class in the following way:
Stack.h:
#include <Objection.h> // Objection header file
const int SIZE = 10;
class Stack {
public:
static Objection underflow;
static Objection overflow;
Stack();
void push(int);
int pop();
int top();
private:
int stack[SIZE];
int size;
};
The Objections referred to in the Stack class must be initialized at some point in the library. The definitions must be part of a source code file, and are initialized as follows:
prog.c: includes definitions of Objections
#include <Stack.h>
.
.
.
Objection Stack::underflow;
Objection Stack::overflow;
The Objection::Objection()
constructor is
used to instantiate the Objections and is one
of two constructors defined for the
Objection class.
We will see the other constructor in a later discussion.