Interchangeable objects with polymorphism (cont…)
•A function that calls a member function declared in a base class through a pointer or a reference to the base data type can call the version of the member function which is overriden in the derived class, if the method is tagged with the virtual keyword.
•
http://www.alessandroogheri.com
•//we assume here that the class Derived is derived from the class Base and that in the class Base there is the do_something_cool() member function declared
•void f(Base * bp) {
•bp->do_something_cool();
•}
•
•Derived D; Base B;
•f(&B); //legal: calls Base->do_something_cool()
•f(&D); //legal too : it calls Base->do_something_cool()
•       //if the method is not virtual,
•       //it calls Derived->do_something_cool() otherwise
If the BirdController reLocate() member function performs something like:
void reLocate() {
  BirdReference_inside_of_BirdController.move();
}
and provided that move() is virtual inside of Bird, if the BirdReference_inside... data member of BirdController is once connected to a Goose and once connected to a Penguin, their corresponding versions of move() get called, which probably is differently implemented in the two classes (a Penguin can not fly, for instance).