NCERT Solutions for Class 12 Computer Science (C++) – Implementation of OOP Concepts in C++
Question 1:
Observe the following C++ code and answer the question (i) and
(ii): [Delhi, 2015]
{
long PNR;
char TName [20];
public :
Traveller ( ) //Function 1
{cout<<"Ready"<<endl;}
void Book (long P, char N [ ])
//Function 2
{PNR = P, strcpy (TName, N);} void print () //Function 3
{cout<<PNR << TName <<endl;} ~Traveller ( ) //Function 4
{cout<<"Booking cancelled!"<<endl;
} ;
(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:
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.
(1/2 Mark for writing Function 4 or- Traveller ())
(1/2 Mark for referring Destructor)
[CBSE Marking Scheme 2015]
Question 2:
Write the definition of a class PIC in C++ with following description : [Delhi, 2015]
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 per
category // 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:
{
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<<Pho<<Category<<Location<<endl;
}
(1/2 Mark for correct syntax for class header)(1/2 Mark for correct declaration of data members)
(1 Mark for correct definition of FixLocation ( ))
(1 Mark for correct definition of Enter ()
with proper invocation of FixLocation () function)
(1 Mark for correct definition of See All( ))
NOTE:
• Deduct % Mark if FixLocation ( ) is not invoked properly inside Enter ( ) function
• No marks to be deducted for defining Member Functions inside the class
• strcmp () strcmpi () acceptable [CBSE Marking Scheme 2015]
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. [O.D, 2012]
Answer:
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
}
Short Answer Type Questions-II [3 marks each]
Question 1:
Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included. [O.D, 2016]
class BOOK
{
long Code;
char Title[20];
float Price;
Public:
BOOK ( )
//Member Function 1
{
cout < <"Bought"< < endl;
Code=10;strcpy(Title,"NoTitle")
;Price=100;
}
BOOK(int C, char T[], float P) //Member Function 2
{
Code=C;
strcpy(Title,T);
Price=P;
}
void Update(float P)
//Member Function 3
{
Price+=P;
}
void Display ( )
//Member Function 4
{
cout<<Code<<":"<<Title<<":"<<Pr ice<<endl;
}
-BOOK()
//Member Function 5
{
cout<<"Book Discarded!"<<endl;
}
} ;
void main()
//Line 1
{
//Line 2
BOOK B, C(101,"Truth",350);
//Line 3
for (int I=0;I<4;I++)
//Line 4
{
//Line 5
B.Update(50);C.Update(20);
//Line 6
B.Display 0 ;C.Display() ;
//Line 7
}
//Line 8
}
//Line 9
(i) Which specific concept of object oriented programming out of the following is illustrated by Member Function 1 and Member Function 2 combined together ?
> Data Encapsulation
> Polymorphism
> Inheritance
> Data Hiding
(ii) How many times the message “Book Discarded!” will be displayed after executing the above C+ + code ? Out of Line 1 to Line 9, which line is responsible to display the message “Book Discarded!”?
Answer:
(i) Polymorphism
(1 Mark for mentioning the correct concept name)
(ii) 2 times
Line 9.
(1/2 Mark for writing correct number of times)
OR
(1/2 Mark for writing – “No execution due to wrong syntax in Line 3”)
OR
Any other equivalent answer conveying similar meaning
(1/2 Mark for writing correct line number)
[CBSE Marking Scheme 2016]
Question 2:
Write the definition of a class CITY in C + + with following description : [O.D, 2016]
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:
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 <<Pop<<KM<< Density;
if (Density>10000)
cout<<"Highly populated city";
}
void CITY :: Dencal()
{
Density=Pop/KM;
}
Question 3:
Write the definition of a class METROPOLIS in C+ + with following description : [Delhi, 2016]
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:
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 < <Mcode< <Mname< <Area< < "Popens",
if (Pop Dens > 12000) cont<<"Highly Populated Area";
}
void METROPOLIS::call Den()
{
PopDens =Mpop/Area
} [3]
Question 4:
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:
(i) Multiple
(ii) Name, City, Manager, Qty
(iii) Input (), View (), Getln (), Show ()
(iv) PRODUCT (), WHOLESALER () , SHOWROOM ()
[CBSE Marking Scheme 2016]
Question 5:
Write the definition of a function Fixpay (float Pay[], int N) in C+ +, which should modify each element of the array Salary pay N elements, as per the following rules : [O.D, 2016]
Existing
Pay Value | Required
Modification in Value |
If
less than 1,00,000 | Add
35% in the existing value |
If
>=1,00,000 and <20,000 | Add
30% in the existing value |
If
>=2,00,000 | Add
20% in the existing value |
Void Fixpay (float pay[],int N)
{
for(int i=0; i<N; i++)
{
if(pay[i] < 100000)
pay[i]+ = pay[i]*0.35;
else if (pay[i] < 200000)
pay[i]+ = pay[i]*0.30; else
pay[i]+ = pay[i]*0.20;
}
}
Long Answer Type Questions [4 marks each]
Question 1:
Define a class Candidate in C++ with the following specification : [Delhi, 2015]
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:
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<<Aggregate Marks'';
cin>>Agg_marks;
set Grade ();
}
void dispResult ()
{
cout<<"Registration No''<<Rno;
cout<<"Name''<<Cname;
cout<<"Aggregate Marks''<<Agg_
marks;
}
(1/2 mark for correct syntax for class header]
(1/2 marks for correct declaration of data members]
(1/2 mark for correct definition of the constructor Candidate ()]
[1 mark for correct definition of setGrade ()]
[1 mark for correct definition of Getdata of Getdata () with proper invocation of setGrade ()]
(1/2 mark for correct definition of dispresult]
Question 2:
Define a class Customer with the following specifications. [CBSE SQP 2015]
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.
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:
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<<Customer_no<<" name <<" "<<Qty<" "<<TotalPrice <<" "<<Netprice;
}
};
Question 3:
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:
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;
} [1]
Question 4:
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:
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]
}
}
Question 5:
Define a class CABS in C++ with the following specification:
Data Members
• CNo – to store Cab No
• Type – to store a character ‘A’, ‘B’ or ‘C’ as City Type
• PKM – to store Kilo Meter charges
• Dist – to store Distance travelled (in KM)
Member Functions
• A constructor function to initialize Type as ‘A’ and CNo as ‘1111’
• A function ChargesQ to assign PKM as per
the following table:
Type | PerKM |
A | 25 |
B | 20 |
C | 15 |
• A function ShowCab() to allow user to enter the value of Distance and display CNo, Type, PKM, PKM “‘Distance (as Amount) on screen [Delhi, 2014]
Answer:
class CABS
{
int CNo;
char Type;
int PKM;
float Dist;
CABS
{
Type='A';
CNo=llll; [1]
}
void Charges()
{
if (Type=='A')
PKM=25;
if(Type=='B')
PKM=20;
if(Type=='C')
PKM=15; PKM= 0;
} [1]
void Register ( )
{
cin>>CNo>>Type;
PKM=Charges () ; [1]
}
void ShowCab()
{
cin>>Dist;
cout<<CNo<<Type<<PKM<<PKM*Dist;
}
}; [1]
Question 6:
Define a class Tourist in C + + with the following specification:
Data Members
• CNo – to store Cab No
• CType – to store a character ‘A’, ‘B’, or ‘C’ as City Type
• PerKM – to store per Kilo Meter charges
• Distance – to store Distance travelled (in KM) Member Functions
• A constructor function to initialize CType as ‘A’ and CNo as ‘0000’
• A function CityCharges() to assign PerKM as per the following table:
CType | PerKM |
A | 20 |
B | 18 |
C | 15 |
• A function Display() to allow user to enter the value of Distance and display CNo, CType, PerKM, PerKM*Distance (as Amount) on screen. [O.D, 2014]
Answer:
Class Tourist
{
int CNo;
char CType;
int PerKM;
float Distance;
Tourist()
{
CType='A';
CNo=0000;
}
void CityCharges () [1]
{
if(CType=='A')
PerKm=20;
if(CType=='B')
PerKM=18;
if(CType=='C')
PerKM=15;
} [1]
void RegisterCab()
{
cin>>CNo>>CType;
CityCharges();
}
void Display () [1]
{
cin>>Distance;
cout<<CNo<<CType<<PerKM<< PerKM*Distance;
} [1]
Question 7:
Define a class Seminar with the following specification:
private members
Seminarld long
Topic string of 20 characters
VenueLocation string of 20 characters
Fee float
CalcFee() function to calculate Fee depending on VenueLocation
VenueLocation | Fee |
Outdoor | 5000 |
Indoor
Non-AC | 6500 |
Indoor
AC | 7500 |
Register() function to accept values for SeminarlD, Topic, VenueLocation and call CalcFee() to calculate Fee
ViewSeminar() function to display all the data members on the screen [CBSE Comptt., 2013]
Answer:
class Seminar
{
private:
long Seminarld;
char Topic [20] ;
char VenueLocation[20];
float Fee;
void CalcFeeO [1]
{
if (strcmp (VenueLocation, ''Outdoor'')==0)
Fee=5000;
else {if (strcmp (VenueLocation, ''Indoor Non-AC" ) = = 0)
Fee=6500;
else {if (strcmp (VenueLocation, ''Indoor AC'')==0)
Fee = 7500; [1]
}
public:
void Register ()
{
cin>>seminarld;
gets (Topic) ;
gets (VenueLocation);
cin>>Fee;
CalcFee () ;
} [1]
Void ViewSeminar ()
{
cout<<SeminarId;
puts (Topic);
puts (VenueLocation);
cout<<Fee;
}
}; [1]
Question 8:
Define a class Bus in C++ with the following specifications:
Data Members
• Busno—to store Bus No
• From—to store Place name of origin
• To—to store Place name of destination
• Type—to store Bus Type such as ‘O’ for ordinary
• Distance—to store the Distance in Kilometers
• Fare—to store the Bus Fare
Member Functions
• A constructor function to initialize Type as ‘O’ and Freight as 500
• A function CalcFare( ) to calculate Fare as per the following criteria
Type Fare
‘O’ 15*Distance
‘E’ 20*Distance
‘L’ 24*Distance
• A function Allocate() to allow user to enter values for BusNo, From, To, Type and Distance. [O.D, 2013]
Answer:
class Bus
{
int Busno;
char From [20];
charTo[20];
char Type;
float Distance;
float Fare, ; [1]
BUS ( )
{
Type='O';
Fare=500;
}
void CalcFare( ) [1]
{
if(Type=='O')
Fare = 15*Distance;
if (Type=='E')
Fare = 20* Distance;
if(Type=='L')
Fare = 24*Distance;
void Allocate( ) [1]
{
cout<<''Enter Bus Number"; cin>>Busno;
cout<<''Enter Source:"; gets(From);
cout<<''Enter Destination"; gets(To);
cout<<''Enter Type:"; cin>>Type;
cout<<''Enter Distance (in kms)"; cin>>Distance;
}
}; [1]
Question 9:
Define a class Tourist in C + + with the following specifications:
Data Members
• Carno—to store Bus No
• Origin—to store Place name of origin
• Destination—to store Place name
• Type—to store Car Type such as ‘E’ for Economy
• Distance—-to store the Distance in Kilometers
• Charge—to store the Car Fare
Member Functions
• A constructor function to initialize Type as ‘E’ and Freight as 250
• A function CalcCharges () to calculate Fare as per the following criteria
Type Fare
‘E’ 16* Distance
‘K’ 22* Distance
T’ 30*Distance
• A function Enter( ) to allow user to enter values for CarNo, Origin, Destination, Type and Distance. Also, this function should call CalcCharges () to calculate fare.
• A function Show() to display contents of all data members on the screen. [Delhi, 2013]
Answer:
class Tourist
{
int Carno; char Origin[20];
char Destination[20];
char Type;
float Distance;
float Charges;
Tourist ( ) [1]
{
Type='E';
int charges=250;
}
void CalcCharges( )
{
if(Types=='E')
Charges = 16*Distance; if(Type=='A')
Charges = 22‘Distance; if(Type=='L')
Charges = 30*Distance;
}
void Enter( )
{
cout<<''Enter Car Number"; cin>>Carno;
cout<<''Enter Source:"; gets(Origin);
cout<<''Enter Destination"; gets(Destination);
cout<<''Enter Type:"; cin>>Type;
cout<<''Enter Distance (in
kms) " ; [1]
cin>>Distance;
CalcCharges( );
}
void Show( )
{
cout<< "Car Number:"<<Carno;
cout<< "Origin:";
puts(Origin);
cout<< "Destination:";
puts(Destination);
cout<<"Fare:"<<Charges;
}
}; [1]
Question 10:
Define a class Hand set in C++ with the following description : Private members :
Make—of type string
Model—of type string
Price—of type long int
Rating—of type char
Public members:
Function Read_Data to read an object of Handset type.
Function Display( ) to display the details of an object of type Handset type.
Function RetPrice() to return the value of Price of an object of Handset type. [CBSE SQP, 2013]
Answer:
class Handset
{
private:
char Make[20] ;
char Model[20] ;
long int Price;
char Rating; [1]
public :
void Read_Data( )
{
gets(Make);
gets(Model);
cin>>Price; [1]
cin>>Rating;
}
void Display()
{
puts (Make); [1]
puts(Model) ;
cout<<Price;
cout<<Rating;
}
long int RetPrice( )
{
return(Price);
}
}; [1]
Question 11:
Find the output of the following program :
#include<iostream.h>
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( )
{
c o u t < < M n o < < " : " < < T r i p N o < < " : "
< < P a s s e n g e r C o u n t < < e n d l ;
} ;
void main( )
{
METRO M(5),T;
M.Trip( ) ;
T.Trip(50) ;
M.StatusShow( ) ;
M.Trip(30) ;
T.StatusShow( );
M.StatusShow( );
} [O.D, 2012]
Answer:
5:1:20
1:1:50
5:2:50 [4]
Question 12:
Define a Class RESTRA in C++ with following description:
Private members
• Food Code of type int
• Food of type string
• FType of type String
• Sticker of type String
• A Member function GetSticker( ) to assign the following values for Food Sticker as per the given FType:
FType Sticker
Vegetarian GREEN
Contains egg YELLOW
Non-Vegetarian RED
Public Members
• A function GetFood() to allow user to enter values for FoodCode, Food, FType and call function GetSticker() to assign Sticker.
• A function ShowFood( ) to allow user to view the content of all the data members. [O.D, 2012]
Answer:
class RESTRA
{
char FoodCode;
charFood[20] ,FType [20] ,Stick er[20];
void GetSticker()
{
if (strcmp (FType, ' 'Vegetari an")==0)
{
strcpy(Sticker,"GREEN");
} [ ]
if (strcmp(FType,''ContainsEgg")==0)
{
strcpy(Sticker, "YELLOW");
}
if (strcmp(FType,''Non-Vegetarian") ==0)
strcpy(Sticker,''RED");
}
public: [1]
void GetFood()
{
cout<<"Enter FoodCode, Food,
Food Type";
cin>>FoodCode;
gets(Food);
gets(FType);
GetSticker( );
} [1]
void ShowFood( )
{
cout<<''Enter Food Code, Food,
FoodType, Sticker:";
cout < < FoodCode;
puts(Food);
puts(FType);
puts(Sticker);
> ,
}; [1]
Question 13:
Find the output of the following program :
#include<iostream.h>
class TRAIN {
int Tno, TripNo, PersonCount, T, N;
public:
Train(int Tmno=l)
{ Tno=Tmno; TripNo=0; PersonCount = 0; }
void Trip (int TC =100)
{TripNo++; PersonCount =TC;}
void show() {cout<<Tno<<'' : ''<<TripNo<<'' :
"<<PersonCoun<<endl;}
} ;
void main( )
{
Train T(10), N;
N.Trip();
T.show();
N.Trip(70) ;
N.Trip(4 0) ;
N.show( );
T.show( );
} [Delhi, 2012]
Answer:
10:0:0
1:2:140
10:1:70 [4]
Question 14:
Define a class Candidate in C++ with the
following description:
Private members
• A data member RNo(Registration Number) of type long
• A data member Name of type String
• A data member Score of type float
• A data member Remarks of type String
• A member function AssignRem() to assign the remarks as per the score obtained by the candidate.
• Scor6 range and the respective remarks are shown as follow:
Score Remarks
>=50 Selected
<50 Not Selected
Public members
• A function ENTERQ to allow user to enter values for RNo, Names, Score and call function AssignRem() to assign the remarks.
• A function display() to allow user to view the content of all data members. [Delhi, 2012]
Answer:
class Candidate
{
long RNo;
char Name[40];
float Score;
[1]
char Remarks[20]
void AssignRem()
if (Score>=50) [1]
strcpy(Remarks, "Selected"); else
strcpy(Remarks,''Not Selected");
}
public:
void Enter()
{ [1]
cout<<''Enter the values for RNo, Name, Score";
cin>>RNo;
gets(Name);
cin>>Score;
AssignRem( ); [1]
}
void Display( )
{
cout<<RNo<<Score;
puts(Name); puts(Remarks);
}
}; [1]
Question 15:
Define a class Applicant in C++ with the following description:
Private members
• A data member ANo(Admission Number) of type long
• A data member Name of type String
• data member Agg(Aggregate Marks) of type float
• A data member Grade of type char
• A member function GradeMe() to assign the grades as per the aggregate obtained by the candidate.
• Score range and the respective remarks are shown as follow:
Aggregate Grade
>=80 A
less than 80 and > = 65 B
less than 65 and > = 50 C
less than 50 D
Public members
• A function ENTER( ) to allow user to enter values for ANo, Names, Aggregate and call function GradeMe () to assign grade.
• A function RESULT( ) to allow user to view the content of all data members. [O.D, 2012]
Answer:
class Applicant
{
long ANo;
char Name[40];
float Agg;
char Grade;
void GradeMe ( ) [1]
{
if (Agg>80)
Grade='A';
if (Agg<80 && Agg>=65)
Grade='B';
if (Agg<65 && Agg>=50)
Grade='C'; if(Agg<50)
Grade='D';
}
public:
void Enter ( )
{
cout<<''Enter the values for Ano, Name, Agg" ;
cin>>ANo;
gets(Name);
cin>>Agg; [1]
GradeMe( );
}
void Result( )
{
cout<<ANo<<Agg;
puts(Name); puts(Grade);
}
}; [1]
Question 16:
Define a class ITEM in C++ with the following description:
Private Members
• Code of type integer (Item Code)
• Iname of type string (Item Name)
• Price of type float (Price of each item)
• Qty of type integer (Quantity of item in stock)
• Offer of type float (Offer percentage on the item)
• A member function GetOffer( ) to calculate Offer percentage as per the following rule :
If Qty < = 50 Offer is 0
If 50 < Qty < = 100 Offer is 5
If Qty >100 Offer is 10
Public Members
• A function GetStock() to allow user to enter values for Code, Iname, Price, Qty and call function GetOffer() to calculate the offer.
• A function ShowItem( ) to allow user to view the content of all the data members.
Answer:
class Item
{
int Code;
char Iname [20];
float Price;
int Qty;
float Offer;
void GetOffer( ) ;
public:
void GetStock ()
{
cin>>Code;
gets (Iname) ; // OR cin.
getline (Iname, 80); Or cin>>Iname;
cin>>Price>>Qty;
GetOfferO ;
}
void Showltem ()
{
cout<<Code<<Iname<<Price<<Qty<<Offer;
};
void Item : GetOffer ()
{
if (Qty < =50)
Offer = 0;
else if (Qty <= 100)
Offer = 5; //OR Offer = 0.05;
else
Offer = 10; //OR Offer = 0.1;
}
(1/2 Mark for correct syntax for class header)
(1/2 Mark for correct declaration of data members) (1 Mark for correct definition of Get OfferQ)
(1 Mark for correct definition of Get Stock () with proper invocation of Get Offer () function)
(1 Mark for correct definition of ShowltemO)
Note : Deduct 1/2 Mark if Get Offer () is not invoked properly inside Stock Ofunction.
Question 17:
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<Qty,=100 Discountis 5
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:
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<<Price<<'\t' <<Qty<<'\
t1<<Discount<<endl ;
}
(1/2 Mark for correct syntax for class header)
(1/2Mark for correct declaration of data members)
(1 Mark for correct definition of FindDiscO)
(1 Mark for correct definition of Buy() with proper invocation of Find Disc() function)
(1 Mark for correct definition of Show All())
Note : Deduct 1/2 Mark if Find Disc() is not invoked properly inside Buy() function.
TOPIC-2 Objects
Short Answer Type Questions-1 [2 marks each]
Question 1:
Answer the questions (i) and (ii) after going through the following class :
class Hospital
{
int Pno, Dno;
class Hospital
public :
Hospital (int PN); //Function 1
Hospital (); //Function 2
Hospital (Hospital &H); //Function 3
void In (); //Function 4
void Disp (); //Function 5
} ;
void main()
{
Hospital H(20); //Statement 1
}
(i) Which of the functions out of Function 1, 2, 3, 4 or 5 will get executed when the Statement 1 is executed in the above code?
(ii) Write a Statement to declare a new object G
with reference to already existing object H using Function 3. [O.D, 2014]
Answer:
(i) Function 1 will get executed. [1]
(ii) Hospital G(H); [1]
Question 2:
Answer the questions (i) and (ii) after going through the following class :
class Health
{
int PId, Did; public:
Health (int PPID); //Function 1
Health (); //Function 2
Health (Health &H); //Function 3
void Entry (); //Function 4
void Display (); //Function 5
};
void main ( )
{
Health H(20); //Statement 1
}
(i) Which of the Function out of Function 1, 2, 3, 4 or 5 will get executed when the Statement 1 is executed in the above code ?
(ii) Write a statement to declare a new object G
with reference to already existing object H using Function 3. [Delhi, 2014]
Answer:
(i) Function 1 will get executed. [1]
(ii) Hospital G (H); [1]
Question 3:
What is the relationship between a class and an object? Illustrate with a suitable example. [CBSE Comptt., 2013]
Answer:
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<<length<<breadth<<height;}
};
void main()
{Box a=new Box();
a. Display(10,20,30);
// a is the object of class Box [1]
}
NCERT Solutions for Class 12 Computer Science
0 Comments
Please Comment