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.
Do you have similar website/ Product?
Show in this page just for only
$2 (for a month)
0/60
0/180
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:
- Its name is same as the class name.
- It is called automatically when object is created.
- It should be declared in public section.
- Constructor does not have any return type, not even void.
- Constructor can take any number of arguments.
- A class can have more than one constructors, they are distinguished by their arguments number and type.
- Constructor can be inherited to derived class.
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-
- Its name is same as the class name but with tilde sign before name.
- It is called automatically when object is destroyed
- It should be declared in public section.
- Destructor does not have any return type, not even void.
- Destructor does have any argument.
- A class can have only one destructor.
CONTINUE READING
Concept of Constructors and Destructor in C++.
Kinnari
Tech writer at NewsandStory