Constructor

A constructor is a special member function whose task is to initialize the objects of its class. Its name is same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class. Its name is same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class.

Example:

1.   #include<iostream>
2.   using namespace std;
3.   class code
4.   {
5.       int a,b;
6.      public:
7.      code()
8.      {
9.        cout<<"Object created"<<endl;
10.    }
11.   };
12.     main()
13.   {
14.    code s1;
15.     return 0;
16.     }

Here is the simple example to understand the concept of constructor. A class namely 'code' is defined in Line 3. A constructor is defined in Line 7. The name of the constructor should be same as the name of class. Constructor is simply a function whose name is same as class name. Object of the class is created in Line 14. There is no need to write any statement to invoke the constructor function as we do in normal function. When object of the class is created, automatically calls the constructor.

Characteristics of Constructor

i. They should be declared in public section.
ii. They are invoked automatically when the objects are created.
iii. They do not have return types, so cannot return values.
iv. They cannot be inherited (but a derived class can call the base class constructor.
v. They can have default arguments.
vi. Constructors cannot be virtual.

Significance of Constructor

        Constructor can be used to set values of the members of an object, either to default or to user defined values.

Types of Constructor

i. Parameterized Constructor : It may be necessary to initialize the various data elements of different objects with different values when they are created. C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.

Example:

1.   #include<iostream>
2.   using namespace std;
3.   class num
4.   {
5.     int x, y;
6.     public:
7.     num (int a, int b)
8.     {
9.       x = a;
10.     y = b;
11.    }
12.   void display( )
13.    {
14.      cout<< x <<"   "<< y << endl;
15.    }
16.   };
17.    int main( )
18.    {
19.      sum s1 (10,20);
20.      sum s2 (30,40);
21.     s1.display( );
22.     s2.display( );
23.     return 0;
24.    }

OUTPUT:
10   20
30   40


ii. Default Constructor : A constructor with no parameters is known as default constructor.

Example:

#include <iostream>
using namespace std;
class num
{
  int a,b;
  public:
  num( )
  {
    a=0;
    b=0;
   }
void display( )
 {
   cout<< "Value of a is " << a << endl;
   cout<< "Value of b is " << b << endl;
  }
};
int main( )
{
  num n;
  n.display( );
  return 0;
}

OUTPUT:
Value of a is 0
Value of b is 0


iii. Copy Constructor : A constructor that creates a copy of the previous constructor is known as copy constructor.

Syntax:

class num
{
  ...................
  ...................
  ...................
}
int main( )
{
num n;
num n1(n);      --> copy constructor
}

A copy constructor takes the object of the constructor that is to be copied as an argument.

Example:

#include <iostream>
using namespace std;
class num
{
  int code;
  float  price;
public:
num (int c, float p)
 {
   code = c;
   price = p;
  }
num (num & n)
 {
   code = n.code;
   price = n.price;
  }
void display( )
 {
  cout << "code: "<<code<<endl;
  cout<< "price: "<<price<<endl;
 }
};
int main( )
{
  num n1 (50, 25.5);
  num n2 (n1);
n1.display( );
n2.display( );
return 0;
}

OUTPUT:
code: 50
price: 25.5
code: 50
price:25.5


Destructor

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

Syntax:
class class_name

~class_name ( )

Characteristics of Destructor

i. Destructors have the same name as that of the class.
ii. A tilde (~) sign is used before a the name of the destructor.
iii. Destructors do  not have return type and not even void.
iv. Derived class can call destructors of the base class but cannot be inherited.
v. Destructors can be virtual.
vi. Destructors does not have any arguments.

Example:

#include<iostream>
using namespace std;
class num
{
    int a,b;
public:
    num( )
    {
        cout<<"Object created"<<endl;
    }
    ~num( )
    {
        cout<<"Object destroyed"<<endl;
    }
};
main()
{
   num s1;
   {
       num s2;
   }
   cout<<"End of program"<<endl;
   return 0;
}
OUTPUT:
Object created
Object created
Object destroyed
End of program
Object destroyed


In the main ( ) function, s1 is the object of the constructor. Whenever a object of a class is created constructor is called. Since, the object is created so constructor is called and the first output is "Object created " which is defined inside a constructor. Again, when the object s2 is created, constructor is called and the second output is "Object created". Now, whenever there is the end of definition section, destructor is called. In this case, num s2 is inside two curly braces. When the compiler comes out of these braces, destructor is called and hence our third output is "Object destroyed". Our fourth output is " End of program" which is obvious and need no explanation. Now, our last output is "Object destroyed ". It is because when program ends, the compiler automatically calls the destructor.