In: Computer Science
(C++ program)
***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***
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:
// stack.h
#ifndef STACK_H
#define STACK_H
#include <string>
using namespace std;
class Item
{
private:
string name;
string color;
public:
Item();
void setName(const string& name);
void setColor(const string& color);
string getName() const;
string getColor() const;
};
class Stack
{
private:
static const int MAX = 100; // large so that stack is never
full
Item items[MAX];
int top;
public:
Stack();
bool isEmpty() const;
void push(const Item& item);
void pop();
Item peek() const;
void display() const;
};
#endif // STACK_H
//end of stack.h
// stack.cpp
#include "stack.h"
#include <iostream>
// default constructor
Item::Item() : name(""), color("")
{}
// set the name of the item
void Item:: setName(const string& name)
{
this->name = name;
}
// set the color of the item
void Item:: setColor(const string& color)
{
this->color = color;
}
// return the name of the item
string Item:: getName() const
{
return name;
}
// return the color of the item
string Item:: getColor() const
{
return color;
}
// default constructor to create an empty stack of maximum size
100
Stack::Stack(): top(-1)
{}
// returns true if stack is empty else false
bool Stack:: isEmpty() const
{
return top == -1;
}
// insert item at top of stack
void Stack:: push(const Item& item)
{
top++;
items[top] = item;
}
// remove the top element of stack
void Stack:: pop()
{
if(!isEmpty()) // stack is not empty
top--;
}
// return the top element of stack
Item Stack:: peek() const
{
Item item;
// non-empty stack
if(!isEmpty())
item = items[top]; // get the top item
return item;
}
// display all the items of the stack
void Stack:: display() const
{
cout<<"Top: "<<top<<endl; // display top
index
// display the items from top to bottom
cout<<"Stack Items(top to bottom): "<<endl;
for(int i=top;i>=0;i--)
cout<<"Name: "<<items[i].getName()<<" Color:
"<<items[i].getColor()<<endl;
}
//end of stack.cpp
// driver.cpp : C++ program to test the stack class
#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
Stack stack;
int choice;
string name, color;
Item item;
// loop that continues until the user exits
do
{
// display menu
cout<<"1) Add an item"<<endl;
cout<<"2) Remove the top item"<<endl;
cout<<"3) Is the stack empty"<<endl;
cout<<"4) Get top item"<<endl;
cout<<"5) Display the stack"<<endl;
cout<<"6) Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
// based on menu choice perform the operation
if(choice == 1)
{
cout<<"Name: ";
cin.ignore();
getline(cin, name);
cout<<"Color: ";
getline(cin, color);
item.setName(name);
item.setColor(color);
stack.push(item);
}
else if(choice == 2)
{
stack.pop();
}
else if(choice == 3)
{
if(stack.isEmpty())
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
}
else if(choice == 4)
{
if(!stack.isEmpty()){
item = stack.peek();
cout<<"Name: "<<item.getName()<<" Color:
"<<item.getColor()<<endl;
}else
cout<<"Stack is empty"<<endl;
}
else if(choice == 5)
stack.display();
else if(choice != 6)
cout<<"Invalid choice"<<endl;
cout<<endl;
}while(choice != 6);
return 0;
}
//end of driver.cpp
Output: