Difference Between Constructor and Destructor

The class is at the very core of object-oriented programming (OOP). It’s like a blueprint for creating objects that provides initial values for member variables and member functions. It’s the basic building block of OOP that defines the nature of a future object. The most important thing about a class is that it defines a new data type and once defined, it can be used to create objects of that type. So it would be suffice to say a class is a template for an object, and an object in fact is an instance of a class. It is used to create and manage new objects and support inheritance, which is one of the core concepts of object-oriented programming languages such as Java. In short, a class organizes information about a data type so as to reuse elements when making multiple instances of that data type. The data or variables defined within a class are called instance variables.

What is a Constructor?

A constructor is a special member function of a class which is called when instance of an object is created. It can be difficult at times to initialize all the variables in a class every time an instance is created. As the requirement for initialization is common in object-oriented programming, it allows objects to initialize automatically when they are created. This process of automatic initialization is done through the use of a constructor. The reason why it’s called special is because the instance method usually has the same name as the class. It initializes an object immediately upon creation and can be used to set the values of the members of an object. A constructor is syntactically similar to a method in object-oriented programming and a method is a procedure associated with a class and included in any object of that class. In short, a constructor is called each time an object of a class is created. The most important thing about constructors is that they have no Return Type, not even void.

What is a Destructor?

A destructor is entirely a different concept; it’s a special method which is called when instance of a class is deleted from the memory. A destructor is called automatically when an object is no longer needed or is about to disappear or is released explicitly. The main purpose of a destructor is to get rid of the resources the object was using during its lifecycle. It gives the object a last chance to free up any memory it allocated so that there will be enough space on heap to store new objects to run programs efficiently. Destructors are often used in conjunction with constructors, except they are used to destroy objects that were created using constructors. The idea is to de-initialize objects when they are deleted to free up memory for new objects. Like constructors, destructors are defined as subroutines in the class definition and they have the same name as the class name, except a destructor is prefixed with a ~ (tilde) operator. Destructors are called explicitly in C++, however, there are no destructors in Java.

Difference between Constructor and Destructor

Purpose of Constructor and Destructor

Both constructors and destructors are special member functions of every class but with different concepts. A constructor is used to initialize the instance of a class, meaning it is invoked each time a class is instantiated, whereas a destructor is the opposite of a constructor which are called in the reverse order of constructors.

Memory Allocation

A constructor is called each time a new instance of a class is created. It is basically a member function of a class which initializes an object immediately upon creation and allocates memory to it. A destructor, on the other hand, is called when instance of a class is deleted from the memory which in turns de-initializes objects that were created using constructors to free up memory for new objects.

Name

Both constructors and destructors are special member functions of a class with the same name as the class name, except a destructor is prefixed with a ~ (tilde) operator. A constructor is nothing but a method that has no Return Type, not even void, whereas destructors are exactly the opposite of constructors.

Overloading

Both constructors and destructors are called automatically after creation and deletion. However, unlike constructors, destructors cannot be overloaded. Overloading is an object-oriented programming concept that allows you to define two or more functions with the same name so that you can call them with different argument lists.

Arguments

In object-oriented programming, constructors often accept arguments that they use to set required member variables, whereas destructors do not accept any arguments.

Working of Constructor and Destructor

Constructors allow the object to initialize some of its value before it’s being used, whereas destructors allow the object to execute some part of the code at the time when it’s being destroyed.

 Constructor Verses Destructor: Comparison Chart

Summary of Constructor Vs. Destructor

Both constructors and destructors are special member functions of a class with the same name as the class name, except destructors are preceded by a ~ tilde operator. A constructor is nothing but a method except it’s called when instance of an object is created, whereas a destructor is exactly the opposite of constructors which are called when instance of an object is deleted from memory. The purpose of destructors is to de-initialize objects that were created using constructors in order to free up memory space to accommodate new objects.