7. Virtual functions and polymorphism
Through the mechanism of virtual functions, C++ enables dynamic binding of member functions, thus implementing polymorphism. This is an extremely powerful concept, enabling the same operation to be applied to objects of different types, without the need to know their exact type. Before presenting virtual functions, we'll start by showing the constraints that weigh on the choice of a method in an inheritance context.
Compatibility rules between a base class and a derived class allow a pointer to a base class to be assigned the value of a pointer to a derived class. However, by default, the type of objects pointed to is defined at compile time.
Example
class A class B :...
You do not have access to this resource.
Exclusive to subscribers. 97% yet to be discovered!
Already subscribed?
Log in!
Ongoing reading
Virtual functions and polymorphism