Javatpoint Logo
Javatpoint Logo

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.

Constructor vs Destructor in C++

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.

Syntax:

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
  • Dynamic constructor
  • Parameterized constructor
  • Copy constructor

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.

Example:

The default constructor for a class student is given below:

hello::hello()

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:

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.

Syntax:

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:

Basis Constructor Destructor
Purpose of use To allocate memory to the object, we used a constructor in C++. To deallocate the memory that the constructor allocated to an object for this purpose, we use the concept of destructor in C++.
Arguments It may or may not contain arguments. It cannot contain the arguments.
Calling It is called automatically whenever the object of the class is created. It is called automatically whenever the program terminates.
Memory Constructor occupies memory. The Destructor releases memory.
Return type It has return types. It doesn't have any return type.
Special symbol While declaring constructor in the C++ programming language, there is no requirement of the special symbol. While declaring a destructor in C++ programming language, a particular symbol is required, i.e., tilde symbol.
In numbers We can use more than one constructor in our program. We cannot use more than one destructor in the program.
Inheritance It can be inherited. It cannot be inherited.
Overloading It can be overloaded. It cannot be overloaded.
Execution Order They are executed in successive order. They are executed in the constructor's reverse order; basically, they are the inverse of the constructors.
Types Constructor has four types:
  • Default constructor
  • Copy constructor
  • Parameterized constructor
  • Dynamic constructor
Destructors have no classes.
Declaration The following declaration is used for creating a constructor:
class class_name
{
……….
public
class_name ([parameter list])
{
……………….
}
};
The following declaration is used for creating a destructor:
class class_name
{
…………….;
…………….;
public:
~xyz();  
{
…………         
};

Next Topic#




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA