If a constructor 'Date' is declared explicitly and has to be defined outside…
2022
If a constructor 'Date' is declared explicitly and has to be defined outside the class, which of the following is correct?
- A.
Date::Date(int dd) {/∗…∗/}
- B.
explicit Date:: Date(int dd) {/∗…∗/}
- C.
Such a constructor cannot be defined
- D.
Constructor always has to be defined inside the class
Attempted by 91 students.
Show answer & explanation
Correct answer: A
Answer: The correct way to define the constructor outside the class is Date::Date(int dd) { /*...*/ }.
Key points:
Out-of-class definition syntax: use ClassName::ClassName(parameter-list) { /*...*/ }.
The explicit specifier belongs in the declaration inside the class. Do not repeat explicit when writing the out-of-class definition.
Constructors can be defined either inside the class body (inline) or outside the class using the scope resolution operator.
Example: declare inside the class as explicit Date(int dd); and define outside as Date::Date(int dd) { /*...*/ }.
A video solution is available for this question — log in and enroll to watch it.