Interview Question

What is a virtual function?

Virtual functions dispatch to the final overrider of the dynamic object type.

💡 Concept ✅ Quick Revision 🧠 C++

Answer

A virtual function supports dynamic dispatch through a base-class reference or pointer. • The final overrider is selected from the dynamic type of the object. • The override keyword asks the compiler to verify that a derived declaration overrides. • Virtual dispatch is suppressed in some qualified calls and during parts of construction and destruction.

💡 C++ Example

struct Shape { virtual double area() const = 0; virtual ~Shape() = default; };

⚡ Quick Revision

Virtual functions dispatch to the final overrider of the dynamic object type.