One day, I wrote a simple Stack class:
Code:
class LinkedList {
public:
int value;
LinkedList *next;
LinkedList() : next(0) { }
};
class Stack
{
protected:
LinkedList *top;
public:
Stack() : top(0) { }
void push( int value );
int pop();
};
I compiled it and then use it in some project...
3 years later, I need a stack that counts every item that pushed or popped into it
I lost the Stack source code and the only thing left is its header and compiled binary
due to OOP matter, I just extend it then.
Code:
class CountingStack : public Stack
{
protected:
int count;
public:
CountingStack() : count(0) { }
void push( int value ) {
//count++
}
int pop() {
//count--
}
int getCount();
};
the next year , I need a stack that throws an Exception when it's empty
but it doesn't need to count every item that pushed or popped into it
So I wrote this ExceptionStack
Code:
class ExceptionStack : public Stack
{
public:
int pop() {
//throw Exception if the stack is empty ( top == NULL )
}
};
And now, I need both CountingStack and ExceptionStack features
but again, I lost their source code!
Because of the project's time limit, I couln't rewrite the Stack code.
So, I guess I need to write this kind of class, but I found a problem...
Code:
class CountingExceptionStack : public CountingStack, public ExceptionStack
{
public:
void push( int value )
{
CountingStack::push( value );
}
/*
int pop()
{
What should I write here??
}
*/
};