- In C++ constructor is a special type of member function of a class which initializes objects of a class.
- Constructor is automatically called when object is created in C++.
- It does not have any return type because it is special member function of the class.
- In public section of class and it has same name as the class itself.
- Copy and parameterized constructors have input arguments, by default constructors don’t have input argument however.
- C++ compiler generates a default constructor for object, if we do not specify a constructor.
- In constructor there are three types, they are default constructor, parameterized constructor, copy constructor.
Default Constructor
- It is the constructor which has no parameters and doesn’t take any argument.
Sample Code
Output
Parameterized Constructor
- It is possible to pass arguments to constructors and these arguments help initialize an object when it is created typically.
Sample Code
Output
Copy Constructor
- It is a member function which initializes an object using another object of the same class.