MENU

Chapter 3 Implementation of OOP Concepts in C plus plus Solutions

Question - 1 : -
Observe the following C++ code and answer the question (i) and (ii):   

class Traveller
 {
 long PNR;
 char TName [20];
 public :
 Traveller ( ) //Function 1
 {cout<<"Ready"<
 void Book (long P, char N [ ])
 //Function 2
 {PNR = P, strcpy (TName, N);} void print ()    //Function 3
 {cout<
 {cout<<"Booking cancelled!"<
 } ;
(i) Fill in the blank statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code: void main ( )
{
Traveller T;
___________    //Line 1
___________    //Line 2
}//Stops here

Answer - 1 : -

T. Book (1234567, “Ravi”);
//Line 1
T. Print ();
//Line 2
(1/2 Mark for writing each correct Function)
[ICBSE Marking Scheme 2015]
(ii) Which function will be executed at }//Stops here ? What is this function referred as ?
Function 4
OR
~ Traveller ( )
It is a destructor function.

Question - 2 : -

Write the definitionof a class PIC in C++ with following description :  
Private Members
— Pno // Data member for Picture Number (an integer)
— Category // Data member for picture category (a string)
— Location // Data member for Exhibition Location (a string)
— FixLocation // A member function to assign // Exhibition Location as percategory // as

Category

Location

Classic

Amina

Modern

Jim Plaq

Antique

Ustad Khan

Public Members
shown in the following table
— Enter ( ) // A function to allow user to enter values
// Pno, category and call FixLocation () function
— SeeAll ( ) // A function to display all the data members.

Answer - 2 : -

class PIC
{
 int Pno;
 char Category [20] ; 
char Location [20]; 
void FixLocation ( ); 
public :
 void Enter ( ) ; 
void SeeAll ( );
 };
 void PIC : FixLocation ( ) 
 {
 if (strucmpi (category, "Classic”)
 = = 0)
 strcpy (Location, "Amina") ;
 else  if (strcmpi (category,'' / "Modern") = = 0)
 strcpy (Location, "Jim Plaq") ; else if strcmpi (category, "Antique") = = 0) 
strcpy (Location, "Ustad khan");
void PIC : : Enter ( )
 {
 cin>>Pno; gets (Category) ; 
FixLocation ( );
}
 void PIC :    SeeAll ( )
 {
 cout<
 }

Question - 3 : -
What is the difference between the members in private visibility mode and the members in protected visibility mode inside a class ? Also, give a suitable C++ code to illustrate both. 

Answer - 3 : -

Private:
This type of member can only be accessed by code in the same class.    [1]
Protected:
This type of member can only be accessed by code in the same class, or in a derived class. [1]

//C++ code : class Me
{
public :
int a, b;
protected :
int c, d;  //protected Member
private :
int e, f;  //Private Member
}

Question - 4 : -
Write the definition of a class CITY in C + + with following description :   
Private Members

-    CCode    //Data member for
 City Code (an integer)
 -    CName    //Data    member    for
 City Name    (a string)
 -    Pop    //Data    member    for
 Population (a long int)
 -    KM    //Data    member    for
 Area Coverage (a float)
 -    Density    //Data    member    for
 Population Density (a float)
 -    DenCalO    //A member function
 to calculate............... 
 //Density as Pop/KM 
Public Members
 -    Record 0    //A function to
 allow user to enter values of //Acode, Name, pop, KM and call DenCalO function
 -    view    //A function to
 display all the data members 
//also display a message "Highly Populated City" 
//if the Density is more than 10000

Answer - 4 : -

Class City
 {
 int Ccode;
 Char CName [2 0] ; 
long int Pop; 
float KM; 
float Density; 
void Dencal (); 
Public :
 void Record(); 
void View();
 };
 void CITY :: 
Record ( )
 {
 cin>>ccode; 
gets(C Name);
 cin>>pop; 
cin»KM; 
Dencal () ;
 }
 void CITY :: view()
 {
 Cout < < code < < CName <
 if (Density>10000)
 cout<<"Highly populated city";
 }
 void CITY :: Dencal()
 {
 Density=Pop/KM;
 }

Question - 5 : -
Write the definition of a class METROPOLIS in C+ + with following description :    
Private Members
 -    MCode    //Data member for
 Code (an integer)
 -    MName    //Data member for
 Name (a string)
 -    MPop    //Data member for
 Population (a long int)
 -    Area    //Data member for
 Area Coverage (a float)
 -    PopDens    //Data member for
 Population Density (a float)
 -    CaldenO    //A member function
 to calculate.........
 //Density as PopDens/Area
Public Members
 -    Ender ()    //A function to
 allow    user to  enter values  of
 //MCode, MName, MPop, Area and call claDen () //function
 -    viewall    //A function    to
 display all the data members 
//also display a message "Highly Populated Area" 
//if the Density is more than 12000

Answer - 5 : -

class METROPOLIS
 {
 int MCode; 
char MName[2 0] ; 
long int MPop; 
float Area; 
float PopDens; 
void CalDen() 
public:
 Void Enter(); 
void veiw ALL();
 } ;
 void METROPOLIS::Enter()
 {
 cin>>Mcode; 
gets(MName);
 Cin>>Mpop;
 Cin>>Area;
 CalDen();
 }
 void METROPOLIS::viewALL ( )
 {
 cout <
 if (Pop Dens > 12000) cont<<"Highly Populated Area";
 }
 void METROPOLIS::call Den()
 {
 PopDens =Mpop/Area
 }    

Question - 6 : -
Answer the questions (i) to (iv) based on the following:    [Delhi, 2016]

class PRODUCT
 {
 int Code; 
char Item[20]; 
protected: 
float Qty; 
public:
 PRODUCT();
 void Getln(); 
void show();
 {
 int WCode;
 Protected :
 char Manager[20]; 
public:
 WHOLE SALER(); 
void Enter(); 
void Display();
 }
 class SHOWROOM :    public PRODUCT,
 private WHOLE SALER
 {
 Char Name [20], 
public :
 SHOWROOM(); 
void Input(); 
void View();
 } ;
(i) Which type of Inheritance out of the following is illustrated in the above example ?
–    Single Level Inheritance
–    Multi Level Inheritance
–    Multiple Inheritance
(ii) Write the names of all the data members, which are directly accessible from the member functions of class SHOWROOM.
(iii) Write the names of all the member functions, which are directly accessible by an object of class SHOWROOM.
(iv) What will be the order of execution of the constructors, when an object of class SHOWROOM is declared ?

Answer - 6 : -

(i) Multiple
(ii) Name, City, Manager, Qty
(iii) Input (), View (), Getln (), Show ()
(iv) PRODUCT (), WHOLESALER () , SHOWROOM ()

Question - 7 : -
Define a class Candidate in C++ with the following specification :   
Private Mumbers:
A data members Rno (Registration Number) type long
A data member Cname of type string
A data members Agg_marks (Aggregate Marks) of type float
A data members Grade of type char
A member function setGrade() to find the grade as per the aggregate marks obtained by the student. Equivalent aggregate marks range and the respective grade as shown below.

Aggregate Marks                      Grade
> = 80                                          A
Less than 80 and >    =    65     B
Less than 65 and >    =    50     C
Less than 50                               D
Public members:
A constructor to assign default values to data members:
Rno = 0, Cname “N.A”, Age_marks = 0.0
A function Getdata ( ) to allow users to enter values for Rno. Cname, Agg_marks and call function setGrade () to find the grade.
A function dispResult () to allow user to view the content of all the data members.

Answer - 7 : -

class Candidate 
{ long Rno;
 char Cname [20]; 
float Agg_marks; 
char Grade; 
void setGrade ()
 { if (Agg_marks>= 80)
 Grade = 'A';
 else if (Agg_marks<80 && Agg_ marks>=65)
 Grade = 'B' ;
 else if (Agg_marks<65 && Agg_ marks>=50)
 Grade = 'C'; 
else
 Grade = 'D' ;
 }
 public;
 Candidate ( )
 {
 Rno=0;
 Strcpy (Cname, ''N.A.''); 
Agg_marks=0.0;
 }
 void Getdata ()
 {
 cout<<''Registration No''; 
cin>>Rno; 
cout<<"Name''; 
cin>>Cname;
 cout<
cin>>Agg_marks; 
set Grade ();
 }
 void dispResult ()
 {
 cout<<"Registration No''<
cout<<"Name''<
 cout<<"Aggregate Marks''<
 marks;
 }

Question - 8 : -
Define a class Customer with the following specifications.   
Private Members:
Customer_no integer
Customerjname char (20)
Qty integer
Price, TotalPrice, Discount, Netprice float
Member Functions:
Public members:

1. A constructer to assign initial values of Customer_no as 111, Customer_name as “Leena”, Quantity as 0 and Price, Discount and Netprice as 0.
2. Input() – to read data members (Customer_ no, Customer_name, Quantity and Price) call Caldiscount().
3. Caldiscount ( ) – To calculate Discount according to TotalPrice and NetPrice
TotalPrice = Price*Qty
TotalPrice > =50000 – Discount 25% of TotalPrice TotalPrice >=25000 and TotalPrice <50000 – Discount 15% of TotalPrice TotalPrice <25000 – Discount 10% of TotalPrice Netprice = TotalPrice-Discount Show() – to display Customer details.

Answer - 8 : -

class Customer
 {
 int Customer_no; 
char Customer_name; 
int Qty;
 float Price, TotalPrice, Discount, Netprice; 
public:
 Customer ( )    [1]
 {
 Customer_no = 111;
 strcpy( Customer_name, "Leena");
 Qty=0 ;
 Price =0, Discount=0, Netprice=0;
 }
 void Input ()    [1]
{
 cin>>Custome r_no; 
gets( Customer_name); 
cin>>Qty>>TotalPrice;
 Caldiscount();
 }
 void Caldiscount()    [1]
 {
 TotalPrice = Price*Qty; 
if ( TotalPrice >= 50000)
 Discount = 0.25 * TotalPrice;
 else if (TotalPrice >= 2 5000) 
Discount = 0.15 * TotalPrice; 
else
 Discount = 0.10 * TotalPrice; 
Netprice = TotalPrice - Discount;
 }
 void Show ( )    [1]
 {
 cout<
 }
 };

Question - 9 : -
Define a class CONTEST folowing description:
Private Data Members

Eventno               integer
Description         char(30)
Score                    integer
Qualified             char

Public Member functions
• A constructor to assign initial values Evento as 11, Description’s “School level”, Score as 100, Qualified as ‘N’
• Input()-To take the input for Eventno, Description and Score.
• Award (int cutoffscore) – To assign Qualified as ‘Y’, If score is more than the cutoffscore that is passed as argument to the function, else assign Qualified as ‘N’.
• Displaydata()-to display all the data members.

Answer - 9 : -

class CONTEST
 {
 private :
 int Eventno;
 char ‘Description [30];
 public :
 Contest () ;
 void Input () ;
 void Award (int cutoffscore);    [1]
 void Display ();
 };
 Contest : : contest()
 {
 Evento =11 ;
 Description = "school level" ;
 Score = 100;
 Qualified = 'N' ;
 }    [1]
 void Contest : : Input ( )
 {
 cin >> Eventno;
 gets (Description);
 cin >> Score ;
 }    [1]
 void CONTEST : :    Award (in cut of
 score)
 {
 int K = cutoffscore ;
 (Score > K)
 Qualified = 'Y' ;
 else
 Qualified = 'N' ;
 }
 void CONTEST ()
 {
 cout << Event No : << Eventno << endl ;
 cout << Description :    <<
 Description << endl;
 cout << Score : << Score << Endl
 cout << Qualified : << Qualified << endl;
 } 

Question - 10 : -
Consider the following class state:

class State 
{
 protected :
 int tp; //no. of tourist places 
public :
 State()
 {
 tp = 0 ;
 }
 void inctp ( )
 {
 tp++ ;
 }
 int gettp ( ) 
{
 return tp;
 }
 };
Write a code in C+ + to publically derive another

class ‘District’ with the following additional members derived in the Public visibility mode.
Data Members
distname – char (50)
population – long
Member functions:
• dinput ( ) – To enter distname and population.
• doutput ( ) – To display distname and population on screen.

Answer - 10 : -

class District ; public state
 { [1]
 private :
 char *distname [50]; 
long population; 
pubilc :    [1]
 void dinput ()  [1]
 {
 gets (distname); 
cin ... population ;    [1]
 }
 void doutput ()
 {
puts (distname) ;
 cout >> population ;    [1]
 }
 }

×