Difference between Constructor and Destructor in C++?
In this article, we will see the comparisons between the constructors and destructors in the C++ programming language. The first thing that we will learn from this article is the basic idea of constructors and destructors. After that, we will learn the various comparisons of constructors and destructors in C++ programming.
What is C++?
C++ is a superset of C because it supports both procedural-oriented and object-oriented programming languages. It is a middle-level language. It has various features such as encapsulation, inheritance, abstraction, data hiding, constructor, and destructor.
Constructor in C++?
A constructor is a particular member function having the same name as the class name. It calls automatically whenever the object of the class is created.
The syntax of the constructor in C++ are given below.
In the above-given syntax, class_name is the constructor's name, the public is an access specifier, and the parameter list is optional.
Example of Constructor:
There are four types of constructors used in C++.
Default Constructor: A constructor is a class which accepts no parameter and is called a default constructor. If there is no constructor for a class, the compiler implicitly creates a default constructor.
Following is the syntax of the default constructor:
In this type of constructor, there are no arguments and parameter lists.
If no constructor is defined in the class, the compiler automatically creates the class's default constructor.
The default constructor for a class student is given below:
Parameterised Constructor: A constructor is a class that can take parameters and is called a parameterized constructor. It is used to initialize objects with a different set of values.
Syntax of the Parameterised constructor is given below.
Here, we can define the parameter list of the constructor.
Copy Constructor: A particular constructor used for the creation of an existing object. The copy constructor is used to initialize the thing from another of the same type.
Syntax of the copy constructor is given below.
In the above syntax, the object refers to a thing used to initialize another object.
Dynamic Constructor: This type of constructor can be used to allocate the memory while creating the objects. The data members of an object after creation can be initialized, called dynamic initialization.
Destructor in C++?
Destructors have the same class name preceded by (~) tilde symbol. It removes and destroys the memory of the object, which the constructor allocated during the creation of an object.
The syntax of destructor in C++ are given below.
Here, we use the tilde symbol for defining the destructor in C++ programming.
The Destructor has no argument and does not return any value, so it cannot be overloaded.
Example of Destructor:
Difference between Constructor and Destructor in C++ programming
Following table shows the various differences between constructor and destructor in the C++ programming language: