Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAM Create a class called GroceryItem which has private data members: fl

ID: 3685782 • Letter: C

Question

C++ PROGRAM

Create a class called GroceryItem which has private data members:
float: price - for one box or jar
int: quantity - how many of an item
float: cost - the cost of the item is price × quantity
name - a dynamic string

Static Member
1m Define a static member, total_cost, which adds up the cost of all the items.

Constructor
1m Create a constructor with parameters for data members: name, price, and
quantity. The quantity should have a default value of 1.
1m The constructor should calculate the cost member from price and quantity.
2m In the constructor, add the cost of the item being created to the static variable
total_cost.
1m Add a comment with cout<< creating an item using the name data member.
The constructor should work for the following code in main()
GroceryItem item1("milk", 2.50, 4);
GroceryItem item2("cereal", 8.95, 2);
GroceryItem item3("bread", 3.50);

Operator Function
2m Create a member operator function using << which prints out the information for
an item purchased. The item name and price are printed. If the quantity is more
than one, the quantity and cost are also printed. See the sample output on the
next page.

Non-member Function
3m Create a non-member friend function called Two_item_deal()
This is for a promotion the store is putting on. The first two items have been
Page 2
chosen as arguments to the function. Use pass by reference.
If the total cost of these two items is over $15.00 then an extra box or jar will be
added for free. Update the quantity data for each, but leave the cost data as is.
Output a message on the new quantities with a congratulations!! message.
If the cost is less than $15, then output a message "No deals today"
Destructor

1m Create a destructor the outputs a message deleting an item using its name
member.

In main();
1m • create three items as follows GoceryItem item1("milk", 2.50, 4);
GroceryItem item2("cereal", 8.95, 2);
GroceryItem item3("bread", 3.50);
1m • Call the Display function for each item.
1m • Call the Two_item_deal() function only for the first two items
1m • When the program finishes print out the total cost of the 3 items from the static
variable.
Here is a sample output..
milk:
price: $2.50, for 4, $10.00
cereal:
price: $8.95, for 2, $17.90
bread:
price: $3.50
Congratulations!!, you now have 5 of milk and 3 of cereal
Total cost: $31.40
deleting bread
deleting cereal
deleting milk
Bonus Mark
1m Describe how the cost of $15 should best be represented in you code. Think of
defines, constants.

Explanation / Answer

//C++ program to test the class GroceryItem. h and
//GroceryItem. h and
#include<iostream>
#include "GroceryItem.h"
using namespace std;
//intialize static variable total_cost to 0
//set total_cost =0
float GroceryItem::total_cost=0;
//function prototype
void Two_item_deal(GroceryItem &item1,GroceryItem &item2);
//set constant value
const float PROMOTION_COST=15;
//main method
int main()
{

   //create three GroceryItem objects
   GroceryItem item1("milk", 2.50, 4);
   GroceryItem item2("cereal", 8.95, 2);
   GroceryItem item3("bread", 3.50);

   //print items to console
   cout<<item1;
   cout<<item2;
   cout<<item3;
  
   Two_item_deal(item1,item2);
   cout<<"Total cost: $"<<GroceryItem::total_cost<<endl;

   //explicit call to destructor
   item1.~GroceryItem();
   item2.~GroceryItem();
   item3.~GroceryItem();

   system("pause");
   return 0;

}

//The method Two_item_deal that takes two GroceryItem objects
//and check if total cost is more than or equal to 15
void Two_item_deal(GroceryItem &item1,GroceryItem &item2)
{
  
   if(GroceryItem::total_cost>=PROMOTION_COST)
       cout<<"Congratulations!!, you now have "<<item1.getQuantity()+1<<
       " of milk and "<<item2.getQuantity()+1<<" of cereal . "<<endl;
}


---------------------------------------------------------------------------------------------------------

//GroceryItem.h
#ifndef GROCERY_ITEM
#define GROCERY_ITEM
#include<iostream>
#include<string>
using namespace std;
class GroceryItem
{
   string name;
   float price;
   int quantity;
   float cost;
  

public:

   //public member functions
   GroceryItem(string name, float price);
   GroceryItem(string name, float price, int quantity);

   friend ostream &operator<<(std::ostream &out, GroceryItem c);
   int getQuantity();

   ~GroceryItem();

   //declaration of static variable
   static float total_cost;
};

#endif GROCERY_ITEM

--------------------------------------------------------------------------------------------------------------------------------

//GroceryItem.cpp
#include<iostream>
#include "GroceryItem.h"
using namespace std;

//constructor that take name and price and set quantity=1
GroceryItem::GroceryItem(string name, float price)
{
   this->name=name;  
   this->price=price;
   quantity=1;
   cost=quantity*price;
   total_cost+=cost;
}
//constructor that take name and price and quantity
GroceryItem::GroceryItem(string name, float price,int quantity)
{
   this->name=name;
   this->quantity=quantity;
   this->price=price;
   cost=quantity*price;

   total_cost+=cost;

}

//Returns quantity
int GroceryItem::getQuantity()
{
   return quantity;
}

//Overload << operator
std::ostream &operator<<(std::ostream &out, GroceryItem gc) {
   out << gc.name<<" :"<<endl;
   out << "Price : "<<gc.cost;

   if(gc.quantity>=2)
       out<<", for "<<gc.quantity<<", $"<<gc.cost<<endl;

    return out;
}

//Destructor of class
GroceryItem::~GroceryItem()
{
   cout<<"deleting "<<name<<endl;
}


------------------------------------------------------------------------------------------------------------

Sample Output:


milk :
Price : 10, for 4, $10
deleting milk
cereal :
Price : 17.9, for 2, $17.9
deleting cereal
bread :
Price : 3.5deleting bread
Congratulations!!, you now have 5 of milk and 3 of cereal .
Total cost: $31.4
deleting milk
deleting cereal
deleting bread

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote