CBSE Class 12 Computer Science HOTs Programming in C++ Four mark Questions

Programming in C++
Q.1. Define a class TEST in C++ with following description:
Private Members
• TestCode of type integer
• Description of type string
• NoCandidate of type integer
• CenterReqd (number of centers required) of type integer
• A member function CALCNTR() to calculate and return the number of centers as
(NoCandidates/100+1)
Public Members
• A function SCHEDULE() to allow user to enter values for TestCode, Description,
NoCandidate & call function CALCNTR() to calculate the number of Centres
• A function DISPTEST() to allow user to view the content of all the data members

Ans.1. class TEST
{
int TestCode;
char Description[20];
int NoCandidate,CenterReqd;
void CALCNTR();
public:
void SCHEDULE();
void DISPTEST();
};
void TEST::CALCNTR()
{
CenterReqd=(NoCandidate/100 + 1);
}
void TEST::SCHEDULE()
{
cout<<"Test Code :";cin>>TestCode;
cout<<"Description :";gets(Description);
cout<<"Number :";cin>>NoCandidate;
CALCNTR();
}
void TEST::DISPTEST()
{
cout<<"Test Code :"<<TestCode<<endl;
cout<<"Description :"<<Description<<endl;
cout<<"Number :"<<NoCandidate<<endl;;
cout<<"Centres :"<<CenterReqd<<endl;;
}

Q.2 Define a class in C++ with following description:
Private Members
• A data member Flight number of type integer
• A data member Destination of type string
• A data member Distance of type float
• A data member Fuel of type float
• A member function CALFUEL() to calculate the value of Fuel as per the
following criteria:
Distance Fuel
<=1000 500
more than 1000 and <=2000 1100
More than 2000 2200
Public Members
" A function FEEDINFO() to allow user to enter values for Flight Number,
Destination, Distance & call function CALFUEL() to calculate the quantity of Fuel
" A function SHOWINFO() to allow user to view the content of all the data members

Ans.2. class FLIGHT
{
int Fno;
char Destination[20];
float Distance, Fuel;
void CALFUEL();
public:
void FEEDINFO();
void FEEDINFO();
void SHOWINFO();
};
void FLIGHT::CALFUEL()
{
if (Distance<=1000)
Fuel=500;
else
if (Distance<=2000)
Fuel=1100;
else
Fuel=2200;
}
void FLIGHT::FEEDINFO()
{
cout<<"Flight No :";cin>>Fno;
cout<<"Destination :";gets(Destination);
cout<<"Distance :";cin>>Distance;
CALFUEL();
}
void FLIGHT::SHOWINFO()
{
cout<<"Flight No :"<<Fno<<endl;
cout<<"Destination :"<<Destination<<endl;
cout<<"Distance :"<<Distance<<endl;;
cout<<"Fuel :"<<Fuel<<endl;;
}

Q.3 Define a class Clothing in C++ with the following descriptions:
Private Members:
Code of type string
Type of type string
Size of type integer
Material of type string
Price of type float
A function Calc_Price() which calculates and assigns the value of Price as follows:
For the value of Material as “COTTON”
Type Price (Rs.)
TROUSER 1500
SHIRT 1200
For Material other than “COTTON” the above mentioned Price gets reduced by 25%.
Public Members:
A constructor to assign initial values of Code, Type and Material with the word “NOT ASSIGNED” and Size and Price with 0.
A function Enter () to input the values of the data members Code, Type, Size and Material and invoke the CalcPrice() function.
A function Show () which displays the content of all the data members for a Clothing.

Ans.3. class Clothing
{
char Code[25];
char Type[25];
int Size ;
char Material[30];
float Price;
public:
Clothing();
void Calc_Price();
void Enter();
void Show();
};
Clothing::Clothing()
{
strcpy(Code,”NOT ASSIGNED”);
strcpy(Type,”NOT ASSIGNED”);
Size=0;
strcpy(Material,”NOT ASSIGNED”);
Price=0;
}
void Clothing::Calc_Price() or void Clothing::CalcPrice()
{
if(strcmp(Type,”TROUSER”)==0 &&
strcmp(Material,”COTTON”)==0)
Price=1500;
else if (strcmp(Type,”SHIRT”)==0 &&
strcmp(Material,”COTTON”)==0)
Price=1200;
else if (strcmp(Type,”TROUSER”)==0 &&
strcmp(Material,”COTTON”)!=0)
Price=1500*0.75;
else if (strcmp(Type,”SHIRT”)==0)&& strcmp(Material,”COTTON”)!=
0)
Price=1200*0.75;
}
void Clothing::Enter()
{
gets(Code); // or cin >> Code;
gets(Type); // or cin >> Type;
cin>>Size;
gets(Material); // or cin >> Material;
Calc_Price(); OR CalcPrice();
}
void Clothing::Show()
{
cout<<Code<<Type<<Size<<Material<<Price<<endl;
}

Q.4. Define a class Travel in C++ with the description given below:
Private Members:
T_Code of type string
No_of_Adults of type integer
No_of_Children of type integer
Distance of type integer
TotalFare of type float
Public Members:
A constructor to assign initial values as follows :
T_Code with the word “NULL”
No_of_Adults as 0
No_of_Children as 0
Distance as 0
TotalFare as 0
A function AssignFare( ) which calculates and assigns the value of the data member TotalFare as follows :
For each Adult
Fare (Rs) For Distance (Km)
500 >=1000
300 <1000 & >=500
200 <500
For each Child the above Fare will be 50% of the Fare mentioned in the above table.
For example :
If Distance is 750, No_of_Adults = 3 and No_of_Children = 2
Then TotalFare should be calculated as
No_of_Adults * 300 + No_of_Children * 150
i.e. 3 * 300 + 2 * 150 = 1200
• A function EnterTraveK ) to input the values of the data members
T_Code, No_of_Adults, No_of_Children and Distance; and invoke the AssignFare( ) function.
• A function ShowTraveK) which displays the content of all the data members for a Travel.

Ans.4. class Travel
{
char TCode[5]; //OR char *Tcode;
int No_of_Adults;
int No_of_Children;
int Distance;
float TotalFare;
public:
Travel();
void AssignFare();
void EnterTravel();
void ShowTravel();
};
Travel::Travel() //Constructor
{
strcpy(TCode,”NULL”);// OR TCode[0]=’\0’ OR strcpy(TCode,”\0”)
// OR TCode=NULL if TCode is declared as char pointer
No_of_Adults = 0;
No_of_Children = 0;
Distance = 0;
TotalFare = 0;
}
void Travel::AssignFare()
{
if(Distance>=1000)
TotalFare = 500*No_of_Adults+250*No_of_Children;
else
if (Distance >= 500)
TotalFare = 300*No_of_Adults+150*No_of_Children;
else
TotalFare = 200*No_of_Adults+100*No_of_Children;
}
void Travel::EnterTravel()
{
gets(TCode); // or cin >> TCode;
cin>>No_of_Adults>>No_of_Children>>Distance;
AssignFare();
}
void Travel::ShowTravel()
{
cout<<TCode<<No_of_Adults<<No_of_Children
<<Distance<<TotalFare<<endl;
}

Q.5 Answer the questions (i) to (iv) based on the following code :
class CUSTOMER
{
int Cust_no;
char Cust_Name[20];
protected:
void Register();
public:
CUSTOMER();
void Status();
};
class SALESMAN
{
int Salesman_no;
char Salesman_Name[20];
protected:
float Salary;
public:
SALESMAN();
void Enter();
void Show();
};
class SHOP : private CUSTOMER , public SALESMAN
{
char Voucher_No[10];
char Sales_Date[8];
public:
SHOP();
void Sales_Entry();
void Sales_Detail();
}
(iii) Write the names of data members which are accessible from objects belonging to class CUSTOMER.
(iv) Write the names of all the member functions which are accessible from objects belonging to class SALESMAN.
(v) Write the names of all the members which are accessible from member functions of class SHOP.
(iv) How many bytes will be required by an object belonging to SHOP?

Ans.5. (i) None of data members are accessible from objects belonging to class
AUTHOR.
(ii) Enter(), Show()
(iii) Data members: Voucher_No, Sales_Date, Salary
Member
function:Sales_Entry(),Sales_Detail(),Enter(),Show(),Register(),
Status()
(iv) 66

Q.6. Answer the questions (i) to (iv) based on the following:
class PUBLISHER
{
char Pub[12];
double Turnover;
protected:
void Register();
public:
PUBLISHER();
void Enter();
void Display();
};
class BRANCH
{
char CITY[20];
protected:
float Employees
public:
BRANCH();
void Haveit();
void Giveit();
};
class AUTHOR : private BRANCH , public PUBLISHER
{
int Acode;
char Aname[20];
float Amount;
public:
AUTHOR();
void Start();
void Show();
};
(i) Write the names of data members, which are accessible from objects belonging to class AUTHOR.
(ii) Write the names of all the member functions which are accessible from objects belonging to class BRANCH.
(iii) Write the names of all the members which are accessible frommember functions of class AUTHOR.
(iii) How many bytes will be required by an object belonging to class
AUTHOR?

Ans.6. (i)None of data members are accessible from objects belonging to class 4
AUTHOR.
(ii) Haveit(), Giveit()
(iii) Data members: Employees, Acode, Aname, Amount
Member function: Register(), Enter(), Display(), Haveit(), Giveit(), Start(),
Show(),
(iv) 70

Q.7 Answer the questions (i) to (iv) based on the following code:
class Dolls
{
char DCode[5];
protected:
float Price ;
void CalcPrice(float);
public:
Dolls( );
void DInput( );
void DShow( );
};
class SoftDolls: public Dolls
{
char SDName[20];
float Weight;
public:
SoftDolls( );
void SDInput( );
void SDShow( );
};
class ElectronicDolls: public Dolls
{
char EDName[20];
char BatteryType[10];
int Battieries;
public:
ElectronicDolls ( );
void EDInput( );
void EDShow( );
};
(i) Which type of Inheritance is shown in the above example?
(ii) How many bytes will be required by an object of the class ElectronicDolls?
(iii) Write name of all the data members accessible from member functions of the class SoftDolls.
(iv) Write name of all the member functions accessible by an object.`

Ans.7.
(i) Hierarchical Inheritance OR Single Level Inheritance
(ii) 41 bytes
(iii) SDName, Weight, Price
(iv) EDInput(), EDShow(),DInput(), DShow()
Note:
• Constructor functions ElectronicDolls() & Dolls() to be
ignored.

Q.8 consider the following class declaration and answer the question below :
class university {
int noc;
protected;
char uname[25];
public:
university();
char state[25];
void enterdata();
void displaydata();
};
class college:public university{
int nod;
char cname[25];
protected:
void affiliation();
public:
college();
void enrol(int ,int);
void show();
};
class department:public college{
char dname[25];
int nof;
public:
department();
void display();
void input();
};
(i) Which class’s constructor will be called first at the time of declaration of an object of class department?
(ii) How many bytes does an object belonging to class department require?
(iii)Name the member function(s), which are accessed from the object of class department.
(iv) Name the data member, which are accessible from the object of class college.

Ans.8. (i) Constructor of University Class ( Top most Base class)
(ii) 106 bytes
(iii) display(), input(), enrol(int,int), show(), enterdata(), displaydata()
(iv) state

9 Answer the questions(i) to (iv) based on the following :
class cloth
{
char category[5];
char description[25];
protected:
float price;
public:
void Entercloth( );
void dispcloth( );
};
class Design : protected cloth
{
char design[21];
protected:
float cost_of_cloth;
public:
int design_code;
Design( );
void Enterdesign( );
void dispdesign( );
};
class costing : public cloth
{
float designfee;
float stiching;
float cal_cp( );
protected:
float costprice;
float sellprice;
public:
void Entercost( );
void dispcost( );
costing ( ) { };
};
(i) Write the names of data members which are accessible from objects belonging to class cloth.
(ii) Write the names of all the members which are accessible from objects belonging to class Design.
(iii) Write the names of all the data members which are accessible from member functions of class costing.
(iv) How many bytes will be required by an object belonging to class Design?

Ans.9.
(i) None of the data members
(ii) void Enterdesign( ), void dispdesign( )
(iii) price, cost_of_cloth, design_code, designfee, stiching,costprice, sellprice;
(iv) 61 bytes

Q.10.
Answer the questions(i) to (iv) based on the following :
class Regular
{
char SchoolCode[10];
public:
void InRegular( );
void OutRegular( );
};
class Distance
{
char StudyCentreCode[5];
public:
void InDistance( );
void OutDistance( );
};
class Course : public Regular, private Distance
char Code[5];
float Fees;
int Duration;
public:
void InCourse( );
void OutCourse( );
};
(i) Which type of Inheritance is shown in the above example?
(ii) Write names of all the member functions accessible from Outcourse function of class Course.
(iii) Write name of all the members accessible through an object of the Class Course.
(iv) Is the function InRegular( ) accessible inside the function InDistance ( )?
Justify your answer.

Ans.10. (i)Multiple Inheritance
(ii) InCourse( ), InDistance( ), OutDistance( ), InRegular( ),
OutRegular( )
(iii)InCourse( ), OutCourse( ), InRegular( ), OutRegular( )
(iv) No, function InRegular( ) is not accessible inside the function
InDistance( ), because InRegular( ) is a member of class Regular
and InDistance( ) is a member of class Distance, and the class
Regular and Distance are two independent classes. OR
Yes, Reason is that if we call InRegular( ) inside the function InDistance ( ) in
this way only
void InDistance( )
{
Regular R;
R. InRegular();
}

11. Define a class named ADMISSION in C++ with the following descriptions:
Private members:
AD_NO integer (Ranges 10 - 2000)
NAME Array of characters (String)
CLASS Character
FEES Float
Public Members:
• Function Read_Data ( ) to read an object of ADMISSION type
• Function Display( ) to display the details of an object
• Function Draw_Nos ( ) to choose 2 students randomly and display the details.
Use random function to generate admission nos to match with AD_NO.

12. Define a class named MOVIE in C++ with the following description:
Private members
HALL_NO integer
MOVIE_NAME Array of characters (String)
WEEK integer (Total number of weeks the same movie is shown)
WEEK_COLLECTION Float
TOTAL_COLLECTION Float
Public Members
• Function Read_Data( ) to read an object of ADMISSION type
• Function Display( ) to display the details of an object
• Function Update( ) to update the total collection and Weekly collection once in a
week changes. Total collection will be incremented by Weekly collection and Weekly collection is made Zero

13. Consider the following declarations and answer the questions given
below:
class Mydata
{ protected:
int data;
public:
void Get_mydata(int);
void Manip_mydata(int);
void Show_mydata(int);
Mydata( );
~Mydata( );
};
class Personal_data
{
protected:
int data1;
public:
void Get_personaldata(int);
void Show_personaldata(int);
Personal_data1( );
~Personal_data1( );
};
class Person: public Mydata, Personal_data
{
public:
void Show_person(void);
Person( );
~Person( );
};
i) How many bytes will be required by an object belonging to class Person?
ii) Which type of inheritance is depicted in the above example?
iii) List the data members that can be accessed by the member function Show_person( ).
iv) What is the order of constructor execution at the time of creating an object of class Person?

14. Answer the questions (i) to (iv) based on the following:
class Book
{
int year_publication;
char title[25];
float price;
public:
Book( );
void input_data( );
void output_data( );
};
class Tape
{
char comp_name[20];
protected:
char comp_addr[35];
public:
Tape( );
void read_data( );
void show_data( );
};
class Publication : private Book , public Tape
{
int no_copies;
public:
Publication( );
void Pub_Entry( );
void Pub_Detail( );
};
(i) Write the names of data members which are accessible from objects belonging to class Publication.
(ii) Write the names of all the member functions which are accessible from objects belonging to class Tape.
(iii) Write in which order the constructors will be invoked when an object of class Publication is created .
(iv) How many bytes will be required by an object belonging to class Publication?

15. Answer the questions (i) to (iv) based on the following code:
class vehicle
{
int wheels;
protected:
int passenger;
public:
void inputdata( );
void outputdata( );
};
class heavyvehicle : protected vehicle
{
int diesel_petrol;
protected:
int load;
public:
void readdata(int, int);
void writedata( );
};
class bus : private heavyvehicle
{
char make[20];
public:
void fetchdata( );
void displaydata( );
};
i) Name the base class and derived class of heavyvehicle class.
ii) Name the data member(s) that can be accessed from the function displaydata( ).
iii) How many bytes will be required by an object of vehicle and heavyvehicle classes respectively?
iv) Is the member function outputdata( ) accessible to the objects of the class heavyvehicle?

16.. Consider the following declarations and answer the questions given below:
class Animal
{
int leg:
protected:
int tail;
public:
void INPUT (int );
void OUT ( );
};
class wild : private Animal
{
int carniv;
protected:
int teeth;
Public:
void INDATA (int, int )
void OUTDATA( );
};
class pet : public Animal
{
int herbiv;
public:
void Display (void);
};
(i) Name the base class and derived class of the class wild.
(ii) Name the data member(s) that can be accessed from function Display ( ).
(iii) Name the member function(s), which can be accessed from the objects of class pet.
(iv) Is the member function OUT ( ) accessible by the objects of the class wild?

17. Answer the questions (i) to (iv) based on the following class declaration:
class Medicine
{
char category[10];
char Date_of_Manufacture[10];
char Date_Of_Expiry[10];
protected:
char company[20];
public:
int x,y;
Medicine( );
void Enter( );
void Show( );
};
class Tablet :protected Medicine
{
protected:
char tablet_name[30];
char volume_label[20];
void disprin( );
public:
float price;
Tablet( );
void enterdet( );
void showdet( );
};
class PainReliever : public Tablet
{
int Dosage_units;
long int tab;
char effects[20];
protected:
I int use_within_Days;
public :
PainReliever( );
void enterpr( );
showpr( );
};
(i) How many bytes will be required by an object of class Drug and an object of class PainReliever respectively.
(ii) Write names of all the data members which are accessible from the object of class PainReliever.
(iii) Write names of all member functions which are accessible from objects of class
PianReliever.
(iv) Write the names of all the data members which are accessible from the functions
enterpr().

18. Answer the questions (i) to (iv) based on following code:
class World
{
int H;
protected
int s;
public:
void INPUT(int);
void OUTPUT( );
};
class Country : private World
{
int T;
protected:
int U;
public :
void INDATA(int, int);
void OUTDATA();
};
class State : Public Country
{
int M;
public :
void DISPLAY(void);
};
(i) Name the base class and derived class of the class Country.
(ii) Name the data member that can be accessed from function DISPLAY( )
(iii) Name the member functions, which can be accessed from the objects of class State.
(iv) Is the member function OUTPUT() accessible by the objects of the class Country ?

19. Answer the questions (i) to (v) based on the following code :
class Employee
{
int id;
protected:
char name[20];
char doj[20];
public :
Employee( );
~Employee( );
void get( );
void show( );
};
class Daily_wager : protected Employee
{
int wphour;
protected :
int nofhworked;
public :
void getd( );
void showd( );
};
class Payment : private Daily_wager
{
char date[10];
protected :
int amount;
public :
Payment( );
~Payment( );
void show( );
};
(i) Name the member functions, which are accessible by the objects of class Payment.
(ii) From the following, Identify the member function(s) that can be called
directly from the object of class Daily_wager class show( ), getd( ),
get( )
(iii) Find the memory size of object of class Daily_wager.
(iv) Is the constructors of class Employee will copied in class Payment Due to Inheritance?

20. Answer the questions (i) to (iii) based on the following code:
class toys
{
char Code;
char Manufacturer [10];
public:
toys( );
void Read_toy_details ( );
void Disp_toy_details( );
};
class electronic : public toys
{
int no_of_types;
float cost_of_toy;
public:
void Read_elect_details ( );
void Disp_elect_details ( );
};
class infants : private electronic
{
int no_of_buyers;
char delivery date[10];
public:
void Read_infant_details ( );
void Disp_jnfant_details( );
};
void main ( )
{
infants MyToy;
}
(a) Mention the member names which are accessible by MyToy declared in main ( ) function.
(b) What is the size of MyToy in bytes?
(c) Mention the names of functions accessible from the member function Read_infant_details () of class printer.
(d) Which type of inheritance shown in the above code?

Tags: 

 


Click for more Computer Science Study Material

Latest NCERT & CBSE News

Read the latest news and announcements from NCERT and CBSE below. Important updates relating to your studies which will help you to keep yourself updated with latest happenings in school level education. Keep yourself updated with all latest news and also read articles from teachers which will help you to improve your studies, increase motivation level and promote faster learning

MCQ Question based CBSE examination

For 2021-22 CBSE has launched MCQ question-based examination for Term 1 & Term 2 board examinations. The entire syllabus has been divided into two parts each including 50% of the entire syllabus. To score well, students must practice as per the new CBSE term-wise...

How To Solve Unseen Passages In English

Unseen passages may contain one or many paragraphs. This is one of the important yet easy parts for a student to get marks. Students should thoroughly study and understand the passage to answer the related questions. The unseen passages are there just to test the...

Score well in Class 12 English Boards Exam

12th Board exams are an important part of students' lives. The marks obtained in the board exam decide the college in which one can study. In class 12 the syllabus of each and every subject increases vastly and it is difficult to cover up every point. In English also...

Moderation of Marks Class 11 and 12 Board Exams

The portal for moderation and finalization of results for Class-12 is being opened from 16.07.2021 to 22.07.2021. As Board has to declare the result latest by 31.07.2021, schools have been requested to follow the schedule strictly and complete the moderation within...

CBSE Class 10 Revised Syllabus

Last year CBSE had to reduce the syllabus because of the pandemic situation but it was not very effective because there were no examinations. This year to avoid any confusion and conflict, CBSE has decided to reduce the syllabus into term 1 and term 2. 50 percent of...

×
Studies Today