Answered step by step
Verified Expert Solution
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 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 pushconst T& itemPush an item onto the stack
b popPop an item from the stack
c topGet the top item of the stack d bool emptyCheck if the stack is empty
e size t sizeGet the size of the stack
f void displayDisplay 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 ps items followed by qs 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started