CJCoding With Joseph
15free / daySign inCreate account

Bounded Stack with Overflow and Underflow

Build a class BoundedStack from scratch. It holds at most three int values in a private array int m_items[3] and keeps a private int m_count of how many are in use, starting at 0. The top is the last value pushed.

- void push(int value): when the stack already holds three values, throw std::overflow_error("stack is full") and change nothing; otherwise store value as the new top - int pop(): when the stack is empty, throw std::underflow_error("stack is empty"); otherwise remove the top value and return it - int size() const: the number of values in use

The tests read commands from input: push followed by a number, pop, or size. They print pushed and the value, popped and the value, size and the count, or error: and what() when a push or pop throws. For the first test the commands push 1, 2, 3 and 4, pop four times, then ask for the size.

Do NOT write a main function.

Expected Output:

pushed 1
pushed 2
pushed 3
error: stack is full
popped 3
popped 2
popped 1
error: stack is empty
size 0

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Exceptions
📥 Auto-Input:
push 1\npush 2\npush 3\npush 4\npop\npop\npop\npop\nsize\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.