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:
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
Discussion & Comments