Virtual Member Functions

In this lesson, we'll be learning about a very important concept of polymorphism, i.e., Virtual member.

Virtual means existing in appearance but not in reality.

Definition

A virtual function is a member function which is declared within the base class and is overridden by the derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for the function call. They are mainly used to achieve Runtime polymorphism. Functions are declared with a virtual keyword in a base class. The function resolution call is done at run-time.

Get hands-on with 1200+ tech skills courses.