A. Implement all the member functions of the Stack class. B. Write the code for
ID: 3606205 • Letter: A
Question
A. Implement all the member functions of the Stack class.
B. Write the code for a main() function that test all the member functions. Note that you are ask to display at least the contents of the element removed from that Stack object.
#include <vector>
template <class T>
class Stack
{
friend ostream& operator<<(ostream&, const Stack<T>& obj);
// Postcondition: displalys the top element stored on top of the Stack obj
public:
stack();
// Postcondition: creates an empty Stack object 5%
bool isEmpty();
// Postcondition: returns true if the Stack object is empty; false otherwise 5%
int size();
// Postcondition: returns the size or the # of elements that are stored in the stack 5%
void push(T&);
// Postcondition: a new T-type object is pushed onto the Stack object 10%
T& top();
// Postcondition: returns the object sitting on top of the Stack object 10%
void pop();
// Postcondition: if the Stack object is not empty, the top element is removed. 10%
private:
vector<T> v;
};
Since the Stack class uses a vector type of data member as its underlying storage, your may need to uses ome or all of the following member functions of the vector class. The prototypes of these functions are provided below:
void push_back(T&);
// Postcondition: Inserts an element to the end of a vector; automatically increasing the container size by one.
T& back();
// Postcondition: if the vector is not empty, returns a reference to the last element of the vector
void pop_back();
// Postcondition: if the vector is not empty, removes the last element in the vector and automatically reducing the container size by one.
int size();
// Postcondition: returns the size or # of elements stored in the vector
bool empty();
// Postcondition: returns true if vector is empty; returns false otherwise
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Stack
{
private:
// Vector initialized to empty
vector<T> v;
public:
// The constructor of the Stack class
// Does not have to do anything as vector is already initialized to empty
Stack(){
// vector is empty
}
// Postcondition: creates an empty Stack object 5%
// Function to return if the stack is empty
bool isEmpty(){
// If the vector is empty, the stack is empty
return v.empty();
}
// Postcondition: returns true if the Stack object is empty; false otherwise 5%
// Size of the stack is the size of the vector
int size(){
return v.size();
}
// Postcondition: returns the size or the # of elements that are stored in the stack 5%
// Function to push element t into the stack
void push(T& t){
// Use push_back function of vector
v.push_back(t);
}
// Postcondition: a new T-type object is pushed onto the Stack object 10%
// Function to return the top element of the stack
T& top(){
// Check if there are no elements in the vector
// If yes, throw an exception saying there are no elements
if(v.empty()){
throw std::out_of_range("underflow");
}
// Use vector's back method that returns the last element
return v.back();
}
// Postcondition: returns the object sitting on top of the Stack object 10%
// Function to return the top element of the stack
void pop(){
// Check if there are no elements in the vector
// If yes, throw an exception saying there are no elements
if(v.empty()){
throw std::out_of_range("underflow");
}
// Use vector's pop_back method that removes the last element
v.pop_back();
}
// Postcondition: if the Stack object is not empty, the top element is removed. 10%
// implement operator overloading in order to print the top element of the stack
// by accessing back function of the vector v.
// though, v is private we can access as this function is declared friend
friend ostream& operator<<(ostream& out, const Stack<T>& obj){
out << obj.v.back();
}
// Postcondition: displalys the top element stored on top of the Stack obj
};
// sample class that stores an integer
class myclass {
int i;
public:
// constructor to copy param j to variable i
myclass(int j) {i = j;}
// returns i
int get_i(){return i;}
// overload << operator to print its int element
friend ostream& operator<<(ostream& out, const myclass& obj){
out << obj.i;
}
};
// Driver program to test above functions
int main()
{
// create 3 myclass instances with 3 different values
myclass n1(1);
myclass n2(34);
myclass n3(27);
// Create a stack
struct Stack<myclass> s;
// push the myclass instances created above into the stack
s.push(n1);
s.push(n3);
s.push(n2);
// Pop from the stack
s.pop();
// Print the top element of the stack
cout << s.top().get_i() << " is on top of the stack ";
cout << s;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.