0
0
C++programming~5 mins

Runtime polymorphism in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is runtime polymorphism in C++?
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
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
What is the purpose of a vtable?
AStore data members
BStore pointers to virtual functions
CStore global variables
DStore function parameters
What does a pure virtual function do?
AProvides a default implementation
BDisables polymorphism
CPrevents inheritance
DMakes the class abstract
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
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.