Multiple choice

Why is there a need for virtual function when we access the function (having same name as that in base class) in derived class through the use of a pointer declared as pointer to the base class?

  1. We can not use the object name(with dot operator), as runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.

  2. We need to use single pointer variable to refer to the objects of different classes.

  3. When function is made virtual, C++ determines which function to use at run time based on the type of object pointed by the base class.

  4. All the above.

  5. None of these

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The necessity of virtual function is given by all the above 3 options, so option 4 is the correct answer.