In: Computer Science
C++ Code
***User Interface
Write a program that offers an easy way to add items, remove the last item, look at the last item put in the list. You will write all of the code for processing the stack - do not use any predefined objects in C++. You decide how the interface will look. A menu driven program is always a good choice.
***Rules for program***
****PLEASE READ**** Additional information for program below.
Write a program to keep up with a collection of "Item" objects. ( Please put comments on all functions other than main to describe purpose)
create a class “Item” with the following private data attributes:
For this lab have separate files in a project. Put your class definitions in header files and the implementations of the methods in a .cpp file.
You will have the following public methods:
Create another class to implement a stack of "Items" using an array-based implementation.
Your stack class should include the following operations:
Program to implement stack functions using array implementation:
// In this array has a pair two string value called item_name and color,
//peek - method to display top element;
//isempty - to check stack is empty;
//push - to add element in the stack class;
//pop - to remove last inserted data;
// display - to show all the data in the stack class;
#include <iostream>
using namespace std;
class Item{ // Item class is created here
public:
pair<string,string> stack[100]; // Here is the pair of item_name and color and size of array is 100
int n=100; // n here is used to know the size of array
int top=-1; // top here is used to check the top element index and also used to check empty stack and full stack
void push(string item_name,string color) { // push method takes two argument item_name and color
if(top>=n-1) // if loop is to check the stack is full aur not
cout<<"Stack is full"<<endl;
else {
stack[top]=make_pair(item_name,color); // if not full we push or add the pair of item_name and color
top++; // and increment the value of top to add another data in next index
}
}
void isEmpty(){ // isempty is used to check stack is empty or not
if(top<=-1){ // if the top value is -1 or less than -1 then the stack is empty
cout<<"Stack is empty"<<endl;
}
}
void peek(){ // peek is used to display top element of stack
if(top<=-1){ // check whether the stack is empty or not
cout<<"Stack is empty"<<endl;
}
else{
cout<<stack[top].first<<" "<<stack[top].second<<endl; // if not empty then display the current position of top in stack array
}
}
void pop() { // here pop method is used to delete element which is last inserted
if(top<=-1)
cout<<"Stack is empty"<<endl;
else {
top--; // in this we decrement the value of top so that access to last inserted data is not possible
}
}
void display() { // display is used to show all the elements in the stack array
if(top>=0) { // show only when stack is not empty
for(int i=top; i>=0; i--) // for loop to iterate over the stack array
cout<<stack[i].first<<" "<<stack[i].second<<endl;
} else
cout<<"Stack is empty";
}
};
int main() {
Item t; // create a object of Item class
t.push("Apple","red"); // Push element in the Item Stack class
t.push("Hair","black");
t.push("sky","blue");
t.push("heart","pink");
t.push("chocolate","brown");
t.pop(); // pop last inserted element in the Item Stack Class
t.display();
}