The various types of Constructors and Destructor

A special method of the class that will be automatically invoked when an instance of the class is created is called as the constructor. 
Following are the most useful features of the constructor. 
1) The constructor is used for Initializing the values to the data members of the Class.
2) The constructor is that whose name is same as the name of the class. 
3) Constructor gets Automatically called when an object of the class is created. 
4) Constructors never have a Return Type even void. 
5) The constructor is by Default, Parameterized, and Copy Constructors. 

The various types of Constructor are as follows:- 
Constructors can be classified into 3 types
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor

1. Default Constructor:- Default Constructor is also called as Empty Constructor which has no arguments and It is Automatically called when we create the object of class but Remember the name of Constructor is same as the name of class and Constructor never declared with the help of Return Type. Means we can‟t declare a Constructor with the help of void Return Type. , if we never Pass or declare any Arguments then this called as the Copy Constructors. 

2. Parameterized Constructor: - This is another type constructor which has some Arguments and the same name as class name but it uses some Arguments So For this We have to create the object of Class by passing some Arguments at the time of creating the object with the name of the class. When we pass some Arguments to the Constructor then this will automatically pass the Arguments to the Constructor and the values will retrieve by the Respective Data Members of the Class.

3. Copy Constructor: - This is also another type of Constructor. In this Constructor, we pass the object of the class into the Another Object of Same Class. As name Suggests you Copy, means Copy the values of one Object into another Object of Class.This is used for Copying the values of class object into another object of class So we call them as Copy Constructor and For Copying the values We have to pass the name of object whose values we want to Copy and When we are using or passing an Object to a Constructor then we must have to use the & Ampersand or Address Operator.

Destructor: As we know that Constructor is that which is used for Assigning Some Values to data Members and For Assigning Some Values this May also used Some Memory so that to free up the Memory which is Allocated by Constructor, destructor is used which gets Automatically Called at the End of Program and we doesn‟t have to Explicitly Call a Destructor and Destructor Cant be Parameterized or a Copy This can be only one Means Default Destructor which Has no Arguments. For Declaring a Destructor we have to use ~tiled Symbol in front of Destructor. 


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: