In C++ object-oriented programming, which concept refers to waiting until runtime to decide which actual function implementation will be called through a base interface? Choose the best term that names this runtime decision process.

Difficulty: Easy

Correct Answer: Dynamic binding

Explanation:


Introduction:
Late selection of the actual function implementation is a cornerstone of runtime polymorphism in C++. This question asks you to identify the precise term for deciding which overridden method to execute when a call is made through a base-class reference or pointer.


Given Data / Assumptions:

  • Language: C++ with classes and inheritance.
  • Calls are made through a base pointer/reference.
  • Virtual functions are used to enable runtime selection.


Concept / Approach:
The term is dynamic binding (also called late binding). When a function is declared virtual in a base class and overridden in derived classes, the call p->f() is resolved using the object's dynamic type at runtime. Typical implementations use a vtable (virtual table) and a hidden vptr inside each polymorphic object to indirect to the correct function body.


Step-by-Step Solution:
1) Declare a virtual function in Base: virtual void draw();2) Override it in Derived: void draw() override;3) Form a base pointer to a derived object: Base* p = new Derived();4) Invoke p->draw(); at runtime, the program selects Derived::draw via dynamic binding.


Verification / Alternative check:
Print from Base::draw and Derived::draw. When called through a Base* that actually points to a Derived, the Derived version prints, confirming dynamic binding.


Why Other Options Are Wrong:
Data hiding: about encapsulation and access control, not dispatch.Dynamic casting: RTTI-based casts (dynamic_cast), not dispatch selection.Dynamic loading: loading modules at runtime (e.g., dlopen), unrelated to virtual calls.


Common Pitfalls:
Assuming all member functions are dynamically bound; in C++, only virtual functions dispatch dynamically. Also, within constructors/destructors, virtual calls do not dispatch to more-derived overrides.


Final Answer:
Dynamic binding

More Questions from OOPS Concepts

Discussion & Comments

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