T
Tony Johansson
Hello Experts!
Assume I have the following template classes which are Stack and Element see
below.
I have removed all kinds of exeption just to get a shorter code.
The code below is not complete.
I wonder if I could have something in main like
Stack<Element<T> > s;
because the stack contains of element of type Element.
Im I right.
What changes should then has to be done in this present code.
template<class U>
class Element;
template <class T>
class Stack
{
public:
Stack() : first(0) {}
~Stack();
void push(T d);
void pop();
T top();
bool empty();
private:
Element<T> *first;
Stack(const Stack&) {}
Stack& operator= (const Stack&) {}
};
template <class T>
void Stack<T>:ush( T d)
{
first = new Element<T>(first, d);
}
template <class T>
Stack<T>::~Stack()
{
while(!empty() )
{
Element<T> *p = first;
first = first->next;
delete p;
}
}
template<class U>
class Element
{
friend class Stack<U>;
Element *next;
U data;
Element(Element* n, int d) : next(n), data(d) {}
};
int main
{
Stack<int> s;
}
Many thanks!!
//Tony
Assume I have the following template classes which are Stack and Element see
below.
I have removed all kinds of exeption just to get a shorter code.
The code below is not complete.
I wonder if I could have something in main like
Stack<Element<T> > s;
because the stack contains of element of type Element.
Im I right.
What changes should then has to be done in this present code.
template<class U>
class Element;
template <class T>
class Stack
{
public:
Stack() : first(0) {}
~Stack();
void push(T d);
void pop();
T top();
bool empty();
private:
Element<T> *first;
Stack(const Stack&) {}
Stack& operator= (const Stack&) {}
};
template <class T>
void Stack<T>:ush( T d)
{
first = new Element<T>(first, d);
}
template <class T>
Stack<T>::~Stack()
{
while(!empty() )
{
Element<T> *p = first;
first = first->next;
delete p;
}
}
template<class U>
class Element
{
friend class Stack<U>;
Element *next;
U data;
Element(Element* n, int d) : next(n), data(d) {}
};
int main
{
Stack<int> s;
}
Many thanks!!
//Tony