MENU

Chapter 2 Object Oriented Programming in C plus plus Solutions

Question - 11 : -
Write any four important characteristics of object Oriented Programming ? Give example of any one of the characteristics using C++. 

Answer - 11 : -


Question - 12 : -
Find the output of the following C++ program 
#include
#include
#include 
class Class 
{
int Cno,total; 
char section; public:
Class(int no=l)
{
Cno=no; 
section='A'; 
total=30;
}
void admission(int c=20)
{
section++;
total+=c;
}
void ClassShow ()
{
cout<
endl;
}
};
void main ()
{
Class C1 (5),C2;
Cl.admission(25)
Cl.ClassShow();
C2.admission() ;
Cl.admission(30)
C2.ClassShow();
Cl.ClassShow();
}

Answer - 12 : -

5 : B : 55 [1]
1: B : 50 [1]
5 : C : 85 [1]

Question - 13 : -
Obtain the output of the following C+ + program, which will appear on the screen after its execution.
Important Note:
• All the desired header files are already included in the code, which are required to run the code,

class Game 
{
int Level, Score; 
char Type; 
public:
Game (char GType='p')
{Level=1; Score=0; Type= GType;} 
void Play (int GS); 
void Change (); 
void Show()
{
cout<
cout<
}
} ;
void main () 
{
Game A ('G'), B;
B. Show ();
A. Play (11);
A. Change ();
B. Play (25);
A. Show ();
B. Show ();
}
void Game:    : Change()
{
Type=(Type== 'P')?'G' :'P';
}
void Game: :Play(int GS)
{
Score+=GS; 
if(Score>=30)
Level=3;
else if (Score>=20)
Level=2; 
else
Level=l;
}        

Answer - 13 : -

P@1
0
P@1
11
P@2
25

Question - 14 : -
Obtain the output of the following C+ + program, which will appear on the screen after its execution.
Important Note:
• All the desired header files are already included in the code, which are required to run the code,

class Player 
{
int Score, Level; 
char Game;
public:
Player (char GGame='A')
{Score=0; Level=1; Game =GGame;} 
void Start (int SC); 
void Next (); 
void Disp ()
{
cout <
cout <
}
};
void main ()
{
Player P,Q ('B');
P. Disp ();
Q. Start (75);
Q. Next ();
P. Start (120);
Q. DispO ;
P.Disp ();
}
void Player : : Next ()
{
Game = (Game =='A')?'B':'A' ;
}
void Player : : Start (int SC)
{
Score+=SC;
If (Score>=100)
Level=3;
else if (Score>=50)
Level=2; 
else
Level=1;
}   

Answer - 14 : -

A@1 0
A@2
75
A@3
120    [3]

Question - 15 : -
Observe the following C++ code carefully and obtain the output, which will appear on the screen after execution of it.

# include  
class Aroundus
 {
 int Place, Humidity, Temp; public:
 Aroundus(int p = 2) {
 Place = P; Humidity = 60; Temp = 20;
 }
 void Hot (int T) {
 Temp+=T ;
 };
 void Humid (int H) {
 Humidity+=H ;
 };
 void JustSee ( )
 {
 cout<
 }
 };
 int main()
 {
 Aroundus A, B (5) ;
 A. Hot (10)    ;
 A. JustSee ( )    ;
 B. Humid (15)    ;
 B. Hot (2)    ;
 B. JustSee ( )    ;
 A. Humid (5)    ;
 A. Justsee ( ) ;   

Answer - 15 : -

Output:
2 : 30 & 60%    [1]
5 : 22 & 75%    [1]
2 : 30 & 65%    [1]

Question - 16 : -
Find the output of the following program : 

#include  
class METRO 
{
 int Mno, TripNo, PassengerCount; 
public :
METRO (int Tmno=l)
 {
 Mno=Tmno;
 TripNo=0;
 PassengerCount=0;
 }
 void Trip (int PC = 20)
 { TripNo++; PassengerCount+=PC;} 
void StatusShow ()
 {cout<
 <
 } ;
 int main ()
 {
 METRO M (5), T;
 M.Trip () ;
 T.Trip (50) ;
 M.StatusShow ();
 M.Trip (30)    ;
 T.StatusShow ();
 M.StatusShow ();
 }

Answer - 16 : -

5 :1: 20
1 :1 : 50
5 : 2 : 50

Question - 17 : -
Find and write the output of the following C+ + program code:
Note : Assume all required header files are already being included in the program,

class product 
{
 int PID' 
float price; 
int Qty;
 public:
 Product ( ) {PID=100;Price=20;Qty=100;} 
void Add(int I, float p)
 {
 ID=I ;
 Price=P;
 }
 void Alter(int Change,int TQ)
 {
 Price+=Change;
 Qty+=TQ;
 }
 void Display ( )
 {
 cout<<"PID:"<
cout<
 }
 };
 void main ( )
 {
 Product P,Q,R;
 P. Add(104,50);
 Q. Add(201,30) ;
 R. Alter(-10,25) ;
 P. Display();
 Q. Display();
 R. Display();
 }

Answer - 17 : -

PID: 100 
104@150 
PID:29 201
@130 PID:25 
100@10

Question - 18 : -
Answer the questions (i) and (ii) after going through the following class :

class Motor 
{
int MotorNo. Track; 
public :
Motor ( );    //Function 1
Motor (int MN) ; //Function 2 
Motor (Motor &M); //Function 3 
void Allocate ( ) ///Function 4 
void Move ( ); //Function 5
} ;
void main ( )
{
Motor M;
}
(i) Out of the following, which of the option is correct for calling Function 2?
Option 1 – Motor N (M);
Option 2 – Motor P (10);
(ii) Name the feature of Object Oriented
Programming, which is illustrated by Function 1, Function 2 and Function 3 combined together.   

Answer - 18 : -

(i) Option 2 is correct for calling Function 2. [1]
(ii) Function overloading, i.e., Polymorphism. [1]

Question - 19 : -
Answer the questions (i) and (ii) after going through the following class :

class Race 
{
int CarNo, Track; 
public:
Race ( ) ; //Function 1
Race (int CN) ; //Function 2
Race (Race &R);  //Function 3
void Register ( ); //Function 4
void Drive ( ); //Function 5
};
void main ( )
{
Race R ;
:
}
(i) Out of the following, which of the option is correct for calling Function 2?
Option 1 – Race T (30);
Option 2 – Race U (R);
(ii)  Name the feature of Object Oriented
Programming, which is illustrated by Function 1, Function 2 and Function 3 combined together.  

Answer - 19 : -

(i) Option 1 – Race T (30); is correct since the parameter to T is integer.    [1]
(ii) When Functions – Function 1, Function 2 and Function 3 are combined together, it is referred as constructor overloading, i.e., Polymorphism.    [1]

Question - 20 : -
What is function overloading ? Write an example using C++ to illustrate the concept of function overloading.

Answer - 20 : -

Function Overloading
In C++, we can declare different functions with same name. This property is called function overloading. Function overloading implements polymorphism.
Example:
# include
#include < stdlib.h>
#include < conio.h>
#define pi 3.14

class fn    [1]
{
public:
void area(int); //circle 
void area(int,int); //rectangle
};
void fn : : area(int a)
{
cout <<"Area of Circle:"<
}
void fn::area (int a, int b)
{
cout <<"Area of rectangle:"<
}
void main ( )
{
int ch; 
int a, b, r; 
fn obj;
cout<<''\nl.Area    of Circle\
n2.Area of Rectangle\ n3.Exit\n:"; 
cout<<''Enter your Choice:"; 
cin>>ch; 
switch(ch)
{
case 1: 
cin>>r; 
obj.area(r);
break;    [1]
case 2:
'cin>>a>>b;
obj.area(a,b); 
break; 
case 3:
exit (0);
}
getch ( );
}

×