A virtual function is one the runtime picks based on the object's actual type. The type of the pointer or reference you call through does not decide it.
Declare it virtual in the base, override it in the derived class, then call through a base handle. The derived version runs. Without virtual, the call binds to the static type and your override is silently skipped.
This is what lets you write code against an interface. A render loop can hold a vector of Shape pointers and call draw() on each. It never learns whether they are circles or polygons, and adding a new shape leaves it untouched.
The cost is one indirection per call and, usually, no inlining. That is cheap but not free, so you do not mark everything virtual by reflex.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓