home
C++ TUTORIALS

CONSTRUCTORS AND DESTRUCTORS

We have used,so far, member functions to provide initial values to private members.These funtions cannot be used to initialize the member variables at the time of creation of the objects.C++ provides a special member function called constructor which enables an object to initialize itself when it is created and another special function destructor that destroys the object when it is no lonegr required.

CONSTRUCTORS

A constructor is a special member function which is used to initialize the objects of its class. It is special because its name is same as the name of the class.
The characteristics of a constructor are:

	
class city { int m,n; public: city(void); //constructor declared ..... ..... }; city :: city(void) //constructor defined { m=0; n=0; }

TYPES OF CONSTRUCTORS

  1. Default Constructor
  2. Parameterised Constructor
  3. Copy Constructor

1. DEFAULT CONSTRUCTOR

A cnstructor that accepts no arguements is called a default constructor. The default constructor for class city is city::city(). If no such constructor is defined, then compiler itself supplies a default constructor.

2. PARAMETERISED CONSTRUCTOR

The constructor that takes arguements is called a parameterised constructor.C++ permits us to initialize the various members of different objects with different objects by passing arguements to the constructor.

	
class city { int m,n; public: city(int x,int y); } city :: city(int x,int y) { m=x; n=y; }

The object declaration statement: city city1; may not work. We must pass initial values to the constructor function.This can be done in two ways:

 CONSTRUCTOR WITH DEFAULT ARGUEMENTS

 It is possible to define a construcor with default arguements.
city(int x,int y=4);
The default value of arguement y=4;
It is important to distinguish between the default constructor city::city(); and the constructor with default arguemnt city::city(int x=0); Statement such as: city city1; will create an ambiguity.

3. COPY CONSTRUCTOR

A copy constructor is used to declare and initialize an oject from another object.For example, the statement: city obj2(obj1); or city obj2 = obj1; would define the object obj2 and initiaze it with the values of obj1.

Copy constructor takes a reference to an object of the same class as itself as an arguement. Consider the following code:

	
#include< iostream> using namespace std; class code { int id; public: code() { } code(int a) { id=a; } code(code &x) { id=x.id; } void display() { cout<< id; } }; int main() { code A(100); //parameterised constructor called code B(A); //copy constructor called code C=A; //copy constructor called cout<<"\n id of A="; A.display(); cout<<"\n id of B="; B.display(); cout<<"\n id of C="; C.display(); }
   Output of the program is: 
	id of A=100
	id of B=100
	id of C=100

	

A reference variable has been used as an arguement to the copy constructor. We cannot pass the arguement by value to the copy constructor.



DESTRUCTORS

A destructor as the name implies, is used to destroy the objects that have been created by a constructor. A destructor is a member function whose name is same as the class name but preceded by a tilde(~). For example:

		~city() { }

The characteristics of a destructor are: It is good practice to use destructors in a program since it releases memory for future use. Consider the following code to understand the calling of constructors and destructors.

	
#include< iostream> using namesapce std int count=0; class test { public: test() { count++; cout<<"\n constructor of object "<< count<<"called"; } ~test() { cout<<"\n destructor of object "<< count<<"called"; count--; } }; int main() { cout<<"Inside main.."; test T1; { //Block 1 cout<<"\nInside Block 1"; test T2,T3; cout<<"\nleaving block 1"; } cout<<"\nLeaving main"; }
	Output of the program is:
	   Inside main..
	   constructor of object 1 called
	   Inside Block 1
	   constructor of object 2 called
	   constructor of object 3 called
	   leaving block 1
	   destructor of object 3 called
	   destructor of object 2 called
	   Leaving main
	   destructor of object 1 called