Question: C++ PROGRAM Create a class called GroceryItem which has private data members: float: price - for one box or jar int: quantity - how many
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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
