// genericStack.hpp // 6_STD_stacks #ifndef genericStack_hpp #define genericStack_hpp #include // good: encapsulation // good: separation of interface and implementation // good: arbitrary element type // good: arbitrary capacity // bad: implementation in .hpp // bad: compiler error messages are difficult to read template struct genericStack { int tos; // top of stack, i.e. index of next write/push ElementType elements[capacity]; public: genericStack(); void push(ElementType element); ElementType pop(); int size(); // number of elements pushed bool isEmpty(); void clear(); void print(); }; // ============= implementation ============= template genericStack::genericStack() : tos(0) { } template void genericStack::push(ElementType element){ elements[tos++] = element; } template ElementType genericStack::pop(){ return elements[--tos]; } template int genericStack::size(){ // number of elements pushed return tos; } template bool genericStack::isEmpty(){ return tos == 0; } template void genericStack::clear(){ tos = 0; } template void genericStack::print(){ std::cout << size() << " of " << capacity << " allocated." << std::endl; for (int i=0; i