Enter your email id to get regular update free
Login
Your Email
Pasword

What is a Constructor and Destructor in C++?

Here we understand about Constructor and Destructor and understand about its properties in C++ language in detailed.

What is a Constructor and Destructor and its properties in C++?
First, we will discuss about what is a Constructor in detail-
A Constructor is a special member function in a class whose task is to initialize data members.
Constructor is a special function because of two reasons. First its name is same as class' name and second it is called automatically whenever objects are created.
It is not mandatory to use Constructor in a class, but preferable.
Now we will discuss about special properties of Constructors:
  1. Its name is same as the class name.
  2. It is called automatically when object is created.
  3. It should be declared in public section.
  4. Constructor does not have any return type, not even void.
  5. Constructor can take any number of arguments.
  6. A class can have more than one constructors, they are distinguished by their arguments number and type.
  7. Constructor can be inherited to derived class.
Now, we will talk about Destructor and its properties in detailed-
Destructor:
A Destructor as its name implies, is used to destroy the objects that have been created by constructors. It releases the memory space for future use.
Now, we will discuss about its properties one by one-
  1. Its name is same as the class name but with tilde sign before name.
  2. It is called automatically when object is destroyed
  3. It should be declared in public section.
  4. Destructor does not have any return type, not even void.
  5. Destructor does have any argument.
  6. A class can have only one destructor.
What is a Constructor?
What is Destructor?
Properties of Constructors
Properties of Destructors
Concept of Constructors and Destructor in C++.
Kinnari
Tech writer at NewsandStory
Total 152 views