I wrote a example from a book that uses two classes. A IntStack and a class called MathStack. The IntStack contains all the regular pop and push commands while MathStack includes the class intStack by declaring it public. The MathStack class performs mathematical operations using IntStack and its pop and push objects. Worked nicely when I completed it but next I wanted to update my stack to a dynamic stack and use this class again. The dynamic stack works well but I used templates when I wrote it. I believe this is what’s causing some problems with MathStack.
What should I do to make MathStack friendly with a template based stack? Please advice.
MathStack declaration:
#include "Stack.h"
class MathStack : public Stack
{
public:
//Constructor
//MathStack operations
void add();
void sub();
void mult();
void div();
void addAll();
void multAll();
};
Stack Declaration:
template < class T>
class Stack{
private:
List<T> aList;
public:
Stack();
Stack(const Stack<T> &aStack);
~Stack();
void push(T newItem);
void pop();
void pop(T StackTop);
T getTop();
bool isEmpty() const;
}; //end List class
The base class needs to be a specialization of the template, not just the general template-name.
If
MathStackonly holdsints:MathStackitself should be a template if it can hold anythingStackcan: