In: Computer Science
10.13 LAB*: Warm up: Online shopping cart (Part 1)
(1) Create three files to submit:
Build the ItemToPurchase class with the following specifications:
(2) In main(), prompt the user for two items and create two
objects of the ItemToPurchase class. Before prompting for the
second item, call cin.ignore() to allow the user
to input a new string. (2 pts)
Ex:
Item 1 Enter the item name: Chocolate Chips Enter the item price: 3 Enter the item quantity: 1 Item 2 Enter the item name: Bottled Water Enter the item price: 1 Enter the item quantity: 10
(3) Add the costs of the two items together and output the total
cost. (2 pts)
Ex:
TOTAL COST Chocolate Chips 1 @ $3 = $3 Bottled Water 10 @ $1 = $10 Total: $13
code ---------------------------------------------------------
#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
int main() {
/* Type your code here */
return 0;
}



output:

my code:
#include <iostream>
using namespace std;
/*Below make a class with a private member ,mutators &
accessors*/
class ItemTOPurchase{
private:
/*Below we define 3 private member*/
string itemname;
int itemprice;
int itemQuantity;
public:
/*default constructor with value 0*/
ItemTOPurchase()
{
itemname=" ";
itemprice=0;
itemQuantity=0;
}
/*below we make 3 mutators functions for set the value of private
member*/
void SetName(string n) /*this function is used for set itemname
*/
{
itemname=n;
}
void SetPrice(int p) /*this function is used for set itemprice
*/
{
itemprice=p;
}
void SetQuantity(int q) /*this function is used for set
itemQuantity */
{
itemQuantity=q;
}
/*below we make 3 accessors functions for set the value of private
member*/
string GetName() /*this function is used for get itemname */
{
return itemname;
}
int GetPrice() /*this function is used for get itemname */
{
return itemprice;
}
int GetQuantity() /*this function is used for get itemname */
{
return itemQuantity;
}
};
int main()
{
ItemTOPurchase I1; /*make object for first item*/
ItemTOPurchase I2; /*make object for second item*/
string name;
int price;
int quantity;
cout<<"Item 1"<<endl<<"Enter the item
name:"<<endl;
getline(cin, name); /*take item name input from user*/
cout<<"Enter the item price:"<<endl;
cin>>price; /*take item price input from user*/
cout<<"Enter the item quantity:"<<endl;
cin>>quantity; /*take item price input from user*/
I1.SetName(name); /*set item 1 name into object*/
I1.SetPrice(price); /*set item 1 price into object*/
I1.SetQuantity(quantity); /*set item 1 quantity into object*/
cin.ignore();
cout<<endl<<endl<<"Item
2"<<endl<<"Enter the item name:"<<endl;
getline(cin, name); /*take item name input from user*/
cout<<"Enter the item price:"<<endl;
cin>>price; /*take item price input from user*/
cout<<"Enter the item quantity:"<<endl;
cin>>quantity; /*take item price input from user*/
I2.SetName(name); /*set item 2 name into object*/
I2.SetPrice(price); /*set item 2 price into object*/
I2.SetQuantity(quantity); /*set item 2 quantity into object*/
/*here we print total cost we use of accessors of class*/
cout<<"TOTAl COST"<<endl;
cout<<I1.GetName()<<" "<<I1.GetQuantity()
<<" @ " <<I1.GetPrice()<<" =
"<<I1.GetPrice()*I1.GetQuantity()<<endl;
cout<<I2.GetName()<<" "<<I2.GetQuantity()
<<" @ " <<I2.GetPrice()<<" =
"<<I2.GetPrice()*I2.GetQuantity()<<endl;
return 0;
}