Write a program in C++ which creates class named as · Item This class should con
ID: 3616758 • Letter: W
Question
Write a program in C++ which creates class named as
· Item
This class should contain following members
o stock //for stockquantity of a certain item
o quantity purchased //forquantity to be purchased in a customer’sorder
o Order() //for checking whether order can be placed or not.
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 to createanother class named as
· Exception
In main create an object of classItem and call the order method inside the try block. You can checkin the definition of the method order () whether stock is less thanquantity purchased. If it is true, then throw exception thatdisplays message saying;
There is not enoughstock
On the other hand if stock is morethan quantity purchased then you need to display appropriatemessage saying;
The required quantity of item is available in thestock
SampleOutput1:
There is not enough stock
(if the stock is less than quantitypurchased)
SampleOutput2:
The required quantity of item is available in thestock
(if the stock is greater than or equalto the quantity purchased)
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.