Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions