Question:

What is constructor in C++ ?

Show Hint

Think of a constructor as the "welcome" or "setup" function for an object. It prepares the object for use by setting its initial state.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

A constructor in C++ is a special member function of a class that is automatically called when an object of that class is created. Its primary purpose is to initialize the data members of the new object. Key properties of a constructor:

It has the same name as the class.
It does not have a return type, not even `void`.
It can be overloaded (i.e., a class can have multiple constructors with different parameters).
If a constructor is not defined by the programmer, the compiler provides a default constructor.
Was this answer helpful?
0
0