MENU

Chapter 4 Constructor and Destructor Solutions

Question - 11 : - What is a Copy Constructor ? Give a suitable example of Copy Constructor using a class in C+ + .

Answer - 11 : -

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

Question - 12 : -
Observe the following C++ code and answer the questions (i) and (ii) Assume all necessary files are included :    

class COMIC
{
long CCode; 
char CTitle [20]; 
float CPrice; 
public :
COMIC ()    //Member Function 1
{
cout<<"got..."<
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<
}
-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 - 12 : -

(i) Polymorphism
(ii) 2 times Line 9

Question - 13 : - Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute ?

Answer - 13 : -

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 - 14 : -
Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included.   

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

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

Question - 15 : -
Write any two similarities between Constructors and Destructors. Write the Function headers for constructor and destructor of a class Flight. 

Answer - 15 : -

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 - 16 : -
Write any two differences between Constructors and Destructors. Write the function headers for constructor and destructor of a class member. 

Answer - 16 : -

Constructor

Destructor

(i)

Invoked every time when the object is created

Invoked 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 - 17 : -
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<
Charges+=100;
Tour (int LC, char L[ ], float C)
                                                      //Function 3
{
LocationCode=LC; 
strcpy(Location, L);
Charges=C;
~Tour( )                                              //Function 4
{
 cout<<"TourPlan cancelled" <
 }
 };
(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?

Answer - 17 : -

(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 - 18 : -
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<
Charges+=100;
}
~Travel ( )                                       //Function 3
{
cout<<"Travel Plan cancelled"<
}
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?

Answer - 18 : -

(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 - 19 : -
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"<
}
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"<
}
-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 - 19 : -

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

×