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.
#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();
}
Comments
Leave a comment