Polymorphism
We've now seen how we can use inheritance to create generic, base versions of objects, then specialize them in derived classes. Some of the many benefits of this include reduced code repetition, the ability to implement a common interface, and polymorphism.
Polymorphism allows us to call different implementations of the same function depending on which inheriting object we're calling the function on. We can do this because we can store derived types in a pointer variable of their base type. When we do this, we limit ourselves to only being able to access the members declared in the base, but when it's called, we'll get the implementations of the derived class.
Let's take a look at some code to see this in action:
// Polymorphism. #include <iostream> #include <string> class MyClassA { public: virtual std::string GetString() = 0; }; class MyClassB: public MyClassA { public: ...