Q:

What is the difference between constructor and destructor in c++?

0

What is the difference between constructor and destructor?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

There are the following differences between the constructor and destructor in C++.

Constructor Destructor
Constructor helps to initialize the object of a class. Whereas destructor is used to destroy the instances.
The constructor’s name is the same as the class name. The destructor name is the same as the class name but preceded by a tiled (~) operator.
A constructor can either accept the arguments or not. While it can’t have any argument.
A constructor is called when the instance or object of the class is created. It is called while the object of the class is freed or deleted.
A constructor is used to allocate the memory to an instance or object. While it is used to deallocate the memory of an object of a class.
A constructor can be overloaded. While it can’t be overloaded.
There is a concept of copy constructor which is used to initialize an object from another object. While here, there is no copy destructor concept.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is “this” pointer in c++?... >>
<< How destructors are different from a normal member...