A virtual function is a member function whose call is resolved at run time by the object's dynamic type, the type it was created as. Declare it virtual in the base, override it in the derived class, then call through a base pointer or reference. The derived version runs. Without virtual, the call binds to the static type, the type of the pointer or reference, and your override is skipped.
This dynamic dispatch lets you code against an interface. A render loop can call draw() through Shape pointers without knowing the real shapes, so a new shape needs no change to the loop.
The cost is one indirection per call and, usually, no inlining. That is cheap but not free, so not every function should be virtual.
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 ↓