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++?
✗ 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?
✗ 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?
✗ Incorrect
The vtable stores pointers to virtual functions to enable dynamic dispatch at runtime.
What does a pure virtual function do?
✗ 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?
✗ 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.