#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] } void stack::push(int i) //Insert an int 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 a 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. }