Constructors and Destructors
Constructors and Destructors
Introduction:
It is sometimes convenient if an object can initialize itself when it is first created, without the
need to make a separate call to a member function. This is possible with special member
functions. Automatic initialization is carried out using such a special member function called
constructor. Generally, we can say, a constructor constructs the data members of an object.
i.e., initialized as a value to the data members.
“A constructor is a special member function that is used in classes to initialize the objects of a
class automatically”.
A constructor is a member function of a class with the same name as that of the class. A
constructor is defined like other member functions of a class. It can be defined either inside
the class definition or outside the class definition.
It is called constructor because it constructs the values of data members of the class.
1. A Constructor always has name that is same as the class name of which they are the
members. This will help the compiler to identify that they are the constructors.
2. There is no return type for constructors (not even void). Since, the constructor is called
automatically by the system, there is no program for it to return anything to; a return
value would not make sense.
4. A constructor is invoked automatically when objects are created. Constructors can have
default arguments.
6. The constructors make implicit calls to the operators new and delete when
memory allocation is required.
Declaration of constructor inside the class definition
Example 9.1: class test
int m, x, y; public:
{
x = 0; y = 0;
Note that when a constructor is declared for a class, initialization of class objects is done
automatically.
Types of constructors:
There are three types of constructors, namely:
Default constructor
Parameterized constructor
Copy constructor
Default Constructor
A constructor which does not accept any arguments is called a zero argument constructor. It is
also known as default constructor. The default constructors are useful when the objects are
need to be created without having to type initial values. Default constructor simply allocated
memory to data members of objects.
Example : student() { }
Parameterized constructors are also invoked automatically, whenever objects with arguments
are created. The parameters are used to initialize the object.
1. Explicit call
2. Implicit call
3. Initialization at the time of declaration with = operator
Explicit call
In explicit call, declaration of an object is followed by assignment operator, constructor name
and argument list enclosed in parenthesis.
#include<iostream.h>
class num
private:
num(int p, int q)
a = p,b = q;
void display()
{
cout<<"a = "<<a<<" and b = "<<b<<endl;
};
void main()
obj1.display();
obj2.display();
In the above program code the objects obj1 and obj2 are called explicitly by using
parameterized constructor.
Implicit call
An Implicit call means the declaration of the object is followed by argument list enclosed in
parentheses.
class num
private:
num(int m, int n)
{ a = m, b = n; } void display()
};
void main()
In the above code segment, there are two constructors, one is simpleinterest() and another
one is simpleinterest(float x, float y, float z). The first one is the default constructor and the
second one is the three-argument constructor. Destructors:
A destructor will be called automatically when an object is destroyed. Destroying an object
means, de-allocating all the resources such as memory that was allocated for the object by the
constructor. A destructor is a special member function that will be executed automatically
when an object is destroyed. It will have, like constructor, the name same as that of the class
but preceded by a tilde (~).
{
private:
//data variables
//method public:
classname(); //constructor
~classname(); //destructor
• The destructor name is same as that of class. The first character must be tilde (~).
• Destructors do not have a return value. Destructor can never return a value.
• They take no arguments. Therefore destructors cannot be overloaded.
• The most common use of destructors is to de-allocate memory that was allocated for the
object by the constructor. Also, they are declared public.
.
∗∗∗∗∗