• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • NCERT Solutions
    • NCERT Books Free Download
  • TS Grewal
    • TS Grewal Class 12 Accountancy Solutions
    • TS Grewal Class 11 Accountancy Solutions
  • CBSE Sample Papers
  • NCERT Exemplar Problems
  • English Grammar
    • Wordfeud Cheat
  • MCQ Questions

CBSE Tuts

CBSE Maths notes, CBSE physics notes, CBSE chemistry notes

Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Constructor and Destructor

Contents

  • 1 Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Constructor and Destructor
    • 1.1 Topic – 1 Constructor
    • 1.2 Topic – 2 Destructor

Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Constructor and Destructor

Topic – 1
Constructor

Exam Practice
Very Short Answer Type Questions [2 Mark]

Question 1:
Find the output of the following program: Delhi 2012

#include<iostream.h> 
class Train
{
int TNo,TripNo,PersonCount; 
public:
Train(int TN=1)
{
TNo=TN;
TripNo=0;
PersonCount=0;
}
void Trip(int TC=100)
{
TripNo++; 
PersonCount += TC;
}
void Show()
{
cout<<TNo<<":"<<TripNo<<":"; 
cout<<PersonCount<<endl;
}
};
void main()
{
Train T(10),N; 
N.Trip();
T.Show();
T.Trip(70);
N.Trip(40);
N.Show();
T.Show();
}

Аnswer:
Output
10 : 0 : 0
1 : 2 : 140
10 : 1 : 70

Question 2:
Find the output of the following program:  All India 2012

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

Аnswer:
Output
5 : 1 : 20
1 : 1 : 50
5 : 2 : 50

Question 3:
Answer the questions (i) and (ii) after going through the folio wing class:    Delhi 2013

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

Аnswer:

  1. Option 2-Motor P(10) is correct.
  2. Constructor overloading.

Question 4:
What is copy constructor? Give an example in C++ to illustrate copy constructor.    Delhi 2009
Аnswer:
Copy Constructor A copy constructor is that constructor which is used to initialise one object with the values
from another object of same class during declaration, e.g.

class Student 
{
int s; 
public:
Student() //Default constructor 
{
s = 10;
}
Student(Student &i) //Copy constructor
{
s=i.s;
}
};
void main() 
{
Student s1; //Default constructor called 
Student s2(s1); //Copy constructor called 
}

Question 5:
Answer the questions (i) and (ii) after going through the following class:    Delhi 2009

class Factory 
{
char Name[20]; 
int Workers; 
public:
Factory()    //Function 1
{
strcpy(Name,"Default”);
Workers = 0;
}
void Details()    //Function 2
{
cout<<Name<<endl<<Workers<<endl;
}
Factory(char *act_Name,int No); //Function 3 
Factory(Factory &F); //Function 4 
};

(i)  In object oriented programming, what is function 4 referred as? Also, write a statement which will invoke
this function.
(ii) In object oriented programming, which concept is illustrated by Function 1, Function 3 and Function
4 together?

Аnswer:

  1. Function 4 is referred as copy constructor, the statement to invoke it.
    Factory F1;
    Factory F2(“ABC”,101);
    Factory F3(F2);
  2. Function 1, Function 3 and Function 4 illustrate the concept of polymorphism (or) constructor overloading.

Question 6:
Answer the questions (i) and (ii) after going through the folio wing program:    Delhi 2008

#include<iostream.h>
#1nclude<string.h> 
class Bazar 
{
char Type[20]; 
char Product[20]; 
int Qty; 
float Price;
Bazar()    //Function 1
{
strcpy(Type,"electronic"); 
strcpy(Product,"calculator");
Qty = 10;
Price = 225;
}
public:
void disp()
{
cout<<Type<<"-"<<Product<<":"; 
cout<<Qty<<"@"<<Price<<endl;
}
};
void main()
{
Bazar B;    //Statement 1
B.disp();    //Statement 2
}
  1. Will Statement 1 initialise all the data members for object B with the values given in the Function 1?
    (Yes or No). Justify your answer suggesting the corrections to be made in the above code.
  2. What shall be the possible output when the program gets executed? (assuming, if required the suggested corrections are made in the program).

Аnswer:

  1. No. Since, the default constructor Bazar() is declared inside private section, it cannot initialise the
    objects declared outside the class.
    Correction needed is:
    The constructor Bazar( ) should be declared inside public section.
  2. Output
    electronic-calculator :[email protected]

Question 7:
Discuss the benefits of constructor overloading. Can other member function of a class be also overload? What is your opinion?
Аnswer:
Constructor overloading facilitates multiple ways of object creation. That is, with constructor overloading one can create objects without passing any value; by passing certain value; or as replica of an existing object.
Yes, any member function of a class can be overloaded.

Question 8:
Answer the questions (i) and (ii) after going through the following program:    All India 2008

#include<iostream.h>
#include<conio.h> 
class Retail 
{
char Category[20]; 
char Item[20]; 
int Qty; 
float Price;
Retail()    //Function 1
{
strcpy(Category,"Cereal"); 
strcpy(Item,"Rice");
Qty = 100;
Price = 25;
}
public:
void show()
{
cout<<Category<<"-"<<Item<<":"<<Qty<<"@"<<Price<<endl;
}
};
void main()
{
Retail R;    //Statement 1
R.show();    //Statement 2
}
(i) Will Statement 1 initialise all the data members for object R with the values
given in
the Function 1 ?(Yes or No). Justify your answer suggesting the corrections to
be made in the above code.
(ii) What shall be the possible output when the program gets executed?
(assuming, if required the 
suggested corrections are made in the program.

Аnswer:

  1. No. Since, the default constructor Retail() is declared inside private section, it cannot initialise
    the objects declared outside the class.
    Correction needed is:
    The constructor Retail( ) should be declared inside public section.
  2. Output
    Cereal-Rice:[email protected]

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

class Interview 
{
int Month; 
public:
Interview(int y) //Constructor 1 
{
Month = y;
}
Interview(Interview &t);  //Constructor 2
};
  1. Create an object, such that it invokes Constructor 1.
  2. Write the complete definition for Constructor 2.

Аnswer:

(i) Interview obj 1(7);
(ii) Interview(Interview &t)
 {
 Month = t.Month;
}

Question 10:
What do you understand by default constructor and copy constructor function used in classes?
How are these functions different from normal constructor?
Аnswer:
Default Constructor A constructor gets invoked without any parameter is called default constructor. Copy
Constructor A copy constructor is a constructor that gets invoked when an object is created as copy
of another existing object.
e-g.

class ABC
{ 
int a; 
public:
ABC()    //Default constructor
{
a = 0;
}
ABC(ABC &t) //Copy constructor 
{
 a = t.a;
}
};

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

class Test
{
char Paper[20]; 
int Marks; 
public:
Test()    //Function 1
{
strcpy(Paper,"computer");
Marks = 0;
}
Test(char p[])    //Function 2
{
strcpy(Paper,p);
Marks = 0;
}
Test(int m)    //Function 3
{
strcpy(Paper,"computer");
Marks = m;
}
Test(char p[],int m)//Function 4 
{
strcpy(Paper,p);
Marks = m;
}
};
  1. Which feature of object oriented programming is demonstrated using Function 1, Function 2,
    Function 3 and Function 4 in the above class Test ?
  2. Write statement in C++ that would execute Function 2 and Function 4 of class Test.

Аnswer:

  1. Constructor overloading.
  2. Test tl( “Maths” ) ;    //Function 2 gets invoked
    Test t2(“Chemistry”,85);  //Function 4 gets invoked

Question 12:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
Delhi 2009

#include[iostream.h]
#include[stdio.h] 
class Employee 
{
int EmpId = 901; 
char EName[20]; 
public
Employee()
{}
void Joining()
{
cin>>EmpId; 
gets(EName);
}
void Lis t()
{
cout<<EmpId<<":"<<EName<<endl;
}
};
void main()
{
Employee E;
Joining.E();
E.list();
}

Аnswer:

#include<iostream.h>
#include<stdio.h> 
class Employee 
{
int EmpId; 
char EName[20]; 
public:
Employee() 
{
EmpId = 901:
}
void Joining() 
{
cin>>EmpId; 
gets(EName);
}
void List()
}
cout<<EmpId<<":"<<EName<<endl;
}
};
void main()
{
Employee E;
E.Joining();
E.List();
}

Question 13:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
Delhi 2011C

#include<iostream.h>
#include<stdio.h> 
class AUTO 
{
char Model[20]; 
float Price;
AUTO()
{
Price = 0;
strcpy(Model),"NULL";
}
public:
void Getlnfo()
{
cin>>Price; 
gets(Model);
}
void Put Info()
{
cout<<setw(10)<<Price<<setw(10); 
cout<<Model<<endl;
}
}
void main()
{
AUTO Car;
Car.Get Info(); 
Car.Putlnfo();
}

Аnswer:

#include<iostream.h>
#include<stdio.h>
#include<iomanip.h> 
#include<string.h> 
class AUTO 
{
char Model[20]; 
float Price; 
public:
AUTO()
{
Price = 0;
strcpy(Model."NULL");
}
void GetInfo()
{
cin>>Price; 
gets(Model);
}
void Put Info()
{
cout<<setw(10)<<Price<<setw(10); 
cout<<Model<<endl;
}
};
void main()
{
AUTO Car;
Car.Get Info();
Car.Put Info();
}

Question 14:
Rewrite the following program after removing the syntactical error(s) (if any). Underline each correction.
All India 2009

#include<iostream.h>
#include<stdio.h> 
class MyStudent 
{
int StudentId=1001; 
char Name[20]; 
public:
MyStudent() {} 
void Register()
{
cin>>StudentId; 
gets(Name);
}
void Display()
{
cout<<StudentId<<":"<<Name<<endl;
}
};
void Main()
{
Mystudent MS; 
Register.MS(); 
MS.Display();
}

Аnswer:

#include<iostream.h>
#include<stdio.h> 
class Mystudent 
{
int StudentId;
char Name[20]; 
public:
MyStudent()
{
StudentId=1001:
}
void Register()
{
cin>>StudentId; 
gets(Name); 
}
void Display()
{
cout<<StudentId<<":"<<Name<<endl;
}
};
void main()
{
MyStudent MS;
MS.Register();
MS.Display();
}

Question 15:
Locate the errors in the following program and write the correct program.

#include<iostream.h>
#define 1000 MAX_SIZE; 
typedef BYTE unsigned char 
class MyClass 
{
int m,n; 
public;
void MyClass(); 
void get();
}
void MyClass :MyClass()
{
m=n=0;
}
void MyClass:get()
{
cin>>m>>n;
{
void main{}
{
MyClass MyObject; 
int n=10; 
int Arr[n]; 
for(i=0,i<=n,i++) 
cin>>Arr[i];
}

Аnswer:

The correct program is:
#include<iostream.h> 
#define MAX SIZE 1000 
typedef unsigned char BYTE:
class MyClass 
{
int m,n; 
public:
MyClass();
void get();
};
MyClass::MyClass()
{
m=n=0:
}
void MyClass :: get()
{
cin>>m>>n;
}
void main()
{ 
MyClass MyObject; 
int n=10; 
int Arr[10]; 
for(int i=0;i<n;i++) 
cin>>Arr[i];
}

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

#include<iostrearn.h> 
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<<Place<<":"<<Temp<<"&"<<Humidity<<"%"<<endl;
}
};
void main()
{
Aroundus A, B(5); 
A.Hot(1O);
A.JustSee();
B.Humid(15);
B.Hot(2);
B.JustSee(); 
A.Humid(5);
A.JustSee();
}

Аnswer:
Output
2 : 30&60%
5 : 22&75%
2 : 30&65%

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

class Race 
{
int CarNo,Track; 
public:
Race();
Race(int CN); 
Race(Race &R); 
void Register(); 
void Drive();
};
void main()
{
Race R;
:
}
  1. Out of the following, which of the options is correct for calling Function 2?
    Option 1-Race T(30);
    Option 2-Race U(R);
  2. Name the feature of object oriented programming, which is illustrated by Function i, Function 2 and Function 3 combined together.

Аnswer:

  1. Option 1 – Race T(30) is correct.
  2. Constructor overloading.

Question 18:
Obtain the output of the following C++ program, which will appear on the screen after its execution.
Delhi 2014
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:
PIayer(char GGame=‘A’) 
{
Score=0;
Level=1;
Game=GGame;
}
void Start(int SC); 
void Next(); 
void Disp()
{
cout<<Game<<"@"; 
cout<<Level<<endl; 
cout<<Score<<endl;
}
};
void main()
{
Player P,Q('B');
P.Disp();
Q.Start(75);
Q.Next();
P.Start(120);
Q.Disp();
P.Disp();
}
void Player::Next()
{
Game=(Game=='A')?'B':'A';
}
void PIayer::Start(int SC) 
{
Score += SC; 
if(Score>=100)
Level=3;
else if(Score>=50)
Level,=2;
else
Level=1;
}

Аnswer:
Output
[email protected]
0
[email protected]
75
[email protected]
120

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

class Hospital
{
int Pno.Dno; 
public:
Hospital(int PN); 
Hospital(); 
Hospital(Hospital &H);
void In(); 
void Disp();
};
void main()
{
Hospital H(20);    //Statement 1
}
  1. 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?
  2. Write a statement to declare a new object G with reference to already existing object H using
    Function 3.

Аnswer:

  1. Function 1 will be called when statement 1 will be executed.
  2. Hospital G(H);

Question 20:
Write a characteristics of a constructor function used in a class.    Delhi 2014
Аnswer:
Characteristics of the constructor function used in a class are as follows:

  1. Constructors are special member functions having same name as that of class.
  2. Constructors do not need to be called explicitly. They are invoked automatically whenever
    an object of the class is created.
  3. They are used for initialisation of data members of a class.
  4. Constructors do not have a return type and that is why cannot return a value.

Question 21:
Write the output of the following program:

#include<iostream.h> 
class Quiz
{
int Round; 
float Score; 
public:
Quiz()
{
Round = 1;
Score = 0;
}
Quiz(Quiz &Q)
{
Round = Q.Round + 1;
Score = Q.Score + 10;
}
void GetBonus(float B = 5)
{
Score += B;
}
void ShowScore()
{
cout<<Round<<"#"<<Score<<endl;
}
};
void main()
{
Quiz A;
A.ShowScore(); 
A.GetBonus(10);
A.ShowScore(); 
Quiz B(A);
B.GetBonus();
B.ShowScore();
}

Аnswer:
Output
1#0
1#10
2#25

Question 22:
Answer the questions (i) and (ii) after going through the folio wing class: Delhi 2013C

class Book
{
int BookNo; 
char BookTitle[20]; 
public:
Book();   //Function 1
Book(Book &); //Function 2
Book(int,char[]); //Function 3
void Buy(); //Function 4
void Sell(); //Function 5
};
:
void main()
{
 :
 :
}
  1. Name the feature of object oriented programming demonstrated by Function 1, Function 2 and Function 3.
  2. Write statements in C++ to execute Function 3 and Function 4 inside the main( ) function.

Аnswer:

  1. Constructor overloading.
  2. Book B (10, “GABS”) ;
    B.Buy( );

Long Answer Type Questions [4 Marks]

Question 23:
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 kilometer charges
  • Distance to store distance travelled (in KM)

Member functions

  • A constructor function to initialise CType as ‘A ’ and CNo as ‘0000’
  • A function CityCharges( ) to assign PerKM as per the following table:
  • A function RegisterCabf ) to allow administrator to enter the values for CNo and CType. Also, this function should call CityCharges( ) to assign PerKM Charges.

    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.

Аnswer:

class Tourist 
{
int CNo; 
char CType;
float PerKM; 
float Distance;
public:
Tourist()
{
CType = 'A';
CNo = 0000;
}
void CityCharges()
{
if(CType == 'A')
PerKM = 20; 
else if(CType == 'B')
PerKM = 18;
else if(CType == 'C')
PerKM = 15;
}
void RegisterCab()
{
cout<<"Enter the Cab Number:"; 
cin>>CNo;
cout<<"Enter the Cab Type(A/B/C):"; 
cin>>CType;
CityCharges();
}
void Display()
{
cout<<"Enter the Distance:"; 
cin>>Distance ;
cout<<"Registered details are\n"; 
cout<<"Cab Number:"<<CNo<<endl; 
cout<<"Cab Type :"<<CType<<endl; 
cout<<"Charges per km:"; 
cout<<PerKM<<endl; 
cout<<"Amount :"<<PerKM*Distance; 
cout<<endl;
}
};

Question 24:
Define a class Garments in C++ with the following description: Delhi 2008
Private members

  • GCode of type string
  • GType of type string
  • GSize of type integer
  • GFabric of type string
  • G Price of type float
  • A function Assign( ) which calculates and assigns the value of G Price as follows:
    GType GPrice
    TROUSER 1300
    SHIRT 1100

For GFabric other than “COTTON” the above mentioned GPrice gets reduced by 10%.
Public members

  • A constructor to assign initial values of GCode, GType and GFabric with the word “NOTALLOTTED”
    and GSize and GPrice with 0.
  • A function input( ) to input the values of the data members of GCode, GType, GFabric, GSize and
    invoke the Assignf ) function.
  • A function Displayf ) which displays the content of all the data members for a Garment.

Аnswer:

class Garments 
{
char GType[15]; 
int GSize; 
char GFabric[15]; 
float GPrice; 
void Assign()
{
if(strcmp(GFabric,"COTTON")==0)
{
if(strcmp(GType,"TROUSER")==0) 
GPrice = 1300;
else if(strcmp(GType,"SHIRT")==0) 
GPrice = 1100;
}
else
{
if(strcmp(GType,"TROUSER")==0) 
GPrice = 1300 - 0.10 *1300;
else if(strcmp(GType,"SHIRT") ==0) 
GPrice = 1100 - 0.10*1100;
}
}
public:
Garments()
{
strcpy(GCode,"NOT ALLOTTED"); 
strcpy(GType,"NOT ALLOTTED"); 
strcpy(GFabric,"NOT ALLOTTED"); 
GSize = 0;
GPrice = 0;
}
void input()
{
cout<<"Enter Garment Code"; 
cin>>GCode;
cout<<"Enter Garment Type(TR0USER/SHIRT)"; 
gets(Glype);
cout<<"Enter Garment Size"; 
cin>>GSize;
cout<<"Enter Garment Fabric"; 
gets(GFabric);
Assign();
}
void Display()
{
cout<<"Garment Code :"<<GCode<<endl; 
cout<<"Garment Type :"<<GType<<endl; 
cout<<"Garment Size:"<<GSize<<endl; 
cout<<"Garment Fabric:"<<GFabric<<endl; 
cout<<"Garment Price: "<<GPrice<<endl;
}
};

Question 25:
Define a class Clothing in C++ with the following description: All India 2008
Private members

  • Code of type string
  • Type of type string
  • Size of type integer
  • Material of type string
  • Price of type float
  • A function Caic_Price( ) which calculates and assigns the value of Price as follows:
    For the Material as “COTTON”

    Type Price
    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 input( ) to input the values of the data members of Code, Type, Material, Size and invoke the Calc_Price( ) function.
  • A function Show( ) which displays the content of all the data members for a Clothing.

Аnswer:

class Clothing
{
char Code[15]; 
char Type[15]; 
int Size;
char Material[15]; 
float Price; 
void Calc_Price()
{
if(strcmp(Material,"COTTON")==0)
{
if(strcmp(Type,"TROUSER")==0) 
Price = 1500;
else if(strcmp(Type,"SHIRT")==0) 
Price = 1200;
}
else
{
if(strcmp(Type,"TROUSER")==0) 
Price = 1500-0.25*1500; 
else if(strcmp(Type,"SHIRT") ==0)
Price = 1200-0.25*1200;
}
}
public:
Clothing()
{
strcpy(Code,"NOT ASSIGNED"); 
strcpy(Type,"NOT ASSIGNED"); 
strcpy(Material."NOT ASSIGNED"); 
Size = 0;
Price = 0;
}
void input()
{
cout<<"Enter Clothing Code"; 
cin>>Code;
cout<<"Enter Clothing Type(TROUSER/SHIRT)";
cin>>Type;
cout<<"Enter Clothing Size"; 
cin>>Size;
cout<<"Enter Clothing Material"; 
gets(Material);
Calc_Price();
}
void Show()
{
cout<<"Clothing Code"<<Code; 
cout<<"\nClothing Type"<<Type; 
cout<<"\nClothing Size"<<Size; 
cout«"\nClothing Material"<<Material; 
cout<<"\nClothing price"<<Price;
}
};

Question 26:
Define a class Tour in C++ with the description given below:
Private members

  • TCode of type string
  • No_of_Adults of type integer
  • No_of_Kids of type integer
  • Kilometers of type integer
  • Total Fare of type float

Public member

  • A constructor to assign initial values as follows:
  • TCode with the word “NULL”
  • No_of_Adults 0
  • No_of_Kids as 0
  • Kilometers as 0
  • TotalFare as 0

A function Assign Fare ( ) which calculates and assigns the value of the data member TotalFare as follows:

TotalFare Kilometers
500 > = 1000
300 <1000&>=500
200 <500

For each Kid, the above Fare will be 50% of the Fare mentioned in the above table.

  • A function EnterTourf ) to input the values of the data members T_Code, No_of_Adults, No_of_Kids and Kilometers and invoke the AssignFaref ) function.
  • A function ShowTourf ) which displays the content of all the data members for a Tour.

Аnswer:

class Tour 
{
char TCode[15]; 
int No_of_Adults; 
int No_of_Kids; 
int Kilometers; 
float Total Fare; 
public:
Tour()
{
strcpy(TCode,"NULL"); 
No_of_Adults = 0;
No_of_Kids = 0;
Kilometers = 0;
Total Fare = 0;
}
void AssignFare()
{
int i,j;
Total Fare = 0 ;
for(i=0;i<No_of_Adults;i++)
{
if(Ki1ometers >= 1000) 
Total Fare += 500; 
else if(Ki1ometers >= 500) 
Total Fare += 300; 
else
Total Fare += 200;
}
for(j=0;j<No_of_Kids;j++)
{
if(Ki1ometers >= 1000) 
 TotalFare += 500/2 
else if(Ki1ometers >= 500) 
 Total Fare += 300/2 
else
 TotalFare += 200/2
}
}
 void EnterTour()
{
cout<<”Enter Tour Code"; 
gets(TCode);
cout<<"Enter Number of Adults"; 
cin>>No_of_Adults;
cout<<"Enter Number of Kids"; 
cin>>No_of_Kids; 
cout<<"Enter kilometers"; 
cin>>Ki1ometers;
AssignFare();
}
void ShowTour()
{
cout<<"Tour Code"<<TCode<<endl; 
cout<<"Number of Adults"; 
cout<<No_of_Adults<<endl; 
cout<<"Number of Kids"; 
cout<<No_of_Kids<<endl; 
cout<<"ki1ometers"cout<<Ki1ometers<<endl; 
cout<<"Total Fare"cout<<TotalFare<<endl;
}
};

Question 27:
Define a class TravelPlan in C++ with the description given below:
Private members

  • PlanCode of type long
  • Place of type character array
  • Number_of_Travellers of type integer
  • Number_of_Buses of type integer

Public members

  • A constructor to assign initial values of PlanCode as 1001, Place as “Agra”, Number_of.^Travellers as 5, Number_of_Buses as 1.
  • A function NewPlanf ) which allows user to enter PlanCode, Place and Number_of_Travellers. Also,
    assign the value of Number_of_Buses as per the following conditions:

    Number_of_Travellers Number_of_Buses
    Less than 20 1
    Equal to or more than 20 and less than 40 2
    Equal to 40 or more than 40 3
  • A function ShowPlanf ) to display the content of all the data members on screen.

Аnswer:

class Travel Plan 
{
long PlanCode; 
char Place[40]; 
int Number_of_Travel1ers; 
int Number_of_Buses;
public:
TravelPlan()
{
PlanCode = 1001; 
strcpy(Place,"Agra"); 
Number_of_Travel1ers = 5; 
Number_of_Buses = 1;
}
void NewPlan()
{
cout<<"Enter Plan Code, Place and Number of Travellers"; 
cin>>PlanCode; 
gets(Place);
cin>>Number_of_Travellers; 
if(Number_of_Travellers<20) 
 Number_of_Buses = 1; 
else if(Number_of_Travellers<40) 
 Number_of_Buses = 2; 
else   
 Number_of_Buses = 3;
}
void Show Plan ()
{
cout<<"Plan Code"; 
cout<<PlanCode<<endl; 
cout<<"Place"<<Place; 
cout<<"Number of Travellers"; 
cout<<Number_of_Travellers; 
cout<<"Number of Buses"; 
cout<<Number_of_Buses;
}

Question 28:
Define a class Serial in C++ with following specifications:
Private members

  • SerialCode
  • Title
  • Duration
  • No_of episodes

Public members

  • A constructor function to initialise Duration as 30 and No_of_episodes as 10.
  • NewSerial( ) function to accept values for Serialcode and Title.
  • Otherentries( ) function to assign the values of Duration and No_of_episodes with the help of
    corresponding values passed as parameters to this function.
  • Dispdata( ) function to display all the data members on the screen.

Аnswer:

class Serial 
{
int SerialCode; 
char Title[20]; 
float Duration; 
int No_of_episodes; 
public:
Serial()
{
Duration = 30;
No_of_episodes = 10;
}
void NewSerial()
{
cout<<"Enter Serial Code and Title"; 
cin>>SerialCode; 
gets(Title);
}
void Otherentries(float i, int j) 
{
Duration = i;
No_of_episodes = j;
}
void Dispdata()
{
cout<<"Serial Code"; 
cout<<SerialCode<<endl; 
cout<<"Title"<<Titie; 
cout<<"Durâtion"<<Duration; 
cout<<"No of episodes"; 
cout<<No_of_episodes;
}
};

Question 29:
Define a class Tourist in C++ with the following specification: Delhi 2013
Data members

  • CarNo to store Car Number
  • Origin to store Place name
  • 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 initialise Type as ‘E’ and Freight as 250
  • A function CalcCharge( ) to calculate Fare as per the following criteria:
    Type Charge
    ‘E’ 16*Distance
    ‘A’ 22*Distance
    ‘L’ 30*Distance

A function Enter( ) to allow user to enter values for CarNo, Origin, Destination, Type and Distance. Also,
this function should call CalcCharge( ) to calculate Fare.
* A function Show( ) to display the content of all the data members on screen.

Аnswer:

class Tourist 
{
char Origin[25], Destination[25],Type; 
int CarNo, Distance, Charge; 
public:
Tourist();    //Constructor
int Enter(); 
void CalcCharge(); 
void Show();
};
Tourist :: Tourist()
{
Charge = 250;
Type = 'E';
}
Tourist :: Enter()
{
cout<<"Enter the CarNo:"; 
cin>>CarNo; 
cout<<"\nEnter the Origin:"; 
cin>>0rigin;
cout<<"\nEnter the Destination"; 
cin>>Destination;
cout<<"\nEnter the Type"; 
cin>>Type; 
cout<<"Enter the Distance"; 
cin>>Distance;
CalcCharge();
}
void Tourist :: CalcCharge()
{
if(Type == 'E') 
{
Charge = 16*Distance;
}
else if(Type == 'A')
{
Charge = 22*Distance;
}
else if(Type == 'L')
{
Charge = 30*Distance;
}
}
void Tourist :: Show()
{
cout<<"Your Tour and Travel details"; 
cout<<"Car No"<<CarNo<<endl; 
cout<<"Origin Place"; 
cout<<Origin<<endl; 
cout<<"Destination Place"; 
cout<<Destination<<endl; 
cout<<"Type"<<Type<<endl; 
cout<<"Distance"<<Distance<<endl; 
cout<<"Charge"<<Charge<<endl;
}

Question 30:
Define a class Bus in C++ with the following specifications: All India 2013
Data members

  • BusNo to store Bus Number
  • 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 initialise Type as ‘O’ and Freight as 500
  • A function CalcFaref ) to calculate Fare as per the following criteria:
    Type Charge
    ‘E’ 15*Distance
    ‘A’ 20*Distance
    ‘L’ 24*Distance
  • A function Allocate( ) to allow user to enter values for BusNo, From, To, Type and Distance. Also,
    this function should call CalcFaref ) to calculate Fare.
  • A function Show( ) to display the content of all the data members on screen.

Аnswer:

class Bus
{
int BusNo; 
char From[25]; 
char To[25]; 
char Type; 
int Distance; 
int Fare; 
public:
Bus()
{
J.ype = 'o';
Fare = 500;
}
void CalcFare()
{
if(Type=='o’)
{
Fare = 15*Distance;
}
else if(Type=='E')
{
Fare = 20*Distance; 
}
else if(Type=='L') 
{
Fare = 24*Distance;
}
}
void Allocate()
{
cout<<"Enter the value of";
cout<<"Bus No, from, To, Type,"; 
cout<<"Distance";
cin>>BusNo;
cin>>From;
cin>>To;
cin>>Type;
cin>>Distance;
CalcFare();
}
void Show()
{
cout<<"Bus No:"<<BusNo; 
cout<<"From;"<<From; 
cout<<"To:"<<To; 
cout<<"Type:"<<Type; 
cout<<"Distance:"<<Distance; 
cout<<"Fare:"<<Fare; 
}
};

Question 31:
Define a class CABS in C++ with the following specification: Delhi 2014
Data members

  • CNo to store Cab Number
  • Type to store a character ‘A\ ‘B’ or ‘C as City Type
  • PKM to store per Kilometer charges
  • Dist to store Distance travelled (in KM)

Member functions

  • A constructor function to initialise Type as A’ and CNo as ‘1111’
  • A function Charges( ) to assign PKM as per the following table:
    Type PKM
    ‘A’ 25
    ‘B’ 20
    ‘C’ 15
  • A function Registerf ) to allow administrator to enter the values for CNo and Type. Also, this function
    should call Charges( ) to adding PKM charges.
  • A function ShowCab( ) to allow user to enter the value of Distance and display CNo, Type, PKM, PKM Distance (as Amount) on screen.

Аnswer:

class CABS 
{
int CNo; 
char Type; 
float PKM; 
float Dist; 
public:
CABS() 
{
Type = 'A'; 
CNo = 1111;
}
void Charges()
{
if(Type=='A')
PKM=25;
else if(Type=='B')
PKM=20;
else if(Type=='C')
PKM=15;
}
void Register()
{
cout<<"Enter value for Cab Number:"; 
cin>>CNo;
cout<<"Enter value for Cab Type:"; 
cin>>Type;
Charges();
}
void ShowCab()
{
cout<<"Enter the Distance;"; 
cin>>Dist;
cout<<"<\nCab Number: "<<CNo; 
cout<<"\nCab Type : "<<Type; 
cout<<"\nPer Kilometer Charges :"<<PKM; 
cout<<"\nAmount:"<<PKM*Dist; 
}
};

Topic – 2
Destructor

Exam Practice
Very Short Answer Type Questions [2 Mark]

Question 1:
Write any two differences between constructor and destructor. Write the function headers for constructor and destructor of a class Member. Delhi 2013
or
Differentiate between constructor and destructor functions in a class. Give a suitable example in C++ to
illustrate the difference. Delhi 2012C
Аnswer:
A constructor is called when you want to create a new instance of a class. A destructor is called when you
want to free up the memory of an object (when you delete it).
A constructor constructs the value of an object. A destructor destructs the value created by the constructor
for the object, e.g. Constructor

class Fraction
{
int m_nNumerator; 
int m_nDenominator; 
public:
Fraction() //Default constructor
{
m_nNumerator = 0;
m_nDenominator = 0;
}
int GetNumerator() 
{
return m_nNumerator;
}
int GetDenominator()
{
return m_nDenominator;
}
double GetFraction()
{
return(m_nNumerator/m_nDenominator);
}
};
e.g. Destructor 
class A 
{
public :
A()
{
cout<<"A::A()"<<endl;
}
˜A()
{
cout<<"A:: ˜A()"<<endl;
}
};
void main()
{
char *P = new char[sizeof(A)]; 
A *ap = new A; 
ap->A:: ~A(); 
delete P;
}

Question 2:
Write any two similarities between constructors and destructors. Write the function headers for constructor and destructor of a class Flight. All India 2013
Аnswer:
Both have same name as the class in which they are declared. If not declared by user both are available in
a class by default, but they now can only allocate and deallocate memory from the objects of a class when an
object is declared or deleted.
e-g.

class Flight 
{
publiC:
Fliqht();  // Constructor for class Flight 
~F1ight();  // Destructor for class Flight
}

Question 3:
Answer the questions (i) and (ii) after going through the following class: Delhi 2012

class Tour  
{
int LocationCode;
char Location[20]; 
float Charges;
public:
Tour()    //Function 1
{
LocationCode = 1; 
strcpy(Location,"PURI");
Charges = 1200;
}
void TourPlan(float C)   //Function 2
{
cout<<LocationCode<<":"; 
cout<<Location<<":"<<Charges<<endl; 
Charges += 100;
}
Tour(int LC.char L[],float C) //Function 3
{
LocationCode=LC; 
strcpy(Location,L);
Charges = C;
}
~Tour()    //Function 4
{
cout<<"TourPlan Cancel 1ed"<<endl;
}
};
  1. In object oriented programming, what are Function 1 and Function 3 combined together referred as?
  2. In object oriented programming, which concept is illustrated by Function 4? When is this function called/invoked?

Аnswer:

  1. Function 1 and Function 3 combined together referred as constructor overloading, i.e. polymorphism.
  2. Function 4 indicates destructor. This function is called/invoked whenever an object goes out of the scope.

Question 4:
Answer the questions (i) and (ii) after going through the following class: All India 2012

class Travel
{
int PlaceCode; 
char Place[20]; 
float charges;
public;
Travel()    //Function 1
{
PlaceCode = 1;
strcpy(Place,"DELHI");
Charges = 1000;
}
void TravelPlan(float C) //Function 2 
{
cout<<PlaceCode<<":"<<P1ace<<":";
cout<<Charges<<endl;
}
~Travel()    //Function 3
{
cout<<"Travel Plan Cancelled"<<endl;
}
Travel(int PC, char P[],float C) //Function 4
{
PlaceCode = PC;
strcpy(Place,P);
Charges = C;
}
};
  1. In object oriented programming, what are Function 1 and Function 4 combined together referred as ?
  2. In object oriented programming, which concept is illustrated by Function 3? When is this function called/invoked ?

Аnswer:

  1. Function 1 and Function 4 referred as constructor overloading, i.e. polymorphism.
  2. Function 3 indicates destructor. It’s called whenever an object goes out of the scope.

Question 5:
Answer the questions (i) and (ii) after going through the following class: Delhi 2010

class TEST 
{
int Regno,Max,Min,Score; 
public:
TEST()    //Function 1
{
Regno = 101;Max = 100;
Min = 40;Score = 75;
}
TEST(int Pregno,int Pscore) //Function 2
{
Regno = Pregno;Max = 100;
Min = 40;Score=Pscore;
}
˜TEST()    //Function 3
{
cout<<"TEST over"<<endl;
}
void Display()    //Function 4
{
cout<<Regno<<":"<<Max<<":"<<Min; 
cout<< "\n[Score]"<<Score<<endi;
}
};
  1. As per object oriented programming, which concept is illustrated by Function 1 and Function 2 together?
  2. What is Function 3 specifically referred as, when do you think Function 3 will be invoked/called?

Аnswer:

  1. Constructor overloading or Polymorphism.
  2. Destructor invoked or called, when scope of an object gets over.

Question 6:
Answer the questions (i) and (ii) after going through the folio wing class: Delhi 2009

class WORK 
{
int WorkId; 
char WorkType; 
public:
˜W0RK()    //Function 1
{
 cout<<"Un-al1ocated"<<endl;
}
void status()    //Function 2
{
cout<<WorkId<<":"<<WorkType<<endl;
}
WORK()    //Function  3
{
WorkId = 10; WorkType = 'T';
}
W0RK(W0RK &W)    //Function  4
{
WorkId = W.WorkId+12;
WorkType = W.WorkType+1;
}
};
  1. Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above
    definition of class WORK is called automatically, when the scope of an object gets over? Is it known as constructor or destructor or overloaded function or copy constructor?
  2. WORK W; //Line 1
    WORK Y(W); //Line 2
    Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class WORK will be called on execution of statement written as Line 2? What is this function specifically known as out of destructor or copy constructor or default constructor?

Аnswer:

  1. Function 1 is called when an object goes out of scope. It is called destructor.
  2. Function 4 will be called and this function is called copy constructor.

Question 7:
Answer the questions (i) and (ii) after going through the folio wing class: All India 2009

class Job
{
int JobId; 
char JobType; 
public:
˜Job()    //Function 1
{
cout<<"Resigned"<<endl;
}
Job()    //Function  2
{
JobId = 10;
JobType = 'T';
}
void TellMe()    //Function  3
{
cout<<JobId<<":"<<JobType<<endl;
}
Job(Job &J)    //Function  4
{
JobId = J.JobId+10;
JobType = J.JobType+1;
}
};
  1. Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above
    definition of class Job is called automatically, when the scope of an object gets over? Is it known as
    constructor or destructor or overloaded function or copy constructor?
  2. Job P; //Line 1
    Job Q(P); //Line 2

Which member function out of Function 7, Function 2, Function 3 and Function 4 shown in the above definition of class Job will be called on execution of statement written as Line 2? What is this function specifically known as out of destructor or copy constructor or default constructor?

Аnswer:

  1. Function 1 is called when an object goes out of , scope. It is called destructor.
  2. Function 4 is called when Line 2 is executed. It is called copy constructor.

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

class Maths 
{
char Chapter[20]; 
int Marks; 
public :
Maths()   //Member Function 1
{
strcpy(Chapter, "Geometry");
Marks = 10;
cout<<"Chapter Initialised";
}
~Maths()    //Member Function 2
{
cout<<"Chapter Over";
}
};
  1. Name the specific features of class shown by Member Function 1 and Member Function 2 in the above example.
  2. How would Member Function 1 and Member Function 2 get executed?

Аnswer:

  1. Maths( ), the Member Function 1 is the constructor function. It constructs the object by allocating memory
    to its data members and by assigning values to them.
    ~Maths(), the Member Function 2 is the destructor function. It is responsible for destructing objects when
    they go out of scope.
  2. Member Function 1 will be executed every time an object of class Maths type is created.
    Member Function 2 will be executed every time an object of class Maths type goes out of scope.

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

class Science 
{
char Topic[20]; 
int Weightage; 
public:
Science()    //Function 1
{
strcpy(Topic,"Optics");
Weightage = 30; 
cout<<"Topic Activated";
}
˜Science()    //Function 2
{
cout<<"Topic Deactivated";
}
};
  1. Name the specific features of class shown by Function 7 and Function 2 in the above example.
  2. How would Function 1 and Function 2 get executed?

Аnswer:

  1. Science( ), the Function 1 is the constructor function. It constructs the object by allocating memory
    to its data members and by assigning values to them.
    ~Science( ), the Function 2 is the destructor function. It is responsible for destructing objects when they
    go out of scope.
  2. Member Function 1 will be executed every time an object of class Science type is created. Member
    Function 2 will be executed every time an object of class Science type goes out of scope.

Question 10:
What is default constructor? How is it different from destructor?
Аnswer:
A constructor is a member function having the same name as that of class and it gets invoked every7 time
when new object is created. It is used to construct and initialise object values. A constructor that accepts no
parameters is known as default constructor.
A destructor function has the same name as that of constructor function, preceded with a tilde ( ~ ) sign.
It gets invoked every time an object goes out of scope. It is used to destroy objects.

Question 11:
Given the following C++ code, answer the questions (i) and (ii).

class Readbook 
{
public:
Readbook()    //Function  1
{
cout<<"open the book"<<endl;
}
void Readchapter()    //Function  2
{
cout<<"reading chapter one"<<endl;
}
˜Readbook()    //Function 3 
{
cout<<"close the book"<<endl;
}
};
  1. In object oriented programming, what Function 7 is referred as and when does it get invoked/called?
  2. In object oriented programming, what Function 3 is referred as and when does it get invoked/called?

Аnswer:

  1. Function 1 is referred to as constructor and it gets invoked when an object gets created.
  2. Function 3 is referred to as destructor and it gets invoked when an object goes out of scope.

Question 12:
Given the following C++ code, answer the questions (i) and (ii).

class Testmeout 
{
public:
~Testmeout()    //Function  1
{
cout<<"Leaving the examination hall"; 
cout<<endl;
}
Testmeout()   //Function  2
{
cout<<"Appearing examination hall"; 
cout<<endl;
}
void Mywork()  //Function    3
{
cout<<”Attempting question"<<endl;
}
};
  1. In object oriented programming, what Function 1 is referred as and when does it get invoked/called?
  2. In object oriented programming, what Function 2 is referred as and when does it get invoked/called?

Аnswer:

  1. Function 1 is referred to as destructor and it gets invoked when an object goes out of scope.
  2. Function 2 is referred to as constructor and it gets invoked when an object gets created.

Question 13:
Identify the syntax error(s), if any

class ABC 
{
int x = 10 ; float y ;
ABC()
{
y = 5;
}
~()
{}
}
void main()
{
ABC a1,a2;
}

Аnswer:

class ABC 
{
int x = 10:    //Data member cannot //be initialised within //a class ABC
float y;
ABC()
{
y = 5;
}
~()    //Name of destructor missing
{}
}    //Missing semicolon(;) at //the class end
void main()
{
ABC a1,a2;
}

Computer ScienceChapterwise Question Bank for Computer ScienceNCERT Solutions

Primary Sidebar

NCERT Exemplar problems With Solutions CBSE Previous Year Questions with Solutoins CBSE Sample Papers

Recent Posts

  • Up From Slavery Summary CBSE Class 11 English Novel
  • NCERT Class 10 Science Lab Manual – pH of Samples
  • NCERT Solutions for Class 11 Hindi Core – काव्य भाग – मेरे तो गिरधर गोपाल दूसरो न कोई, पग घुँघरू बाधि मीरां नाची
  • Glimpses of India Extra Questions and Answers Class 10 English First Flight
  • ML Aggarwal Class 7 Solutions for ICSE Maths Chapter 16 Perimeter and Area Ex 16.1
  • NCERT Solutions for Class 12 Chemistry Chapter 12 Aldehydes, Ketones and Carboxylic Acids
  • Magnetic Effects of Electric Current Class 10 Important Questions with Answers Science Chapter 13
  • Paragraph Writing for Class 8 CBSE Format, Topics Exercises, and Examples
  • Question Tags Exercises for Class 8 CBSE With Answers – English Grammar
  • NCERT Solutions for Class 12 Hindi Core – पूरक पाठ्यपुस्तक-सिल्वर वैडिंग
  • Light Reflection and Refraction Class 10 Important Questions with Answers Science Chapter 10
  • ML Aggarwal Class 8 Solutions for ICSE Maths Chapter 18 Mensuration Ex 18.3
  • NCERT Solutions for Class 12 Chemistry Chapter 11 Alcohols, Phenols and Ehers
  • NCERT Solutions for Class 11 Hindi Core – गद्य भाग – जामुन का पेड़
  • MCQ Questions for Class 8 Science Chapter 16 Light with Answers

Footer

Maths NCERT Solutions

NCERT Solutions for Class 12 Maths
NCERT Solutions for Class 11 Maths
NCERT Solutions for Class 10 Maths
NCERT Solutions for Class 9 Maths
NCERT Solutions for Class 8 Maths
NCERT Solutions for Class 7 Maths
NCERT Solutions for Class 6 Maths

SCIENCE NCERT SOLUTIONS

NCERT Solutions for Class 12 Physics
NCERT Solutions for Class 12 Chemistry
NCERT Solutions for Class 11 Physics
NCERT Solutions for Class 11 Chemistry
NCERT Solutions for Class 10 Science
NCERT Solutions for Class 9 Science
NCERT Solutions for Class 7 Science
MCQ Questions NCERT Solutions
CBSE Sample Papers
NCERT Exemplar Solutions LCM and GCF Calculator
TS Grewal Accountancy Class 12 Solutions
TS Grewal Accountancy Class 11 Solutions