Good introduction to virtual functions! Though there are couple of things I would add. First I would make the base class destructor virtual so the derived class instances are destructed correctly also through base class pointers. Also, in modern C++ (C++11 onwards) it is a good practise to use override keyword in the derived classes. This way the compiler can produce error if the function was not overridden (e.g. missing virtual in base class). It hurts a little inside to see objects not being deleted ;)
Hm, TIL. I never encountered override until your comment. I'll start using that, and its cousin final. Thanks! :-)
In the meantime, I've edited to use that in the code above.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.