C++: Constructors and Destructors


It enables an object to initialize itself when it is created and knows as automatic initialization.
Its name is same as the class and it is invoked whenever an object of its associated class is created.

   class integer {
              int m,n;
              integer(void);  //constructor declared
    integer::integer(void){ //constructor defined

Now object is created
        integer int1;
Here data members m and n are initialized to zero.

A constructor that accepts no parameters is called the default constructor.
   for class A is A::A()

If no constructor is defined, then the compiler supplies a default constructor.
         A a;
invokes default constructor.

Special characteristics of constructor function:

They should be declared in the public section.

They do not have any return type, even void.

They cannot be inherited, though a derived class can call the base class constructor.

Parameterized Constructors:

The constructors that can take arguments are called parameterized constructor.

        class integer{
                   int m,n;
                  integer(int x, int y); // parameterized constructor
      integer :: integer (int x, int y){
           m=x; n=y;

Now object declaration
     integer int1 ;//will not work

correct statements are
     integer int1=integer(12, 45); //explicit call

     integer int1(12, 45); //implicit call

The constructor function can also be defined as inline function.

    class integer{
                   int m,n;
                  integer(int x, int y); // Inline constructor
                        m=x; n=y;

Multiple Constructors in a Class:

    class integer{
                    int m,n;
              integer(){m=0;n=0;} //constructor 1
              integer(int a, int b) //constructor 2
              {m=a; n=b;}
              integer(integer &i) //constructor 3
              {m=i.m; n=i.n;}

integer I1;
invoke first constructor and set both m and n of I1 to zero.

integer I2(24, 45);
invoke second constructor and initialize m to 24 and n to 45.

integer I3(I2);
invoke third constructor which is copy constructor and copies the values of I2 into I3.

 Constructors with Default Arguments:

      complex(float real,float imag=0);

The default value of argument imag is zero.
      complex C(5.0);
assigns the value 5.0 to the real variable and 0.0 to the imag variable.

The default argument constructor can be called with either one argument or no argument. When called with no arguments, it becomes a default constructor.

Copy Constructor:

integer(integer &i);

A copy constructor is used to declare and initialize an object from another object.

     integer I2(I1);
would define the object I2 and at the same time initialize it to value of I1.

     integer I2=I1;
is equivalent to above statement.

The process of initializing through a copy constructor is known as copy initialization.

Dynamic  Constructor:

Allocation of memory toi objects at the time of their construction is known as dynamic construction of objects.

The memory is allocated with the help of new operator.


A destructor is uesd to destroy the objects that have been created by a constructor.

Its name is same as that of class but ius preceded by a tilde.


A destructor never takes any argument nor does it return any value.

Whenever new is used to allocate memory in the constructors,we should use delete to free that memory.

                < Happy Coding>