MENU

Chapter 4 Constructor and Destructor Solutions

Question - 1 : -
Write four characteristics of constructor function used in a class.  

Answer - 1 : -

(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. 

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.   

Answer - 2 : -

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
}

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

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'' <
}
void Display ( ) //Function 4
{
cout<<"Roll no :"<
cout<<"Name :"<
cout<<"Marks :"<
}
} ;
(i) Which OOP concept does Function 1 and Function 2 implement. Explain ?

Answer - 3 : -

(i) 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.
(ii) Function 3 is a Destructor which is invoked when the object goes out of scope.

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

(i) Constructor
It will be excuted at the time of object creation.
(ii) planet p;
p.display(“Pluto”,”7.5 Billion Km”);

Question - 5 : -
Observe the following C++ code and answer the question (i) and (ii):   

class passenger
{
long PNR; 
char Name [20]    ;
public :
passenger    (    )    //Function    1
{cout<<"Ready"<
void Book (long P, char N[ ])
//Function 2
{ PNR = P; strcpy (Name, N) . ; } void print (    )    //Function    3
{ cout<
{ cout<<"Booking cancelled ! "<
}
} ;
(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 - 5 : -

(i) P. Book (1234567, “Ravi”);    //Line    1
P. Frint ();                                      //Line    2
(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.

Question - 6 : -
Answer the questions (i) and (ii) after going through the following C+ + class : 

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<
}
~Stream( )    //Function 3
{
cout<<"End of Stream Object"<
}
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 - 6 : -

(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.
(ii) Write statements in C + + to execute function 3 & Function 4 inside the main () function.

Answer - 7 : -

(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.    

Answer - 8 : -

(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.  

Answer - 9 : -

(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.

Answer - 10 : -

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]

×