#include "ArrayStack.h" #include using namespace std; /** Standard constructor creates an empty stack with given capacity. */ template ArrayStack::ArrayStack(int cap = 1000) { _cap = cap; _t = 0; _S = new Object[_cap]; } template bool ArrayStack::empty() const { return (_t == 0); } /** Returns a live reference to the top object in the stack. * Return: reference to top element */ template Object& ArrayStack::top() { if (empty()) throw domain_error("Stack is empty"); return _S[_t-1]; } /** Returns a const reference to the top object in the stack. * Return: reference to top element */ template const Object& ArrayStack::top() const { if (empty()) throw domain_error("Stack is empty"); return _S[_t-1]; } /** Inserts an object at the top of the stack. * Input: the new element */ template void ArrayStack::push(const Object& elem) { //check if the array is full if (_t == _cap) throw domain_error("Stack is full"); //if there is room, add to the top _S[_t] = elem; _t++; } /** Removes the top object from the stack. */ template void ArrayStack::pop() { if (empty()) throw domain_error("Can't pop from empty stack"); _t--; } /** Returns the number of objects in the stack. * Return: number of elements */ template int ArrayStack::size() const { return _t; } //Copy constructor template ArrayStack::ArrayStack(const ArrayStack& other) { _cap = other._cap; _t = other._t; _S = new Object[_cap]; //loop to copy other's data over for (int i=0; i<_t; i++) _S[i] = other._S[i]; } //Destructor template ArrayStack::~ArrayStack() { delete[] _S; } //Operator= template ArrayStack& ArrayStack::operator=(const ArrayStack& other) { if (this != &other) { delete[] _S; _cap = other._cap; _t = other._t; _S = new Object[_cap]; //loop to copy other's data over for (int i=0; i<_t; i++) _S[i] = other._S[i]; } return *this; }