CJCoding With Joseph
15free / daySign inCreate account

Fixed-Capacity Stack Template

Build a class template Stack<T, Capacity> from scratch: T is the element type and Capacity is an int non-type parameter giving the most items the stack can hold. Store the items in a private array T items[Capacity] together with a private int count of how many are in use (the constructor sets it to 0). The top of the stack is the last item pushed.

Requirements: - bool push(T value): when full, return false without storing; otherwise store value as the new top and return true - T pop(): when empty, print Stack is empty and a newline and return T{}; otherwise remove the top item and return it - T peek() const: returns the top item (only called when the stack is not empty) - bool isEmpty() const, bool isFull() const, int size() const

The tests switch std::cout to boolalpha so bool results print as true or false, and print one value per line. With a Stack<int, 2> they print isEmpty(), the result of pushing 5, pushing 8 and pushing 9, then isFull(), peek(), two pops, a third pop on the now-empty stack (which prints the message first and then the returned 0), and size(). With a Stack<std::string, 3> they push a and b, then print one pop and size().

Do NOT write a main function.

Expected Output:

true
true
true
false
true
8
8
5
Stack is empty
0
0
b
1

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

Hints4 available
Topics:
Templates
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.