NCERT Solutions Class 12 Computer Science (C++) Chapter -4 (Constructor and Destructor)

NCERT Solutions Class 12 Computer Science (C++) Chapter -4 (Constructor and Destructor)

NCERT Solutions Class 12 Computer Science (C++) from class 12th Students will get the answers of Chapter-4 (Constructor and Destructor) This chapter will help you to learn the basics and you should expect at least one question in your exam from this chapter.
We have given the answers of all the questions of NCERT Board Computer Science (C++) Textbook in very easy language, which will be very easy for the students to understand and remember so that you can pass with good marks in your examination.
Solutions Class 12 Computer Science (C++) Chapter -4 (Constructor and Destructor)
NCERT Question-Answer

Class 12 Computer Science (C++)

Chapter-4 (Constructor and Destructor)

Questions and answers given in practice

Chapter-4 (Constructor and Destructor)

TOPIC-1
Constructors

Short Answer Type Questions-I [2 marks each]

Question 1:
Write four characteristics of constructor function used in a class.    [Delhi, 2014]

Answer:
(i) A constructor function has no return type.
(ii) Name of constructor is same as that of class.
(iii) A constructor can be called while creating objects.
(iv) If there is no constructor declared in class then default constructor is invoked. (1/2 mark for each characteristic).

Question 2:
What is a copy constructor ? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it.    [Delhi, 2015]

Answer:
A copy constructor is an overloaded constructor in which an object of the same class is passed as reference parameter,

class point 
{
int x ; 
public :
point ( ) {x = 0 ; } point (Point &p)    // copy
constructor
{x = p . x;}
};
void main ( )
{
point p1;
point p2 (p1) ;    // copy constructor is called here
//
OR 
point p3 = p1 ; //Copy constructor is called here
}

(1 1/2 Mark to be awarded if the copy constructor is explained with an appropriate example)
OR
(1 Mark for correct explanation of copy constructor only without an example)
(1/2 Mark for correct declaration of an object)

Question 3:
Answer the questions (i) and (ii) after going through the following class :    [Delhi, 2015]

class Exam
{
 int Rollno; 
char Cname [25]; 
float Marks; 
public :
Exam( ) //Function 1 
Rollno = 0;
strcpy (Cname = " " );
 Marks = 0.0;
}
Exam (int Rno, char candnam [ ] //Function 2
{
Rollno = Rno;
strcpy(Cname, candname);
}
~Exam ( ) //Function 3
{
cout<<"Result  will    be intimated shortly'' <<endl;
}
void Display ( ) //Function 4
{
cout<<"Roll no :"<<Rollno; 
cout<<"Name :"<<Cname; 
cout<<"Marks :"<<Marks;
}
} ;

(i) Which OOP concept does Function 1 and Function 2 implement. Explain ?
Answer:
Constructor Overloading/Polymorphism, as multiple definitions for Constructors are given in the same scope. Function 1 is a Default constructor and function 2 is a Parameterized constructor.
[1/2 mark for the concept]
[1/2 mark for explanation]
What is Function 3 called ? When will it be invoked ?
Answer:
(ii) Function 3 is a Destructor which is invoked when the object goes out of scope.
[1/2 mark for identification]
[1/2 mark for time of invocation]

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

class planet 
{
char name[20];
char distance[20] 
public:
planet()    //Function 1
{
strcpy(name, "Venus");
strcpy(distance,"38 million km");
}
void display(char na[],char d[]) //Function 2 
{
cout<<na<<"has"<<d<<" distance from Earth"<<endl ;
}
planet (char na[],char d[]) //Funciton 3 
strcpy(name,na); 
strcpy(distance,d);
}
~Planet()  //Function 4
{
cout<<"Planetarium time over !!! "«endl;
}
 };

(i) What is Function 1 referred as ? When will it be executed ?
(ii) Write suitable C++ statement to invoke Function 2.

Answer:
(i)
 Constructor
It will be excuted at the time of object creation.
(1 1/2 Mark for each correct answer)
(ii) planet p;
p.display(“Pluto”,”7.5 Billion Km”);
(1/2 Mark for each correct answer)

Question 5:
Observe the following C++ code and answer the question (i) and (ii):    [O.D, 2015]

class passenger
{
long PNR; 
char Name [20]    ;
public :
passenger    (    )    //Function    1
{cout<<"Ready"<<endl; } 
void Book (long P, char N[ ])
//Function 2
{ PNR = P; strcpy (Name, N) . ; } void print (    )    //Function    3
{ cout<<PNR << Name << endl; } -Passenger (    )    //Function    4
{ cout<<"Booking cancelled ! "<<endl
}
} ;

(i) Fill in the statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code:
void main ( )
{
Passenger P;
__________   //Line 1
__________   //Line 2
} // Ends here

Answer:
(i) P. Book (1234567, “Ravi”);    //Line    1
P. Frint ();                                      //Line    2
(1/2 Mark for writing each correct Function)
(ii) Which function will be executed at } // Ends here ? What is this function referred as ?
(ii) Function 4
OR
~ Passenger ()
It is a Destructor function.
(1/2 Mark for writing Function 4  OR  ~Passenger ())
It is a Destructor function.
(1/2 Mark for referring Destructor)

Question 6:
Answer the questions (i) and (ii) after going through the following C+ + class : [CBSE SQP 2015]

class Stream
{
int StreamCode ; 
char Streamname [20]; float fees;
public:
Stream( )    //Function 1
{
StreamCode=1;
strcpy (Streamname,"DELHI"); 
fees=1000;
}
void display(float C) //Function 2
{
cout<<StreamCode<<":"<<Streamname <<fees<<endl;
}
~Stream( )    //Function 3
{
cout<<"End of Stream Object"<<endl;
}
Stream (int SC,char S[ ],float F) ;
//Function 4
} ;

(i) In Object Oriented Programming, what are Function 1 and Function 4 combined together referred as ? Write the definition of function 4.
(ii) What is the difference between the following statements ?
Stream S(11,”Science”,8700);
Stream S=Stream(11,”Science”,8700);

Answer:
(i) Constructor Overloading
Stream (int Sc, char S[], float F)
{
StreamCode = Sc;
strcpy(Streamname, S);
fees = F;
}    [1]
(ii) The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method.
Stream S(ll,” Science,8700); – In this statement constructor is called implicitly.
Stream S = Stream(ll,”Science,8700); – Here constructor is called explicitly.    [1]

Question 7:
Answer the question (i) and (ii) after going through the following class:

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
:
:
}

(i) Name the feature of Object Oriented Programming demonstrated by Function 1, Function 2 and Function 3. [CBSE Comptt., 2013]
(ii) Write statements in C + + to execute function 3 & Function 4 inside the main () function.

Answer:
(i) Constructor overloading is demonstrated. [1] (ii) void main ()

{ 
Book B;
B. Book (5,'Jay');
B. Buy();
} [1]

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

class Race
{
int CarNo, Track; 
public:
Race ( ) ;                         //Function 1
Race (int CN);               //Function 2
Race (Race &R);          //Function 3
void Register! );          //Function 4
void Drive( );              //Function 5
};
void main( )
{
Race R;
}

(i) Out of the following, which of the options is correct way for calling Function 2?
Option 1—Race T(30);
Option 2—Race U(R);
(ii) Name the feature of Object Oriented
Programming, which is illustrated by Function 1, Function 2, and Function 3 combined Together.    [O.D, 2013]

Answer:
(i) Option 1: Race T(30) is the correct way for calling Function 2.    [1]
(ii) Constructor Overloading, i.e., Polymorphism. [1]

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

class Motor 
{
int MotorNo, Track; 
public:
Motor ();                          //Function 1
Motor (int MN);                   //Function 2
Motor (Motor & M) ;               //Function 3
void Allocate();                  //Function 4
void Move();                      //Function 5
} ;
void main()
{
MotorM;
}

(i)  Out of the following, which of the options is correct way for calling Function 2?
Option 1—Motor N(M);
Option 2—Motor P(10);
(ii) Name the feature of Object Oriented Programming, which is illustrated by Function 1, Function 2 and Function 3 combined Together.    [Delhi, 2013]

Answer:
(i) Option 2: Motor P(10) is the correct way for calling Function 2.    [1]
(ii) Constructor Overloading, i.e., Polymorphism.    [1]

Question 10:
What is constructor overloading ? Give an example to illustrate the same. [CBSE SQP 2013]

Answer:
Declaring constructors with different parameters in a program is called Constructor overloading. [1]
Example:

class Circle
{
int radius, area; public:
Circle()    //Constructor
{
radius=0;
}
Circle(int r)//Constructor Overloaded
{
radius=r;
}
};    [1]

Question 11:
What is a Copy Constructor ? Give a suitable example of Copy Constructor using a class in C+ + .    [CBSE Comptt. 2016]

Answer:
Copy Constructor
• A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object.
• A copy constructor is a constructor of the form classname (classname &). The compiler will use the copy constructors
• whenever you initialize an instance using the values of another instance of the same type.
• Copying of objects is achieved by the use of a copy constructor and a assignment operator.
Example:

class Sample {int i, j ; 
public:
Sample(int a, int b) // constructor {i=a;j =b;}
Sample (Sample & s)
//copy constructor
{j=s.j ; i=s.j;
cout << "\n Copy constructor
working \n";
}
void print (void)
{cout<<i<<j<<"\n";
}
};

Note : The argument to a copy constructor is passed by reference, the reason being that when an argument is passed by value, a copy of it is constructed. But the copy constructor is creating a copy of the object for itself, thus, it calls itself. Again the called copy constructor requires another copy, so again it is called. In fact it calls itself again and again until the compiler runs out of the memory so, in the copy constructor, the argument must be passed by reference.    [2]

Question 12:
Observe the following C++ code and answer the questions (i) and (ii) Assume all necessary files are included :    [CBSE Comptt 2016]

class COMIC
{
long CCode; 
char CTitle [20]; 
float CPrice; 
public :
COMIC ()    //Member Function 1
{
cout<<"got..."<<endl; 
CCode=100;strcpy(CTitle, "Unkown"); CPrice=100;
}
COMIC(int C, char T[],float p) //Member Function 2
CCode=C;
strapy(CTitle, T);
CPrice=P;
}
void Raise(float P)
//Member Function 3
{
void Disp()
//Member Function 4
{ 
cout<<CCode<<";"<<CTitle<< " ; "<<CPrice<<endl;
}
-COMIC() //Member Function 5
{
cout<<"COMIC discarded! "«endl;
}
} ;
void main()                               //Line  1
{                                         //Line  2
COMIC Cl,C2(1001,"Tom and Jerry",75)      //Line  3
for (int 1=0;I<4;I++) /                   //Line  4 
{                                         //Line  5
Cl.Raise(20);C2,Raise(15);                //Line  6
Cl.DispO ;C2.Disp() ;                     //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
• Data Hiding
• Polymorphism
• Inheritance
(ii) How many times the massage “COMIC Discard!” will be displayed after executing the above C++ code ? Out of Line 1 to Line 9, which line(s) is responsible to display the message “COMIC Discarded!”

Answer:
(i) Polymorphism
(1 Mark for mentioning the correct option name)
(ii) 2 times Line 9
(1/2 Mark for writing correct number of times)
(1/2 Mark for writing correct line numbers)

Short Answer Type Questions-II [3 marks each]

Question 1:
Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute ?    
[Delhi, 2016]
Answer:
Constructor is a member function which creates an object.
Destructor is a member function which deallocates the memory occupied by an object

class ABC
{
int a, b; 
public 
ABC () //Constructor
{a=0;b=0;}
~ ABC () //Destructor
{
}
};

Question 2:
Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included.    [Delhi, 2016]

class FICTION
{
long FCode; 
char FTitle [20]; 
float FPrice;
Public:
FICTION()
//Member Function 1
{
cout < <"Bought"< < endl;
FCode=100;strcpy(FTitle,"Noname ");FPrice=50;
}
FICTION(int C, char T[], float P) //Member Function 2
{
FCode=C;
strcpy(FTitle,T);
FPrice=P;
}
void Increase(float P) //Member Function 3
{
FPrice+=P;
}
void Show()
//Member Function 4
{
cout<<Fcode<< " : "<<FTitle<<":"<<FPrice<<endl;
}
~FICTION ( ) //Member Function 5
}
};
void    main()                       //Line 1
{                                    //Line 2
FICTION FI, F2(101,"Dare",75);      // Line 3
for    (int 1=0;I<4;I++)            //Line  4
{                                   //Line  5
F1.Increase(2 0);F2.
Increase(15);                       //Line 6   
FI.Show();                          //Line 7
F2.Show()                           //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
> Data Hiding
> Polymorphism
> Inheritance
(ii) How many times the message “Fiction removed!” will be display ed after executing the above C + + code ? Out of Line 1 to Line 9, which line is responsible to display the message “Fiction removed! ” ?

Answer:

(i) Polymorphism    1
(ii) 2 times    1
Line 9.    1

Topic-II
Destructors
Short Answer Type Questions-I [2 marks each]

Question 1:
Write any two similarities between Constructors and Destructors. Write the Function headers for constructor and destructor of a class Flight. [O.D, 2013]

Answer:
Similarities between Constructor and Destructor:
(i) Both have same name as of the class name.
(ii) Both handle the objects of a class.    [1]
Function Headers:

Constructor:
Flight ( )
Destructor:
~ Flight ( )    [1]

Question 2:
Write any two differences between Constructors and Destructors. Write the function headers for constructor and destructor of a class member. [Delhi, 2013]

Answer:

ConstructorDestructor
(i)Invoked every time when the object is createdInvoked everytime when object goes out of scope.
(ii)Used to construct and initialize object values.Used to destroy objects

[1]
Function Headers:

Constructor:
Member ( )
Destructor:
~ Member ( )    [1]

Question 3:
Answer the following questions (i) and (ii) aftergoing through the following class :

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<<PlaceCode<<":"<<LPlace<<":" <<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 cancelled" <<endl;
 }
 };

(i) In object oriented programming, what are Function 1 and Function 3 combined together known as?
(ii) In object oriented programming, which concept is illustrated by Function 4? When is this function called/invoked?
[Delhi, 2012]
Answer:
(i) Function 1 and 3 are together referred as Constructor overloading.    [1]
(ii) Function 4 illustrates Destructor.    [1/2]
It is invoked when object goes out of scope. [1/2]

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

class Travel
int PlaceCode;
char Place [20];
float Charges;
public:
Travel( )                                        //Function 1
{
PlaceCode=l;
strcpy (Place,"DELHI");
Charges=l000;
}
void TravelPlan( float C)                         //Function 2
{
cout<<PlaceCode<<":"<<Place<<":"« Charges<<endl;
Charges+=100;
}
~Travel ( )                                       //Function 3
{
cout<<"Travel Plan cancelled"<<endl;
}
Travel (int PC, charP [ ], float C)
                                                  //Function 4
{
PlaceCode=PC;
strcpy(Place, P);
Charges=C;
}
};

(i) In object oriented programming, what are Function 1 and Function 4 combined together known as?
(ii) In object oriented programming, which concept is illustrated by Function 3? When is this function called/invoked?
[O.D, 2012]

Answer:
(i) Function 1 and 4 are together referred as Constructor overloading.    [1]
(ii) Function 3 illustrates Destructor.    [1/2]
It is invoked when object of the class goes out of scope.    [1/2]

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

class schoolbag
{
int pockets;
public:
schoolbag()                                //Function 1
{ pockets=30;
cout<<"The bag has pockets"<<endl;
}
void company()                             //Function 2
{
cout<<"The company of the Bag is ABC" «endl ;
}
schoolbag(int D)                           //Function 3
{
pockets=D;
cout<<"Now    the    Bag    has
pockets"<<pockets <<endl;
}
-schoolbag()                                 //Function 4
{
cout<<" Thanks " < < endl ;
}
};

(i) In Object Oriented Programming, what is Function 4 referred as and when does it get invoked/called ?
(ii) In Object Oriented Programming, which concept is illustrated by Function 1 and Function 3 together ?

Answer:
(i) Function 4 is referred as Destructor, and this function is called / invoked as soon as the scope of the object gets over.    [1]
(ii) Here constructor overloading concept is illustrated by function 1 and function 3 together.    [1]

UNIT – 1 : OBJECT ORIENTED PROGRAMMING IN C++

UNIT – II : DATA STRUCTURES

UNIT – III : DATABASE MANAGEMENT SYSTEM AND SQL

UNIT – IV : BOOLEAN ALGEBRA

UNIT – V : NETWORKING & OPEN SOURCE SOFTWARE