C++ - Difference Constructor and Destructor

Constructors and destructors are special member functions in C++ that are used to initialize and destroy objects of a class, respectively. Here are the key differences between constructors and destructors:

Purpose:

  • Constructors: Constructors are used to initialize the object's data members and prepare the object for use. They are called automatically when an object is created.
  • Destructors: Destructors are used to clean up the resources allocated by the object before it is destroyed. They are called automatically when an object goes out of scope or is explicitly deleted.

Invocation:

  • Constructors: Constructors are invoked explicitly or implicitly during object creation. They have the same name as the class and do not have a return type.
  • Destructors: Destructors are invoked automatically when an object goes out of scope or is explicitly deleted. They have the same name as the class preceded by a tilde (~) and do not have any parameters or return type.

Number of Instances:

  • Constructors: Constructors are called once for each instance of the class when objects are created. They initialize the object's data members and set up the initial state of the object.
  • Destructors: Destructors are called once for each instance of the class when objects are destroyed. They clean up the resources allocated by the object and perform any necessary cleanup operations.

Overloading:

  • Constructors: Constructors can be overloaded, which means you can define multiple constructors with different parameter lists in the same class.
  • Destructors: Destructors cannot be overloaded. There can be only one destructor per class.

Inheritance:

  • Constructors: Constructors are not inherited by derived classes. However, the derived class's constructor can call the base class's constructor to initialize the inherited members.
  • Destructors: Destructors are inherited by derived classes. When a derived class object is destroyed, the destructor of the derived class is called first, followed by the destructor of the base class.

Explicit Definition:

  • Constructors: Constructors can be explicitly defined in the class declaration or implemented separately in the class definition.
  • Destructors: Destructors can be explicitly defined in the class declaration, but they are usually implemented separately in the class definition.
#include <iostream>
class MyClass {
public:
    MyClass() {
        std::cout << "Constructor called" << std::endl;
    }
    ~MyClass() {
        std::cout << "Destructor called" << std::endl;
    }
};
int main() {
    MyClass obj1;  // Constructor called
    {
        MyClass obj2;  // Constructor called
    }  // Destructor called for obj2
    // Destructor called for obj1
    return 0;
}

In this example, the MyClass has a constructor and a destructor. When objects obj1 and obj2 of the class are created, the constructor is called automatically. When the objects go out of scope, their destructors are called automatically to clean up any allocated resources.