Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stacks were introduced in Chapters 13 and 14. Define a stack class for storing a

ID: 3826188 • Letter: S

Question

Stacks were introduced in Chapters 13 and 14. Define a stack class for storing a stack of elements of type char A stack object should be of fixed size, the size is a parameter to the constructor that creates a stack object. When used in a program, an object of the stack class will throw exceptions in the following situations Throw a StackOverflowException if the application program tries to push data onto a stack that is already full. Throw a StackEmptyException if the application program tries to pop data off an empty stack. Defining the classes StackOverflowException and StackEmptyException is part of this project. Write a suitable test program.

Explanation / Answer

#include <iostream>
using namespace std;
class PushToFullStackException
{
    public:
        PushToFullStackException()
        {
           message = "EXCEPTION: Your stack is full!";
        }
        PushToFullStackException(string msg)
        {
           message = msg;
        }
        string what()
        {
           return message;
        }
    private:
        string message;
};

class PopEmptyStackException
{
    public:
        PopEmptyStackException()
        {
           message = "EXCEPTION: Your stack is empty!";
        }
        PopEmptyStackException(string msg)
        {
           message = msg;
        }
        string what()
        {
           return message;
        }
    private:
        string message;
};

class Stack
{
    int array[3];
    int top;
    public:
        Stack()
        {
           top = -1;
        }
        void push(int value)
        {
           try
           {
              if(top == 3)
                  throw PushToFullStackException("EXCEPTION: Your stack is full!");
              top++;
              array[top] = value;
           }
           catch (PushToFullStackException& me)
           {
              std::cerr << me.what();
           }
        }
        int pop()
        {
           try
           {
              if(top == -1)
                  throw PopEmptyStackException("EXCEPTION: Your stack is empty!");
              top--;
              return array[top+1];
           }
           catch (PopEmptyStackException& me)
           {
              std::cerr << me.what();
           }
           return -1;
        }
};

int main()
{
    Stack myStk; //Capacity of 3
    cout << "Popping right away.";
    myStk.pop();
    cout << " Pushing 1st item.";
    myStk.push(1);
    cout << " Pushing 2nd item.";
    myStk.push(2);
    cout << " Pushing 3rd item.";
    myStk.push(3);
    cout << " Pushing 4st item.";
    myStk.push(4);
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote