CBSE Class 12 Computer Science (C++) Chapter 3 Classes and Objects Important Questions – Free PDF Download
Free PDF download of Important Questions for CBSE Class 12 Computer Science (C++) Chapter 3 Classes and Objects prepared by expert Computer Science (C++) teachers from latest edition of CBSE(NCERT) books, On CoolGyan.Org to score more marks in CBSE board examination.
CBSE Class 12 Computer Science (C++) Important Questions
Chapter 3 Classes and Objects
Previous years Examination Questions
2 Marks Questions
Question 1:
What is the relationship between a class and an object? Illustrate with a suitable example. Delhi 2013C
Аnswers:
Class
• It is a user-defined data type.
• It is a logical framework that combines data and function into a single unit.
e.g
class GABS { int a,b; public: void set() { a=10; b=20; } void show() { cout<<"Value of a and b is"<<a<<b<<endl; };
Object
• It is an instance of a class.
• It is a physical entity that means it occupies space in a memory. It’s used to access members of the class.
e.g. Suppose “GABS” is the name of the class.
GABS G1, G2;
G1.show( );
Both G1 and G2 are the objects of the class “GABS”
Question 2:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction. Delhi 2012
#include<iostream.h> class Book { long Bld.Qty; public: void Purchase() { cin>>BId<<Qty; } void Sale { cout<<setw(5)<<BId<<"o1d:”<<Qty<<endl; cout<<"New:"<<—Qty<<endl; } }; void main() { Book B; B.Purchase(); Sale(); B.Sale(); }
Аnswers:
#include<iostream.h> #include<iomanip.h> class Book { long Bid, Qty; pub!ic: void Purchase() { cin>>BId>>Qty; } void Sale() { cout<<setw(5)<<BId<<"o1d:"<<Qty<<endl; cout<<"New:"<<--Qty<<endl; } }; void main() { Book B; B.Purchase(); B.Sale(); B.Sale(); }
Question 3:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction. Delhi 2012
#include<iostream.h> class Item { long IId, Qty; public: void Purchase { cin>>IId>>Qty; } void Sale() { cout<<setw(5)<<IId<<”old:"<<Qty<<endl; cout<<"New: "<<--Qty<<endl; } }; void main() { Item I; Purchase(): I.Sale(); I.Sale(); }
Аnswers:
#include<iostream.h> #include<iomanip.h> class Item { long lid,Qty; public: void Purchase() { cin>>IId>>Qty; } void Sale() { cout<<setw(5)<<IId<<"01d:"<<Qty<<endl; cout<<"New:"<<--Qty<<endl; } }; void main() { Item I; I.Purchase(); I.Sale(); I.Sale(); }
Question 4:
Rewrite the following program after removing the syntactical error(s) (if any). Underline each correction. Delhi 2012C
#include<iostream.h> #include<stdio.h> Class OFFER { int OfferId; char Description[80]: void Show { cout<<offerId<<" : ”<<Description<<endl; } public: void Enter { cin>>offerId; gets>>Description; } }; void main() { OFFER Obj; Obj.Enter(); Obj.Show(); }
Аnswers:
#include<iostream.h> #include<stdio.h> class OFFER { int OfferId; char Description[80]; void Show() { cout<<offerld<<":"<<Description<<endl; } public: void Enter() { cin>>offerId; gets(Description): } }; void main() { OFFER Obj; Obj.Enter(); //Obi.Show(); //Show()cannot be called because //it is a private member }
Question 5:
What is the difference between members in private visibility mode and the members in public visibility mode inside a class? Also give a suitable C++ code to illustrate both. Delhi 2012
or
Differentiate between public and private visibility modes in context of object oriented programming using a suitable example. Delhi 2011
Аnswers:
A member declared as private remains hidden from outside world and it can only be accessed by the member function of the class in which it is declared. A member declared as public is made available to the outside world. That is , it can be accessed by any function, any expression in the program but only by using an object of the same class type.
e.g-
class A { private: int x; void show(); public: int y; void get(); };
The members x and show( ) are private and these are not accessible outside the class where the members y and get( ) are public so these are accessible outside the class.
Question 6:
When is public visibility of a mode applied to members of a class? Also give an example to illustrate. Delhi 2011C
Аnswers:
When we need to access the members from out side the class then we apply public mode preceding any members.
e.g.
#include<iostream.h> class ABC { public: int b; void show() { cout<<"b ="<<b; } }; void main() { ABC x; x.b=20; x.show(); }
Question 7:
Rewrite the following C++ program code after removing the syntax error(s) (if any). Underline each correction.
All India 2010
include<iostream.h> class FLIGHT { long FlightCode; char Description[25]; public void Addlnfo() { cin>>FlightCode; gets(Description); } void ShowInfo() { cout<<FlightCode«":" <<Description<<endl; } }; void main() { FLIGHT F; Addlnfo.F(); Showlnfo.F(); }
Аnswers:
# include<iostream.h> #include<stdio.h> class FLIGHT { long FlightCode; char Description[25]; public: void Addlnfo() { cin>>FlightCode; gets(Descri pti on); } void Showlnfo() ( cout<<FlightCode<<":"<<Description<<endl; } }; void main() { FLIGHT F; F.Addlnfo(); F.ShowInfo(); }
Question 8:
Rewrite the following C++ program code after removing the syntax error(s) (if any). Underline each correction.
Delhi 2010
#include<iostream.h> class TRAIN { long TrainNo; char Description[25]; public void Entry() { cin>>TrainNo; gets(Description); } void Display() { cout<<TrainNo<<":"<<Description<<endl; } }; void main( ) { TRAIN T; Entry. T(); Display.T(); }
Аnswers:
#include<iostream.h> #include<stdio.h> class TRAIN { long TrainNo; char Description[25]; public: void Entry() { cin>>TrainNo; gets(Description); } void Display() { cout<<TrainNo<<" : "<<Description<<endl; } }; void main() { TRAIN T; T.Entry(); T.Display(); }
Question 9:
Rewrite the following program after removing the syntactical error(s) (if any) Underline each correction. Delhi 2009C
#include<iostream.h> class Transport { char Model[20]; char Name[20]; void Get() { gets(Model); gets(Name); } void Show() { cout<<Model<<endl; puts(Name); } }; void main( ) { Transport T; T.Get(); Show(); }
Аnswers:
#include<iostream.h> #include<stdio.h> class Transport { char Model[20]; char Name[20]; public: void Get() { gets(Model); gets(Name); } void Show() { cout<<Model<<endl; puts(Name); } }; void main() { Transport T: T.Get(); T.Show(); }
4-5 Marks Questions
Question 10:
Write the definition of a class BOX in C++ with the following description: All India 2017
Аnswer:
class Box { int BoxNumber; float Side, Area; void ExecArea() { Area = Side*Side; } public: void GetBox() { cout<<"Enter the values of BoxNumber and Side”; cin>>BoxNumber; cin>>Side; ExecArea(); } void ShowBox() { cout<<"BoxNumber:"<<BoxNumber<<endl; cout<<”Side:"<<Side<<endl: cout<<”Area:"<<Area; }
Question 11:
Write the definition of a class METROPOLIS in C++ with following description: Delhi 2016
Аnswer:
class METROPOLIS { int MCode; char MName[20]; long int MPop; float Area, PopDens; void CalDen() { PopDens = MPop/Area; } public: void Enter() { cout<<" Enter the code"; cin>>MCode; cout<<" Enter the name"; gets(MName); cout<<" Enter the population"; cin>>MPop; cout<<" Enter the area coverage"; cin>>Area; CalDen(); } void ViewALL() { cout<<" Code: "<<MCode; cout<<" Name: "<MName; cout<<" Population:"<<MPop; cout<<" Area Coverage : "<<Area ; cout<<" Population Density :"<<PopDens; if(PopDens>12000)cout<<"Highly Populated Area"; } };
Question 12:
Write the definition of a class CITY in C++ with following description: All India 2016
Аnswer:
class CITY { int Ccode; char CName[20]; long int Pop; float KM, Density; void DenCal() { Density = Pop/KM; } public : void Record() { cout<<" Enter the city code" cin<<Ccode; cout<<" Enter the city name" gets(CName); cout<<" Enter city’s populiation" cin>>Pop; cout<<" Enter the Area coverage by city"; cin>>KM; DenCal (); } void View() { cout<<" City code:"<<Ccode; cout<<" City name:"<<CName; cout<<" Popul at ion:"<<Pop; cout<<" Area coverage:"<<KM; cout<<" Popul ation density:"<<Density; if(Density>10000) cout<<"Highly Populated City"; } };
Question 13:
Write the definition of a class PIC in C++ with following description Delhi 2015
Аnswer:
class PIC { int Pno; char Category[20]; char Location[20]; void FixLocation() { if(strcmp(Category,"Antique”)==0) strcpy(Location,"Ustad Khan"); else if(strcmp(Category,"Modern" )==0) strcpy(Location,"Jim Plaq”); else if(strcmp(Category,”Classic”)==0) strcpy(Location,"Amina"); } public: void Enter() { cout<<" Enter the picture number"; cin>>Pno; cout<<" Enter the category"; gets(Category); FixLocation(); } void SeeAll() { cout<<" Picture number:"<<Pno; cout<<" Category: "<<Category; cout<<" Location:"<<Location; } };
Question 14:
Write the definition of a class Photo in C++ with following description: All India 2015
Аnswer:
class Photo { int Pno; char Category[20]; char Exhibit[20]; void FixExhibit() { if(strcmp(Category,"Antique")== 0) strcpy(Exhibit, "Zaveri"); else if(strcmp(Category,"Modem") == 0) strcpy(Exhibit, "Johnsen"); else if(strcmp(Category,"Classic") == 0) strcpy(Exhibit, "Terenida”); } public: void Register() { cout<<" Enter the photo number"; cin>>Pno; cout<<" Enter the category"; gets(Category); FixExhibit(); } void ViewAll() { cout<<" Photo number :"<<Pno; cout<<" Category:"<<Category; cout<<" Exhibit:”<<Exhibit; } };
Question 15:
Write the definition of a class STAFF in C++ with following description: All India 20isc
Public Members
- Enroll( ) //A function to allow user to enter values
//SID, Type, Name and call
//AssignPay( ) function - SeeData( ) //A function to display all the data members
Аnswer:
class STAFF { long int SID; char Type; float Pay; char Name[30]; void AssignPay() { if(Type == "D") Pay = 95000; else if(Type == "M") Pay = 75000; else if (Type == "E") Pay = 60000; else if(Type == "S") Pay = 45000; } public: void Enroll() { cout<<”Enter the values of staff ID, Type, Name"; cin>>SID; cin>>Type; gets(Name); AssignPay(); } void seeData() { cout<<SID<<Type<<Name<<Pay; } };
Question 16:
Define a class Seminar with the following specification private members: Delhi 2013C
Seminarld long
Topic string of 20 characters
VenueLocation string of 20 characters
Fee float
CalcFee( ) function to calculate Fee depending on VenueLocation
Venue Location | Fee |
Outdoor | 5000 |
Indoor Non-AC | 6500 |
Indoor AC | 7500 |
Public members
Register( ) function to accept values for Seminarld, Topic, VenueLocation and call CalcFee( ) to calculate Fee.
ViewSeminarf( ) function to display all the data members on the screen.
Аnswer:
class Seminar { long SeminarId; char Topic[20]. VenueLocation[20]; float Fee; void CalcFee() { if(strcmp(VenueFocation,"Outdoor")==0) Fee=5000; else if(strcmp(Venuetocation,"Indoor Non-AC")==0) Fee=6500; else if(strcmp(VenueFocation,"Indoor AC")==0) Fee=7500; } public: void Register() { cout<<"Enter Seminar Id"; cin>>SeminarId; cout<<"Enter Topic"; gets(Topic); cout<<"Enter Venue Focation"; gets(VenueLocation); CalcFee(); } void ViewSeminar() { cout<<"SeminarId:"<<SeminarId<<endl; cout<<"Topic: "<<Topic<<endl; cout<<"Venue Location;"<<VenueFocation<<endl; cout<<"Fee is:"<<Fee; } };
Question 17:
Define a class RESTRA in C++ with following description: Delhi 2012
Private members
- FoodCode of type int
- Food of type string
- FType of type string
- Sticker of type string
- A member function GetStickerf( ) 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 ShowFoodf( ) to allow user to view the content of all the data members.
Аnswer:
class RESTRA { int FoodCode; char Food[30], FType[20],Sticker[20]; void GetSticker() { if(strcmp(FType,"Vegetari an”)==0) strcpy(Sticker,"GREEN"); else if(strcmp(FType,"Contains Egg")==0) strcpy(Sticker,"YELLOW"); else if(strcmpCFType,"Non-Vegetarian")==0) strcpy(Sticker,"RED”); } public: void GetFood() { cout<<"Enter FoodCode, Food, Food Type"; cin>>FoodCode; gets(Food); gets(FType); GetSticker(); } void ShowFood() { cout<<"Entered values of FoodCode, Food, FoodType,Sticker"; cout<<FoodCode<<Food<<FType<<Sticker; } };
Question 18:
Define a class SUPPLY in C++ with following description: All Indio 2012
Private members
- Code of type int
- FoodName of type string
- Sticker of type string
- FoodType of type string
- A member function GetTypef ( ) to assign the following values for FoodType as per the given Sticker
Sticker FoodType
GREEN Vegetarian
YELLOW Contains Egg
RED Non-Vegetarian
Public members
- A function FoodIn( ) to allow user to enter values for Code, FoodName, Sticker and call function GetType( ) to assign respective FoodType.
- A function FoodOut( ) to allow user to view the content of all the data members.
Аnswer:
class SUPPLY { int Code; char FoodName[30],Sticker[30]; char FoodType[20]; void GetType() { if(strcmp(Sticker, "GREEN”)==0) strcpy(FoodType,"Vegetarian" ); else if(strcmp(Sticker,"YELLOW")==0) strcpy(FoodType,"Contains Egg"); else if(strcmp(Sticker,"RED")==0) strcpy(FoodType,"Non -Vegeta rian"); } public: void FoodIn() { cout<<"Enter Code, FoodName, Sticker"; cin>>Code; gets(FoodName); gets(Sticker); GetType(); } void Food0ut() { cout<<"Code,FoodName,Sticker,FoodType are"; cout<<Code<<FoodName<<Sticker<<FoodType; } };
Question 19:
Define a class Candidate in C++with following description: Delhi 2o11
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 a candidate.
- Score range and the respective remarks are shown as follow:
Score Remarks
>=50 Selected
less than 50 Not Selected
Public members
- A function ENTER( ) to .allow user to enter values for RNo, Name, .Score and call function AssignRem( ) to assign Remarks.
- A function DISPLAY( ) to allow user to view the content of all data members.
Аnswer:
class Candidate { long RNo; char Name[40]; float Score; char Remarks[20]; void AssignRem() { if(Score>=50) strcpy(Remarks,"Selected"); else strcpy(Remarks,"Not Selected"); } public: void ENTER() { cout<<"Enter the values for Rno,Name,Score”; cin>>RNo; gets(Name); cin>>Score; AssignRem(); } void DISPLAY() { cout<<RNo<<Name<<Score<<Remarks; } };
Question 20:
Define a class Applicant in C++ with following description: All IndiA 2011
Private members
- A data member ANo (Admission Number) of type long
- A data member Name of type string
- A data member Agg (Aggregate Marks) of type float
- A data member Grade of type char
- A member function GradeMe( ) to find the grade as per the aggregate marks obtained by a student. Equivalent aggregate marks range and the respective grades are shown as follow:
Aggregate marks 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, Name, Agg and call function GradeMe( ) to find the Grade.
- A function RESULT( ) to allow user to view the content of all data members.
Аnswer:
class Applicant { long ANo; char Name[40]; float Agg; char Grade; void GradeMe() { if(Agg>=80) Grade="A"; else if(Agg<80 && Agg>=65) Grade="B"; else if(Agg<65 && Agg>=50) Grade="C"; else Grade="D"; public: void ENTER() { cout<<"Enter the values for Ano.Name.Agg" cin>>ANo; gets(Name); cin>>Agg; GradeMe(); } void RESULT() { cout<<ANo<<Name<<Agg<<Grade; } };
Question 21:
Define a class ITEM in C++ with the following specification: Delhi 2010
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 stock)
- Offer of type float(Offer percentage on the item)
- A member function GetOffer( ) to calculate offer percentage as follows:
If Qty <=50 Offer is 0
If 50<Qtv<=l 00 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 offer.
- A function ShowItem( ) to allow user to view the content of all data members.
Аnswer:
class ITEM { int Code; char Iname[20]: float Price; int Qty; float Offer; void GetOffer() { if(Qty<=50) Offer=0; else if(Qty<=100) Offer=5; else Offer=10; } public: void GetStock() { cout<<"Enter the values for code, Item name, Price, Quantity"; cin>>Code; gets(Iname); cin>>Price>>Qty; GetOffer(); } void ShowItem() { cout<<Code<<Iname<<Price<<Qty<<Offer; } };
Question 22:
Define a class STOCK in C++ with the following specification: All India 2010
Private members
- 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 percentage as follows:
If Qty <=50 Discount is 0
If 50<Qty<=100 Discount is 5
If Qty >100 Discount is 10 Public members
- A function Buy( ) to allow user to enter values for ICode, Item, Price, Qty and call function FindDisc( ) to calculate discount.
- A function ShowAll( ) to allow user to view the content of all data members.
Аnswer:
class STOCK { int ICode; char Item[20]: float Price: int Qty; float Discount: void FindDisc(): { if(Qty<=50) Discount=0; else if(Qty<=100) Discount=5; else Discount=10; } public: void Buy() { cout<<"Enter the values for Icode,Item,Price,Qty"; cin>>ICode; gets(Item); cin>>Price>>Qty; FindDisc(); } void ShowAll() { cout<<ICode<<Item<<Price<<Qty<<Discount; } };
Question 23:
Define a class RESORT in C++ with the following specification: Delhi 2009
Private members
- Rno – Data member to store room number
- Name – Data member to store customer name
- Charges – Data member to store per day charges
- Days – Data member to store number of days of stay
- COMPUTEO – Function to calculate and return amount as days*charges and if the value of days *charges is more than 11000, then as 1.02*Days *Charges
Public members
- Getinfo( ) Function tfo enter the content Rno, Name, Charges and Days
- Dispinfo( ) Function to display Rno, Name, Charges, Days and Amount (amount to be displayed by calling function COMPUTE( ))
Аnswer:
class RESORT { int Rno; char Name[30]; float Charges; int Days ; float COMPUTE() { float temp=Days*Charges; if(temp>11000) return(1.02*temp); return temp; } public: void Getinfo() { cout<<"enter the room number:"; cin>>Rno; cout<<"enter the customer name:"; gets(Name); cout<<”enter the room charges per day"; cin>>Charges; cout<<"enter number of days stayed by customer:"; cin>>Days: } void Dispinfo() { cout<<"Room number:"<<Rno; cout<<"Customer Name:"; puts(Name) ; cout<<"Charges per day:"<<Charges; cout<<"Number of days stayed by customer:"<<Days; cout<<"Total charges of customer: "<<COMPUTE(); } };
Question 24:
Define a class HOTEL in C++ with the following specification: All IndiA 2009
Private members
- Rno Data member to store room number
- Name Data member to store customer name
- Tariff Data member to store per day charges
- NOD Data member to store number of days of stay
- CALC( ) Function to calculate and return amount as NOD*Tariff and if the value of NOD*Tariff is more than 10000, then as 1.05 NOD *charges
Public members
- Checkin( ) Function to enter the content Rno, Name, Tariff and NOD
- Checkout( ) Function to display Rno, Name, Tariff, NOD and Amount (amount to be displayed by calling function
CALC( ))
Аnswer:
class HOTEL { int Rno,NOD; char Name[30]; float Tariff; float CALC() { float temp=NOD*Tariff; if(temp>10000) return(1.05*temp); return temp; } public: void Checkin() { cout«"Enter the room number:"; cin>>Rno; cout<<"Enter the customer name:"; gets(Name); cout<<"Enter the room charges per day"; cin>>Tariff ; cout<<"Enter number of days stayed by customer:"; cin>>NOD; } void Checkout() { cout<<"Room number:"<<Rno; cout<<"Customer Name:"; puts(Name); cout<<"Charges per day:"<<Tariff; cout<<"Number of days stayed by customer:"<<NOD; cout<<"Total charges of customer:"<<CALC(); } };