declared virtual, if the intention is late binding. This enables it to be bound at run time.
Late binding is also
referred to as dynamic binding. An advantage in declaring a function in an object as virtual is that, if the
program that uses this object calls that function only conditionally, there is no need to bind the function early,
during the compilation of the program. It will be bound only if the condition is met and the call of the function
takes place. For example, you could have a polymorphic function called draw() that is associated with
different graphical objects, like for rectangle, circle, and sphere. The details or methods of the functions are
different, but the name draw() is common. If you now have a collection of these objects and pick up an
arbitrary object without knowing exactly what it is (via a pointer, for example), you can still invoke the draw
function for the object and be assured that the right draw function will be bound to the object and called.
Do'stlaringiz bilan baham: