Raga mi date un'occhiata a questo Template? Grazie.
stack.h
codice:#ifndef _STACK_ #define _STACK_ template<class tipo> class stack { int top; int size; tipo* elements; public: stack(int n); ~stack() {delete[] elements;} void push(tipo a); tipo pop(); }; #endif // _STACK_
stack.cpp
main.cppcodice:#include "stack.h" #include <iostream.h> template<class tipo> stack<tipo>::stack(int n) { size = n; elements = new tipo[size]; top = 0; } void stack<tipo>::push(tipo a) { if (top < size) { elements[top] = a; top++; } } tipo stack<tipo>::pop() { if (!top) return (tipo) 0; top--; return elements[top]; } void display(stack<tipo>& s) { tipo a; while (a = s.pop()) cout << a << endl; }
codice:#include "stack.h" #include <iostream.h> void main() { stack<char> cstack(10); stack<double> fstack(20); stack<char*> sstack(5); cstack.push('a'); cstack.push('b'); cstack.push('c'); display(cstack); // scrive: c b a fstack.push(1.23); fstack.push(4.56); fstack.push(7.89); display(fstack); // scrive: 7.89 4.56 1.23 sstack.push("uno"); sstack.push("due"); sstack.push("tre"); display(sstack); // scrive: tre due uno }

