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]
 }

×