All mock tests are FREE till 31 December — register to save your progress

Which of the following are correct about constructors in C++?
A. They have the same name as the class.
B. They can return values.
C. They are called automatically when an object is created.
D. They can be overloaded.
E. They can be inherited.
Choose the correct answer from the options given below:

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: B. A, C & D only

Explanation

A constructor always has the same name as its class, so statement A is true. A constructor has no return type at all, not even void, so it can never return a value, making statement B false. A constructor is invoked automatically whenever an object of the class is created, so statement C is true. Constructors can be overloaded by giving the class multiple constructors with different parameter lists, so statement D is true. Constructors are not inherited by derived classes in C++ (a derived class must define its own constructors, though it may invoke a base-class constructor through the member-initializer list), so statement E is false. Hence only A, C and D are correct.

More Programming Languages and Computer Graphics questions