In C++, polymorphism requires :

2016

In C++, polymorphism requires :

Answer: D. Inheritance, Virtual functions and referencesAnswer: Runtime polymorphism in C++ requires three things: An inheritance relationship: a base class and at least one derived class. Virtual functions: the…

  1. A.

    Inheritance only

  2. B.

    Virtual functions only

  3. C.

    References only

  4. D.

    Inheritance, Virtual functions and references

Attempted by 46 students.

Show answer & explanation

Correct answer: D

Answer: Runtime polymorphism in C++ requires three things:

  • An inheritance relationship: a base class and at least one derived class.

  • Virtual functions: the base-class function must be declared virtual so calls can be dynamically dispatched to the derived override at runtime.

  • Pointer or reference to base: you must call through a base-class pointer or reference (pointers are acceptable) to avoid slicing and to enable virtual dispatch.

Example: Define a base class with a virtual function, derive a class that overrides it, then call the function through a base-class pointer or reference to invoke the derived implementation at runtime.

Note: This describes runtime (dynamic) polymorphism. Compile-time polymorphism (function overloading or templates) is a different mechanism and does not require virtual functions or base-class pointers/references.

Explore the full course: Nta Ugc Net Paper 2

Loading lesson…