Which one of the following is correct for overloaded functions in C++ ?

2017

Which one of the following is correct for overloaded functions in C++ ?

  1. A.

    Compiler sets up a separate function for every definition of function.

  2. B.

    Compiler does not set up a separate function for every definition of function.

  3. C.

    Overloaded functions cannot handle different types of objects.

  4. D.

    Overloaded functions cannot have same number of arguments.

Attempted by 108 students.

Show answer & explanation

Correct answer: A

Answer: Compiler sets up a separate function for every definition of function.

Explanation: The compiler treats each overloaded definition as a distinct function. It distinguishes them (commonly via name mangling) and resolves which function to call at compile time based on the argument types and counts.

  • Key rule: Overloading requires a difference in the parameter list (different number of parameters, different parameter types, or different order).

  • Limitation: The return type alone cannot be used to distinguish two overloaded functions.

  • Example: int f(int); and double f(double); are two separate functions created by the compiler.

  • Why the other statements are incorrect:

    • The statement that the compiler does not set up separate functions is wrong because the compiler must generate distinct symbols for each overload so it can resolve calls.

    • The statement that overloaded functions cannot handle different types is incorrect; handling different parameter types is a primary use of overloading.

    • The statement that overloaded functions cannot have the same number of arguments is misleading: they can have the same number of arguments if the parameter types or order differ. Two functions with identical parameter types and order would be ambiguous.

Explore the full course: Mppsc Assistant Professor