All Exams Test series for 1 year @ ₹349 only
Question

If a constructor 'Date' is declared explicitly and has to be defined outside the class, which of the following is correct?

The correct answer is

Date :: Date(int dd) {/*...*/}

Understanding C++ Constructor Definition Outside the Class

In C++, a constructor is a special member function of a class that is executed automatically when an object of the class is created. It has the same name as the class and no return type (not even void).

Constructors can be declared inside the class definition and defined either inside or outside the class definition, just like other member functions. When a member function, including a constructor, is defined outside the class declaration, you need to specify which class it belongs to using the scope resolution operator (::).

Syntax for Defining a Constructor Outside the Class

The general syntax for defining a member function (including a constructor) outside its class declaration is:

Return_Type Class_Name :: Function_Name(Parameters) { // function body }

For a constructor, there is no explicit return type mentioned before the class name.

So, the syntax for defining a constructor outside the class is:

Class_Name :: Constructor_Name(Parameters) { // constructor body }

Since the constructor's name is the same as the class name, this becomes:

Class_Name :: Class_Name(Parameters) { // constructor body }

Analyzing the Given Options for the 'Date' Constructor

Let's consider the given options for defining a constructor named Date for a class also named Date, with an integer parameter dd, when the definition is outside the class.

  • Date :: Date(int dd) {/*...*/}

    This syntax follows the correct pattern: Class_Name :: Constructor_Name(Parameters) { /* definition */ }. Here, Class_Name is Date and Constructor_Name is also Date. This is the standard and correct way to define a constructor outside the class.

  • explicit Date:: Date(int dd) {/*...*/}

    The explicit keyword is used in the declaration of a constructor inside the class to prevent implicit conversions. It is not used in the definition of the constructor outside the class. Therefore, this syntax is incorrect for the external definition.

  • Such a constructor cannot be defined

    This statement is incorrect. Constructors declared inside a class can definitely be defined outside the class using the scope resolution operator.

  • Constructor always has to be defined inside the class

    This statement is also incorrect. Constructors, like other member functions, can be defined either inside or outside the class declaration. Defining them outside is common practice, especially for longer definitions, to keep the class declaration clean and easy to read.

Based on the standard C++ syntax for defining member functions and constructors outside the class, the correct format uses the class name followed by the scope resolution operator :: and then the constructor name (which is the same as the class name) and its parameter list.

Therefore, the syntax Date :: Date(int dd) {/*...*/} is the correct way to define the Date constructor outside the Date class.

Revision Table: C++ Constructor Definition

Concept Declaration (Inside Class) Definition (Inside Class) Definition (Outside Class)
Constructor Class_Name(Parameters); Class_Name(Parameters) { /* definition */ } Class_Name :: Class_Name(Parameters) { /* definition */ }
Example (Date class) Date(int dd); Date(int dd) { /*...*/ } Date :: Date(int dd) { /*...*/ }

Additional Information: Scope Resolution Operator and explicit Keyword

Scope Resolution Operator (::)

The scope resolution operator (::) is used in C++ to define a member function outside its class declaration or to access a static member or a member of a namespace. When defining a member function like a constructor outside the class, ClassName::FunctionName tells the compiler that FunctionName belongs to ClassName.

explicit Keyword

The explicit keyword is used with constructors (and conversion operators) declared inside the class definition. Its purpose is to prevent the compiler from using that constructor for implicit conversions or copy-initialization. For example:

class Date {
public:
    explicit Date(int dd); // Declared with explicit
    // ...
};

// In client code:
Date d1(10); // OK, direct initialization
// Date d2 = 10; // ERROR if constructor is explicit, OK otherwise (implicit conversion)

The explicit keyword is part of the constructor's declaration, not its definition outside the class.

Was this answer helpful?

Important Questions from OOPs concept

  1. Which statements is correct regarding main () function in C++?

    I. It is a user defined function.

    II. It is a pre-defined function.

  2. Consider the following segment of C++ code.

    int j, n;

    for(j=1;j<=n; )

    j=j*2;

    What is the number of comparisons made in the execution of the loop for any n > 0?

  3. Given below are two statements:

    Statement (I): The friend function and the member functions of a friend class directly access the private and protected data.

    Statement (II): The friend function can access the private data through the member functions of the base class

    In the light of the above statements, choose the most appropriate answer from the options given below:

  4. Which of the following statements is TRUE ?

  5. Match List I with List II

    LIST - I

    LIST - II

    A.

    If the implementation of generated or derived classes differ only through a parameter we have

    I.

    to use class hierarchy

    B.

    If the actual types of objects used cannot be known at compile time, then we have

    II.

    improved run-time efficiency

    C.

    If inline operations are essential and templates are used then we have

    III.

    to use templates

    D.

    To gain access to differing instances for derived classes through base we have

    IV.

    to use explicit casting

    Choose the correct answer from the options given below:

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App