Solution
The correct answer is Date :: Date(int dd) {/*...*/}
Key PointsWhen we declare the constructor inside the class we could use the explicit keyword to prevent implicit conversions:
class Date {
explicit Date(int dd);
};
But when defining it outside the class, we should not include the explicit keyword:
Date :: Date(int dd) {/*...*/}
So, the correct answer is Date :: Date(int dd) {/*...*/}
Additional InformationA constructor in object-oriented programming is a special type of subroutine (function or method) called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set member variables required.
In C++, the constructor is a special function with the same name as the class which is automatically called whenever an object of the class is created. It is used to initialize the object of its class. Here's some basic features of constructors:
- It should be declared in the public section of a class.
- It is automatically invoked whenever an object is created.
- It cannot return values and doesn't have a return type, not even void.
- An error occurs if you declare a constructor with a return type.
- It can have default arguments.
- We cannot refer to their address.
- It can be overloaded with different parameters.