MENU

Chapter 3 Implementation of OOP Concepts in C plus plus Solutions

Question - 21 : -
Define a class Stock in C++ with following description;
Private Memebrs
тАв┬а ICode of type integer (Item Code)
тАв┬а Item of type string (Item Name)
тАв┬а Price of type float (Price of each item)
тАв┬а Qty of type integer (Quantity in stock)
тАв┬а Discount of type float (Discount percentage on the item)
тАв┬а A member function FindDisc() to calculate discount as per the following rule :
If Qty< =50┬а ┬а ┬а ┬а ┬а ┬а ┬а Discount is 0
If 50
If Qty >100┬а ┬а ┬а ┬а ┬а ┬а ┬а Discount is 10
Public Members
тАв A function buy () to allow use to entervalue for ICode, Item, Price, Qty and call function Find Disc () to calculate the discount.
тАв A function Show All() to allow user to view the content of all the data members.

Answer - 21 : -

class Stock
┬а{
┬аint ICode, Qty ;┬а
char Item[20]┬а ┬а ;
┬аfloat Price, Discount;┬а
void FindDisc ()┬а ┬а ;
┬аpublic :
┬аvoid Buy ()┬а ┬а ;
┬аvoid ShowAll ()┬а ┬а ;
┬а};
┬аvoid Stock :┬а ┬а : Buy ( )
{
cin>>ICode ;┬а
gets (Item) ;┬а
cin >> Price ;┬а
cin >> Qty ;
┬аFind Disc () ;
┬а}
┬аvoid Stock : : FindDisc ()
┬а{
┬аIf (Qty < = 50)
┬аDiscount = 0 ;┬а
else if (Qty < = 100)
┬аDiscount =5;┬а ┬а // = 0.05 ;
┬аelse
┬аDiscount = 10 ;┬а ┬а // =0.1;
}
┬аvoid Stock :┬а ┬а : ShowAll ()
┬а{
c o u t < < I C o d e < < ' \ t ' < < l t e m < < 1 \┬а
t1<
t1<
┬а}

Question - 22 : -
What is the relationship between a class and an object? Illustrate with a suitable example.┬а

Answer - 22 : -

A class is a collection of objects. Each object represents the behaviour and functions, which the class members can performs. In other words, an object is an instance of a class.┬а ┬а [1]
For Example:
A Class Box will have characteristics as length, breadth and height:

class Box
┬а{ int length, breadth, height;┬а
void Display (int length, int breadth, int height)┬а
{cout<
┬а};
┬аvoid main()
┬а{Box a=new Box();
┬аa. Display(10,20,30);
┬а// a is the object of class Box [1]
┬а}

×