#include #include template // stack means "a stack of T's" class stack // stack is an abstract data type. What matters is // what effects the add, remove, and isempty operations // have, how they are implemented doesn't matter. { public: // this is the contract with the programmer // the programmer may not change the public section void add(T data); T remove(); // remove must take most-recently-added data bool isempty(); stack(); protected: // the programmer puts whatever the implementation // needs down here. This can be changed any time. T data[10]; int top; // this is a really cheap and lame implementation. }; template stack::stack() { top=0; } template void stack::add(T item) { if (top>=10) { cerr << "stack Full!!!!\n"; exit(1); } data[top]=item; top+=1; } template T stack::remove() { if (top<=0) { cerr << "stack Empty!!!!\n"; exit(1); } top-=1; return data[top]; } template bool stack::isempty() { return top==0; } void main() { stack mystrings; stack myints; mystrings.add("the"); myints.add(1); mystrings.add("cat"); mystrings.add("sat"); mystrings.add("on"); myints.add(12); mystrings.add("her"); myints.add(123); myints.add(1234); mystrings.remove(); myints.add(12345); mystrings.add("the"); mystrings.add("mat"); while (!mystrings.isempty()) { cout << mystrings.remove() << " "; } while (!myints.isempty()) { cout << myints.remove() << " "; } cout << "\n"; }