Question: Exercise 1 . [ 2 0 points ] Part A: Using templates, implement a Stack class that can be used to store items of any

Exercise 1.[20 points]
Part A: Using templates, implement a Stack class that can be used to store items of any type:
You do not need to implement any constructors or destructors; the default constructor should be sufficient, and you will not need to use new. Your class should support the following public functions (assuming T is the parameterized type which the Stack is storing):
(a) void push(const T& item)-Push an item onto the stack
(b) pop()-Pop an item from the stack
(c) top()-Get the top item of the stack (d) bool empty()-Check if the stack is empty
(e) size t size()-Get the size of the stack
(f) void display()-Display the elements in the stack
You can use an STL vector, deque, or list to implement your Stack. You cannot use the STL stack class. When working with templated classes, you cannot separate the function implementations into a separate .cpp file; put all your code in the class definition. (Hint: You can represent pushing by adding to the end of your vector, and popping by removing the last element of the vector. This ensures that the popped item is always the most recently inserted one that has not yet been popped.)
Part B: Make a friend function that implements a + operator for Stacks: The behavior of the + operator should be such that when you write p + q, you get a new stack containing p's items followed by q's items (assuming p and q are both Stacks), in their original order. See the sample runs below and also provide at least two diffent types of usage of your templated Stack.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!