#ifndef STACKH #define STACKH #include //iostream includes cstddef, which defines size_t #include using namespace std; template class stack; //forward declaration for a template class template bool operator==(const stack& s1, const stack& s2); template class stack { public: typedef T value_type; private: static const size_t max_size = 100; static size_t x; //just to demonstrate the syntax T a[max_size]; size_t n; //stack pointer: subscript of next free element public: stack(): n(0) {} ~stack(); void push(const T& t); T& pop(); friend bool operator==(const stack& s1, const stack& s2); }; template size_t stack::x = sizeof (stack); //definition of static data member template stack::~stack() { if (n != 0) { cerr << "Warning: stack still contains " << n << " value(s).\n"; } } //Push a value onto the stack. template void stack::push(const T& t) { if (n == max_size) { //overflow cerr << "Can't push when size " << n << " == capacity " << max_size << ".\n"; exit(EXIT_FAILURE); } a[n++] = t; } //Pop a value off the stack. template T& stack::pop() { if (n == 0) { //underflow cerr << "Can't pop when size " << n << " == 0.\n"; exit(EXIT_FAILURE); } return a[--n]; } template bool operator==(const ::stack& s1, const ::stack& s2) { if (s1.n != s2.n) { return false; } for (size_t i = 0; i < s1.n; ++i) { if (s1.a[i] != s2.a[i]) { return false; } } return true; } template inline bool operator!=(const ::stack& s1, const ::stack& s2) { return !(s1 == s2); } #endif