Code needed in C++, make changes to the file provided (18-1, has3 files)
Chapter 18 Stacks and Queues
-----------------------------------------------------------------------------------------------------
capacity is just 5
1. push 6 numbers on the stack
2. catch the overflow error in a catch block
3. pop one element, which means your capacity is now down to4
4. push the element that was rejected earlier
5. verify your entire stack by popping to show the newnumbers.
IntStack.h
#include
using namespace std;
class IntStack
{
unique_ptrstackArray;
int capacity;
int top;
public:
// Constructor
IntStack(int capacity);
// Member functions
void push(int value);
void pop(int &value);
bool isEmpty() const;
// Stack Exceptions
class Overflow {};
class Underflow {};
};
IntStack.cpp
ZOOM
#include \"intstack.h\"
//************************************
// Constructor *
//************************************
IntStack::IntStack(int capacity)
{
stackArray = make_unique(capacity);
this->capacity = capacity;
top = 0;
}
//***********************************
// Adds a value to the stack *
//***********************************
void IntStack::push(int value)
{
if (top == capacity) throw IntStack::Overflow();
stackArray[top] = value;
top++;
}
//****************************************
// Determines whether the stack is empty *
//****************************************
bool IntStack::isEmpty() const
{
return top == 0;
}
//************************************************
// Removes a value from the stack and returns it *
//************************************************
void IntStack::pop(int &value)
{
if (isEmpty()) throw IntStack::Underflow();
top--;
value = stackArray[top];
}
pr18-01.cpp
// This program illustrates the IntStack class.
#include \"intstack.h\"
#include
using namespace std;
int main()
{
IntStack stack(5);
int values[] = { 5, 10, 15, 20, 25 };
int value;
cout << \"Pushing...\n\";
for (int k = 0; k < 5; k++)
{
cout << values[k] << \" \";
stack.push(values[k]);
}
cout << \"\nPopping...\n\";
while (!stack.isEmpty())
{
stack.pop(value);
cout << value << \" \";
}
cout << endl;
return 0;
}