C++ Constructors and destructors
Constructors are special member functions that are called by the compiler every time an object of that class is instantiated. Constructors have the same name as the class and maybe defined inside or outside the class definition. It is not necessary to define a constructor, a constructor is automatically called every time an object is created.
There some features which distinguish between a constructor and a normal member function these are
- Constructor has same name as the class itself
- Constructors do not have any return type
- A constructor is automatically called when an object is created.
- If we do not specify a constructor while defining a class, the C++ compiler generates a default constructor for us without any parameters and an empty body.
There are 3 types of constructors, default, parameterized and copy constructors.
Let us try to understand constructors by taking real-life examples to understand them better. Suppose you went to a shop to buy a football. When you want to buy a football, what are the options you have?
The first one: you go to a shop and say give me a football. So just saying give me a football means that you did not set which brand name and which color, you didn’t mention anything all you said is you want a football. So when we said just I want a football then whatever football is in his shop he will simply hand over that. And this is what a default constructor is!
The second option: you go to a shop and say I want a football red in color and XYZ brand. So you are mentioning this and he will give you that football. So in this case you have given the parameters. And this is what a parameterized constructor is!
Then the third one: you go to a shop and show him a sample football and ask him to give you the same football in this case you are showing him a copy of the football, so here your parameter is itself a marker of the type you want this is what copy constructor is.
The default constructor doesn’t take any argument. It has no parameters. In case we do not write a constructor, a default constructor is automatically generated by the compiler when an object is created.
Program to illustrate default constructor.
It's possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor just add parameters to the default constructor the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object. Constructors are similar to functions but without return type and have the same name as the class.
When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly.
Uses of Parameterized constructor:
1.It is used to initialize the various data members of different objects with different values when they are created.
2.It is used to overload constructors.
C++ allows us to have more than one constructor in a class with same name, as long as each has a different list of arguments. This concept is known as Constructor Overloading.
- Overloaded constructors essentially have the name of the class and a different number of arguments or the type of arguments basically the signature of the constructor.
- A constructor is called depending upon the number and type of arguments passed.
- While creating an object, arguments must be passed to let the compiler know, which constructor needs to be called.
Consider this example to demonstrate the use of Constructor overloading.
A copy constructor is a member function that initializes an object using another object of the same class.
Whenever we define one or more non-default constructors( with arguments ) for a class, a default constructor( without arguments ) should also be explicitly defined as the compiler will not provide a default constructor in this case. However, it is not necessary but it’s considered to be the best practice to always define a default constructor.
A destructor is a special member function that works just opposite to constructor, unlike constructors that are used for initializing an object, destructors delete the object.
In destructors class name is preceded by " ~" this indicates the member function is a destructor.
The destructor is usually called when:-
- The program finished execution.
- When a scope containing local variable ends.
- When you call the delete operator.
- Name should begin after tilde(~) sign and be the same as the class name.
- There cannot be more than one destructor in a class.
- Destructors do not allow any parameter
- The do not have any return type.
- When you do not specify a destructor in a class, the compiler generates a default destructor and inserts into your code