Answer to Question #273838 in C++ for sonu

Question #273838

Create a class inventory which stores code, cost, count and total cost of an item. Calculate total cost inside parameterized constructor. Include a function sale which takes sale quantity as argument and updates count and total cost. Include another function purchase which takes purchase quantity as argument and updates count and total cost. Both the sale and purchase function should use quantity as 1 when no argument is provided.

1
Expert's answer
2021-12-03T07:45:18-0500
#include<iostream>
#include<string>

using namespace std;

class Inventory
{
	int code;
	double cost;
	int count;
	double totCost;
public:
	Inventory(int _code, double _cost, int _count)
	:code(_code),cost(_cost),count(_count)
	{
		//Calculate total cost inside constructor
		totCost=cost*count;
	}
	void Sale(int quntity=1)
	{
		totCost=totCost-quntity*cost;
		count=count-quntity;
	}
	void Purchase(int quntity=1)
	{
		totCost=totCost+quntity*cost;
		count=count+quntity;
	}
	void Display()
	{
		cout<<"\nInfo about item:"
			<<"\nCode: "<<code
			<<"\nCost: "<<cost
			<<"\nCount: "<<count
			<<"\nTotal cost: "<<totCost;
	}
};

int main()
{
	Inventory inv(123,30,2);
	inv.Display();
	inv.Sale();
	cout<<"\nSale 1 item:";
	inv.Display();
	inv.Purchase();
	cout<<"\nPurchase 1 item:";
	inv.Display();
	cout<<"\nPurchase 3 item:";
	inv.Purchase(3);
	inv.Display();
	inv.Sale(2);
	cout<<"\nSale 2 item:";
	inv.Display();
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS