What is a Destructor in C++?
Destructor is a member function that is instantaneously called whenever an object is destroyed. The destructor is called automatically by the compiler when the object goes out of scope i.e. when a function ends the local objects created within it also gets destroyed with it. The destructor has the same name as the class name, but the name is preceded by a tilde(~). A destructor has no return type and receives no parameters.
Characteristics of a Destructor in C++
- It deallocates the memory of an object.
- It doesn’t take any argument.
- It is called automatically when the block is exited or when the program terminates.
- They allow objects to execute code when it is being destroyed.
- They are called in the reverse order of their creation.
- There is a single destructor in a class.
- Destructor can’t be overloaded.
What is a Constructor in C++?
Constructor in C++ is a special member function of a class whose task is to initialize the object of the class, it’s special because it has the same name as that of the class. It is called a constructor because it constructs the value of data members at the time of object initialization. The compiler invokes the constructor whenever an object is created. Since a constructor defines the value to a data member, it has no return type.
Types of Constructors in C++
There are 4 types of constructors in C++:
- Default Constructors
- Parameterized Constructors
- Copy Constructors
- Dynamic Constructors
Characteristics of Constructors in C++
- It helps allocate memory to an object.
- It can take arguments.
- It is called automatically when an object is created.
- It allows an object to initialize a value before it is used.
- They are called in the successive order of their creation.
- There can be multiple constructors in a single class.
- The copy constructor allows the constructor to declare and initialize an object from another object.
- It can be overloaded.
Differences between Constructor and Destructor in C++
Basis of Comparison | Constructor | Destructor |
Definition | Constructors are special class members used to allocate memory for objects. | A destructor is a special member of a class used to allocate memory for an object. |
Method of Invoking | When the object is created only then the constructor is called. | The destructor is called when the object is destroyed or deleted. |
Usage | The constructor is used to allocate memory for the object. | Destructors are used to allocate memory for objects. |
Parameters | The constructor accepts parameters. | The destructor does not accept any parameters. |
Number of Constructors and Destructors | There can be multiple constructors with different numbers of parameters and different types of parameters. | There is only a single destructor in the class. |
Execution Speed | The constructor’s name is the same as the name of the class. | The name of the destructor is the same as the class name, with a tilde (~) symbol. |
Overloading | Constructors can be overloaded. | Destructors cannot be overloaded. |
Key Points
- Constructor and Destructor are the special member functions of the class which are created by the C++ compiler or can be defined by the user.
- Constructor is called by the compiler whenever the object of the class is created, it allocates the memory to the object and initializes class data members.
- A destructor is called by the compiler when the object is destroyed and its main function is to deallocate the memory of the object.
- Constructors have the same as of class while destructors have the same name of the class with the prefix a tilde (~) operator.
- Both Constructor and destructor can be defined as public, private, or protected. But it is better to declare the constructor as public.
- The constructor can have parameters but the destructor doesn’t receive any parameters.