In object-oriented programming with C++, which concept best matches the statement: “The compiler checks the type of the reference used for a call rather than the actual object’s type.” Select the most appropriate concept name.

Difficulty: Easy

Correct Answer: Polymorphism

Explanation:


Introduction:
Many learners get confused between compile-time resolution (based on the static type of a reference) and runtime resolution (based on the dynamic type of the object). This question asks you to connect that behavior to the correct high-level object-oriented programming concept in C++.


Given Data / Assumptions:

  • A call is made through a reference or pointer.
  • The compiler can use the static type (reference type) for some resolutions.
  • With virtual functions, the language can defer the final choice to runtime based on the object's dynamic type.


Concept / Approach:
Polymorphism is the umbrella concept that covers both compile-time and runtime selection of behavior depending on types. In C++, overloading and templates are compile-time (the compiler checks the reference/parameter types), whereas overriding of virtual functions is runtime (the program checks the object’s dynamic type and uses late binding).


Step-by-Step Solution:
1) If a function call is not virtual (e.g., overloading), the compiler resolves it using the static type of the expression (the reference type).2) If the call is to a virtual function through a base reference/pointer, the runtime decides which override to invoke based on the dynamic object type.3) Both behaviors fall under polymorphism: ad-hoc (compile time) and subtype (runtime).4) Among the given options, the broad, correct concept name is “Polymorphism”.


Verification / Alternative check:
Create Base and Derived with a virtual function, call through Base& to a Derived object, and observe the Derived override executes. For an overloaded non-virtual function set, the compiler’s selection depends on the reference’s static type.


Why Other Options Are Wrong:
Inheritance: a relationship enabling reuse/subtyping, not the selection mechanism itself.Abstraction: exposing essential features while hiding details—unrelated to call dispatch.Encapsulation: bundling data/operations with access control—also unrelated to dispatch rules.


Common Pitfalls:
Assuming every member function call is dynamically bound. In C++, only virtual functions use runtime dispatch; others are resolved using the reference type at compile time.


Final Answer:
Polymorphism

More Questions from OOPS Concepts

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion