logo CodeStepByStep logo

ArrayStack

Write a class named ArrayStack that represents a stack of integers implemented using an unfilled array.

For example, say our stack can hold 5 elements and we push 3 elements: 10, 20, 30. Our stack would look like this:

index    0    1    2    3    4
      +----+----+----+----+----+
value | 10 | 20 | 30 |    |    |
      +----+----+----+----+----+

size     = 3
capacity = 5

Your class should have the following public members:

member name description
ArrayStack() constructs an empty stack with a capacity of 5
~ArrayStack() frees array memory associated with the stack
push(value) adds the given integer to the top of the stack; resizes the stack's internal array if necessary to fit the new element
pop() returns and removes the element on top of the stack; throws a string exception if stack is empty
peek() Returns element on top of the stack without removing it; throws a string exception if stack is empty
isEmpty() Returns true if stack is empty and false otherwise
size() Returns the number of elements in the queue
operator << outputs a string representation of the stack from bottom to top, such as: {10, 20, 30}

You should define the entire class including the class heading, the private member variables, and the declarations and definitions of all the public member functions and constructor.

Class: Write a complete C++ class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.