#include //for the cerr object #include //for class out_of_range #include "stack.h" using namespace std; //because class out_of_range belongs to namespace std; stack::stack() //Constructor : sp {a} //Start with the stack empty. a means &a[0] { } stack::~stack() //Destructor { if (sp != a) { //a means &a[0] cerr << "Warning: destructing a non-empty stack.\n"; } } void stack::push(int i) //Insert the int i into the stack. { if (sp == a + n) { //a+n means &a[n] //The stack is already full. throw out_of_range("Overflow: stack already filled to capacity"); } *sp = i; //Store i into the first currently unused space. ++sp; } int stack::pop() //Remove an int (the most recently inserted one) from stack. { if (sp == a) { //a means &a[0] //The stack is already empty. throw out_of_range("Underflow: can't take blood from a stone."); } --sp; //Point at the most recently pushed value. return *sp; //Return the most recently pushed value. }