Bird
Raised Fist0
C++programming~5 mins

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

Choose your learning style10 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.

      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

      1. Step 1: Understand virtual functions role

        Virtual functions allow a base class to declare a function that derived classes can override.
      2. Step 2: Explain runtime decision

        At runtime, the program decides which overridden function to call based on the actual object type.
      3. Final Answer:

        To allow derived classes to override base class methods and decide at runtime which method to call -> Option D
      4. Quick Check:

        Virtual functions enable runtime method selection [OK]
      Hint: Virtual means choose function at runtime, not compile time [OK]
      Common Mistakes:
      • Thinking virtual speeds up function calls
      • Believing virtual prevents overriding
      • Confusing virtual with memory allocation
      2. Which of the following is the correct syntax to declare a virtual function in a base class?
      easy
      A. void display() virtual;
      B. void virtual display();
      C. virtual void display();
      D. virtual: void display();

      Solution

      1. Step 1: Recall virtual function syntax

        The keyword virtual comes before the return type in the function declaration.
      2. Step 2: Match correct syntax

        virtual void display(); correctly places virtual before void display();
      3. Final Answer:

        virtual void display(); -> Option C
      4. Quick Check:

        Virtual keyword precedes return type [OK]
      Hint: Virtual always goes before return type in declaration [OK]
      Common Mistakes:
      • Placing virtual after function name
      • Using virtual as a label or modifier incorrectly
      • Missing semicolon after declaration
      3. What will be the output of the following code?
      class Base {
      public:
          virtual void show() { std::cout << "Base"; }
      };
      class Derived : public Base {
      public:
          void show() override { std::cout << "Derived"; }
      };
      int main() {
          Base* ptr = new Derived();
          ptr->show();
          delete ptr;
          return 0;
      }
      medium
      A. Derived
      B. Base
      C. Compilation error
      D. Runtime error

      Solution

      1. Step 1: Identify virtual function usage

        The show() function in Base is virtual and overridden in Derived.
      2. Step 2: Understand pointer type and object type

        The pointer is of type Base* but points to a Derived object, so the overridden show() in Derived is called at runtime.
      3. Final Answer:

        Derived -> Option A
      4. Quick Check:

        Virtual function calls derived version [OK]
      Hint: Virtual functions call derived method via base pointer [OK]
      Common Mistakes:
      • Expecting base class method output
      • Confusing pointer type with object type
      • Ignoring virtual keyword effect
      4. Identify the error in the following code snippet related to runtime polymorphism:
      class Animal {
      public:
          void sound() { std::cout << "Animal sound"; }
      };
      class Dog : public Animal {
      public:
          void sound() { std::cout << "Bark"; }
      };
      int main() {
          Animal* a = new Dog();
          a->sound();
          delete a;
          return 0;
      }
      medium
      A. Incorrect pointer type declaration
      B. Missing virtual keyword in base class function
      C. Derived class function should be static
      D. Memory leak due to missing delete

      Solution

      1. Step 1: Check base class function declaration

        The base class function sound() is not declared virtual, so no runtime polymorphism occurs.
      2. Step 2: Effect on function call

        Because sound() is not virtual, calling a->sound() calls the base class version, not the derived one.
      3. Final Answer:

        Missing virtual keyword in base class function -> Option B
      4. Quick Check:

        Virtual needed for runtime polymorphism [OK]
      Hint: Base function must be virtual for polymorphism [OK]
      Common Mistakes:
      • Assuming override works without virtual
      • Thinking pointer type causes error
      • Ignoring virtual keyword importance
      5. Given the classes below, what will be the output when ptr->identify() is called?
      class Shape {
      public:
          virtual void identify() { std::cout << "Shape"; }
      };
      class Circle : public Shape {
      public:
          void identify() override { std::cout << "Circle"; }
      };
      class ColoredCircle : public Circle {
      public:
          void identify() override { std::cout << "Colored Circle"; }
      };
      int main() {
          Shape* ptr = new ColoredCircle();
          ptr->identify();
          delete ptr;
          return 0;
      }
      hard
      A. Colored Circle
      B. Circle
      C. Shape
      D. Compilation error due to multiple overrides

      Solution

      1. Step 1: Trace virtual function overrides

        identify() is virtual in Shape, overridden in Circle, and again in ColoredCircle.
      2. Step 2: Determine actual object type at runtime

        The pointer ptr is of type Shape* but points to a ColoredCircle object, so the most derived override runs.
      3. Final Answer:

        Colored Circle -> Option A
      4. Quick Check:

        Most derived override called at runtime [OK]
      Hint: Runtime calls most derived override in inheritance chain [OK]
      Common Mistakes:
      • Expecting base or intermediate class output
      • Thinking multiple overrides cause errors
      • Ignoring virtual override chain
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard