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]

×