In C++, which type of function is mainly used to achieve run-time polymorphism?

Options

  • A. Inline function
  • B. Virtual function
  • C. Friend function
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. Virtual function

Detailed Explanation

Answer: Virtual function

A virtual function allows C++ to select the appropriate overridden function at run time when a base-class pointer or reference refers to a derived-class object.

This mechanism is called run-time polymorphism or dynamic binding.