Chapter – 5. CONSTRUCTORS & DESTRUCTORS

DELHI: 2007

2.a. Differentiate between Constructor and Destructor function in context of Classes and Objects Using C++? 2

Ans:

Constructor :
Purpose: Is used to intitialize the objects of that class type with a legal initial value
Name: The name of the class
Calling: It will be called automatically at the time of creation of the object. Ie Implicite calling
Return Type: No return type not even void

Destructor :
Purpose: Is used to destroy the objects that have been created by a constructor
Name: The name of the class preceded by a ~.
Calling: It will be called automatically at the time of destruction of an object. Ie Implicite calling
Return Type: No return type not even void

Constructor: A constructor is used to intitialize the objects of that class type with a legal initial value.If a class has a constructor, each object of that class will be initialized before any use is made of the object.
(A member function with the same name as its class is called Constructor and it is used to initialize the objects of that class type with a legal initial value. )

Destructor: A destructor is used to destroy the objects that have been created by a constructor. A destructor destroys the values of the object being destroyed.

2.b. Answer the question (i)and (ii)after going through the following class: 2

class Maths
{
char Chapter[20]
int Marks;
public: Maths()
//Member Function 1
{
strcpy (Chapter, “Geometry”); Marks=10;
cout <<”Chapter Initialised “;
}
-Maths() //Member Functions 2
{
cout<<”Chapter Over”;
}
};