C++

C++ Constructors and Destructors

In C++, Constructor is a strong feature of a class. We know that in C++, we used to make various classes. In classes, we use it to make a function. So constructor is a special type of member function.

Destructor is also a function of a class in C++. Like constructor destructor also has some special features. Now we discuss both constructor and destructor in our topic.

Basic characteristics of Constructor:

  1. Constructor has a unique feature: both class name and constructor name must be the same.
  2. Constructor has not any return type as the normal function. So we cannot use any return keyword inside the constructor.
  3. Constructor behaves like a function, but it differs from a normal function.

Now we write a program, how to make a constructor in a class.

Programming example 1:

#include<iostream>
using namespace std ;
class Complex
{
  private :
   int  x , y ;
   public :
    Complex()  //create a constructor inside the class.
     {
       cout<< “ Example of a Constructor \n ” ;
     }
};


int main()
  {
    Complex com;
     return 0 ;
  }

Output:

Explanation:

Here we create a class named Complex. We declare two variables, x and y, under the private access modifier. Then we create a constructor in a public modifier similar to the class name. Here we do not write any return keyword for the constructor.

Constructor is called implicitly. When an object is getting memory constructor will run by default.

Classification of the constructor:

There are three kinds of constructors available in C++. They are

  1. Default.
  2. Parameterized.
  3. Copy.

a. Default Constructor:

Whenever we do not create any constructor for a class and create an object for that class, the constructor is implicitly called by the compiler. That type of constructor is called the default constructor. It has a special feature. There is no code inside its body, and it does not take any parameters.

Programming example 2:

#include<iostream>
using namespace std ;
class Complex
{
  Private :
   int  a , b ;
   public :
};

void main()
  {
    Complex com ;
  }

Output:
As this program has not any output statement, it has not any output.

Explanation:

Here we create a class complex. Inside the complex class, we do not create any constructor. But inside the main(), when we create an object named com, the compiler creates a default constructor in the object file. It is called the default constructor. This type of constructor has not any coding inside its body.

b. Parameterized Constructor:

If we want to pass some arguments inside the constructor, it is called a parameterized constructor.

Programming Example 3:

#include<iostream>
using namespace std ;
class Complex
{
  private :
   int  a , b ;
   public :
    Complex( int x , int y)  //create a  Parameterized constructor.
     {
       a = x ;
       b = y ;
       cout<< “ The values of a and b are”
                           <<a <<and<<b ;
     };

      int main()
        {
           Complex com ( 3 , 4 ) ;
           return 0 ;
         }

Output:

Explanation:

The above code is an example of a parameterized constructor. Here we pass two integers as arguments x and y inside the complex constructor. If we pass some values like 3 and 4 inside the com object as com( 3, 4 ) from the main function, that value goes to the constructor.

c. Copy Constructor:

When one class object is copied to another object of the same class, it is called a copy constructor. It means both objects hold the same value of the same class.

Programming Example 4:

#include<iostream>
class Example
{
  int x , y ;
   public :
    Example( int a , int b)  //create a constructor inside the class.
     {
       x = a ;
       y = b ;
     }

     void display()
       {
         cout << “ The values are :
                   << x <<and<< y ;
}
};

void main()
  {
    Example ex1 ( 50 , 60 ) ;
    Example ex2 = com1;
    ex2.display() ;

}

Output:

Explanation:

Here we create a parameterized constructor names complex. ex1 is an object of the class Example. Through the ex1 object, we pass two values, 50 and 60. In display(), the output will be shown at 50 and 60.

Now we copy the value of ex1 to another object, ex2. The result will be the same if we call the display() function through the ex2 object.

Destructor:

A destructor is another powerful feature like the constructor of a class. Its name will be the same as the name of the class as we saw in the constructor previously. But a symbol called tilde (~) is separated a destructor from a constructor in coding.

It also has no return type as a constructor and can never be static.
The main difference between constructor and destructor is that the destructor takes no arguments like the constructor. It means overloading is not possible in the case of a destructor.

Programming Example 5:

#include<iostream>
using namespace std ;
class Complex
{
  private :
   int a , b ;
   public:
    ~Complex()  //create a destructor inside the class.
     {
       cout<< “ Example of a Destructor\n ” ;
     }
};

      void fun()
       {
         Complex des ;  // creating the  object of the class
       }

int main()
  {
    fun() ;
    return 0 ;
}

Output:

Explanation:

Here we create a destructor names complex under the Complex class. Now we have to know what the reason that we use destructor is. Destructor is invoked implicitly by the compiler when an object will destroy. It is a misconception that destructors destroy the memory of the object. The fact is that destructor is the last method that runs. After that object is sure to be destroyed, which means in main() when the fun() is run, the object has to be destroyed.

Conclusion:

After discussing the concept of Constructor and Destructor, we will conclude that Constructor and Destructor are important member functions of the class in C++. Both Constructor and Destructor enhance the features of class very efficiently.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com