Runtime polymorphism means that the program decides which function to call during execution, not while compiling. It allows a base class pointer to call derived class functions using virtual functions.
Click to reveal answer
beginner
How do you enable runtime polymorphism in C++?
By declaring a function as virtual in the base class. This tells the compiler to use dynamic dispatch to call the correct function at runtime.
Click to reveal answer
intermediate
What happens if a base class function is not virtual but overridden in a derived class?
The base class pointer will call the base class version of the function, not the derived class version. This is because without <code>virtual</code>, the function call is resolved at compile time.
Click to reveal answer
advanced
Explain the role of the vtable in runtime polymorphism.
The vtable is a hidden table created by the compiler that stores pointers to virtual functions. Each class with virtual functions has a vtable. At runtime, the program uses the vtable to call the correct function version.
Click to reveal answer
intermediate
What is a pure virtual function and how does it relate to runtime polymorphism?
A pure virtual function is declared by assigning 0 in the base class (e.g., <code>virtual void f() = 0;</code>). It makes the class abstract and forces derived classes to implement the function, enabling polymorphism.
Click to reveal answer
Which keyword enables runtime polymorphism in C++?
Avirtual
Bstatic
Cconst
Dinline
✗ Incorrect
The virtual keyword tells the compiler to use dynamic dispatch for function calls, enabling runtime polymorphism.
What will happen if a base class pointer calls a non-virtual function overridden in a derived class?
ACompilation error
BDerived class function is called
CBase class function is called
DProgram crashes
✗ Incorrect
Without virtual, the function call is resolved at compile time, so the base class function is called.
What is the purpose of a vtable?
AStore data members
BStore pointers to virtual functions
CStore global variables
DStore function parameters
✗ Incorrect
The vtable stores pointers to virtual functions to enable dynamic dispatch at runtime.
What does a pure virtual function do?
AProvides a default implementation
BDisables polymorphism
CPrevents inheritance
DMakes the class abstract
✗ Incorrect
A pure virtual function makes the class abstract and forces derived classes to implement the function.
Which of these is true about runtime polymorphism?
AIt requires virtual functions
BFunction calls are decided at compile time
CIt only works with static functions
DIt disables inheritance
✗ Incorrect
Runtime polymorphism requires virtual functions to decide which function to call during program execution.
Describe how runtime polymorphism works in C++ and why virtual functions are important.
Think about how the program decides which function to call when using a base class pointer.
You got /5 concepts.
Explain the difference between compile-time and runtime polymorphism with examples.
Consider when the program decides which function to call: before running or while running.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of virtual functions in C++ runtime polymorphism?
easy
A. To allocate memory for objects dynamically
B. To make functions run faster by avoiding function calls
C. To prevent any function from being overridden in derived classes
D. To allow derived classes to override base class methods and decide at runtime which method to call
Solution
Step 1: Understand virtual functions role
Virtual functions allow a base class to declare a function that derived classes can override.
Step 2: Explain runtime decision
At runtime, the program decides which overridden function to call based on the actual object type.
Final Answer:
To allow derived classes to override base class methods and decide at runtime which method to call -> Option D