A constructor is the member function of a class which initializes objects of a class. It is automatically invoked during the time of object creation of the class. It is the special member function of the class.
A constructor although looks a lot like a normal function differs from that in the following ways:
- A constructor has the same name as the class
- A constructor does not have a return type
- A constructor is automatically or implicitly invoked at the time of object creation
- If the programmer does not explicitly specify a constructor, the compiler automatically defines a constructor implicitly
There are majorly three types of constructors namely:
- Default or unparameterized constructors : Those constructors which do not take any arguments
- Parameterized constructors : Those constructors which takes arguments
- Copy constructors : Those constructors which initialize an object using another object of the same class. The syntax is as follows: