Which of the following are true about Constructors?
-
Member Function
-
Same name as Class name
-
Default constructor initializes all non-initialized fields and variables to zero.
-
Compiler adds an empty constructor, if we do not write our own constructor
Constructors are special member functions with the same name as their class. The default constructor initializes primitive fields to zero/null. If no constructor is written, the compiler provides a default constructor.
To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) Member Function - This option is correct. Constructors are special member functions in a class that are used to initialize objects of that class.
Option B) Same name as Class name - This option is correct. Constructors have the same name as the class they belong to.
Option C) Default constructor initializes all non-initialized fields and variables to zero - This option is correct. If a class does not have any constructor defined, the compiler adds a default constructor that initializes all non-initialized fields and variables to zero.
Option D) Compiler adds an empty constructor if we do not write our own constructor - This option is correct. If a class does not have any constructor defined, the compiler automatically adds an empty constructor (default constructor) to the class.
Therefore, all of the given options (A, B, C, D) are true about Constructors.
The correct answer is A, B, C, D.