Write a program in C++ which creates class named as · Item This class should con
ID: 3616787 • Letter: W
Question
Write a program in C++ which creates class named as
· Item
This class should contain following members
o stock //forstock quantity of a certain item
o quantitypurchased //for quantity to be purchased ina customer’sorder
This can be checked by comparingquantity_purchased with stock. If the quantity to be purchased ismore than the stock; an exception should be thrown.
To throw exception, you need tocreate another class named as
· Exception
In main create an object ofclass Item and call the order method inside the try block. You cancheck in the definition of the method order () whether stock isless than quantity purchased. If it is true, then throw exceptionthat displays message saying;
Thereis not enough stock
On the other hand if stock is morethan quantity purchased then you need to display appropriatemessage saying;
Therequired quantity of item is available in thestock
SampleOutput1:
There is not enough stock
(if the stock is less than quantitypurchased)
Sample Output2:
The required quantity of item is available inthe stock
Explanation / Answer
#include<iostream.h>
#include<conio.c>
class Exception{
private:
char message[30] ;
public:
Exception() {strcpy(message,"There is not enoughstock");}
char * get_message(){ return message; }
};
class Item {
private:
int stock ;
intrequired_quantity;
public:
Item(int stk, int qty)
{
stock = stk;
required_quantity = qty;
}
intget_stock()
{
return stock;
}
int get_required_quantity()
{
returnrequired_quantity;
}
void order()
{
if(get_stock()< get_required_quantity())
throwException();
else
cout<<"The required quantity of item is available in thestock";
}
~Item(){}
};
void main()
{
Item obj(10, 20);
try
{
obj.order();
}
catch(Exception& exp2)
{
getch();
cout << "Exception: " << exp2.get_message()<< endl;
}
getch();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.