A constructor is a special type of function that is invoked while creating objects with no return type. It is defined inside a class and its name is the same as the name of the class.
Types of Constructors
In C++, we have three main types of constructors: • Default Constructor • Parameterized Constructor • Copy Constructor
A constructor that doesn’t take any arguments and has no parameters is called a default constructor. It is also known as a zero-argument constructor. The compiler will automatically generate a default constructor if we don’t define it inside the class. Data members (variables) are initialized with real values using these constructors.
These receive arguments and help in initializing an object when it is created. You can pass multiple arguments to the same parameterized constructor. Additionally, these constructors are used for overloading to distinguish between various constructors.
A constructor that uses another object from the same class to initialize a new object is called a copy constructor. By copying the members of an existing object, the copy constructor is used to initialize the members of a newly formed object.