LeakyStackA - throw runtime_error("Nothing in...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <cstdlib> #include <iostream> #include <stdexcept> #include "LeakyStack.h" #include "LeakyStackA.h" using namespace std; /** * Constructor with specified max capacity * (parameter is optional, so this also serves as default constructor) * @param the maximum capacity */ LeakyStackA::LeakyStackA(int cap) { Stack = new Object[cap]; top = -1; } /** Returns the number of objects in the stack. * @return number of elements */ int LeakyStackA::size() const { return (top+1); // Replace this as needed } /** Determines if the stack is currently empty. * @return true if empty, false otherwise. */ bool LeakyStackA::empty() const { return (top< 0); // Replace this as needed } /** Returns a const reference to the top object in the stack. * Throws a runtime_error if the stack is empty. * @return reference to top element */ // These lines are NOT correct, but they compile. .. if (empty())
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: throw runtime_error(&quot;Nothing in history.&quot;); else { return &amp;top; // returns reference to this local variable (a sure mistake!) } } /** Inserts an object at the top of the stack. If the stack * is already at capacity, the oldest element will be lost. * @param the new element */ void LeakyStackA::push(const string&amp; elem) { if (top==cap){ string temp; string temp2; for (int i=top; i&gt;0; i--){ temp=Stack[i]; temp2=Stack[i-1]; ///Need to keep record of the object that's going to be overrided. Stack[i-1]=temp; if ((i-2&gt;0)) ///Check if index is at the second to last to see if we need to store the object before Stack[i] Stack[i-2]=temp2; } } Stack[top]=elem; } /** Removes the top object from the stack. * Throws a runtime_error if the stack is empty. */ void LeakyStackA::pop() { if (empty()) throw runtime_error(&quot;Nothing in stack.&quot;) else top--;// . .. }...
View Full Document

This note was uploaded on 05/03/2011 for the course CSCI 180 taught by Professor Goldwasser,m during the Fall '08 term at Saint Louis.

Page1 / 2

LeakyStackA - throw runtime_error(&amp;amp;quot;Nothing in...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online