Example
For example, a base class Animal
could have a virtual function eat
. Subclass Fish
would implement eat
differently than subclass Wolf
, but you can invoke eat
on any class instance referred to as Animal, and get the eat
behaviour of the specific subclass.
This allows a programmer to process a list of objects of class Animal
, telling each in turn to eat (by calling eat
), without needing to know what kind of animal may be in the list, how each animal eats, or what the complete set of possible animal types might be.
Read more about this topic: Virtual Function
Famous quotes containing the word example:
“Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.”
—Marcel Proust (18711922)