Casting between Types
Now that we can store and interact with types polymorphically, we need to know how to cast between them. Casting is the process of converting an object from one type to another. This is important if we're storing derived types in a collection whose type is the base. In this case, we would need to cast from a base to a derived type. This is called a down-cast and requires a type check. We can also cast from a derived type to a base class, and this is called an up-cast. These are always allowed.
Consider the following:
// Casting. #include <iostream> #include <string> class MyClassA { public: int myInt = 0; }; class MyClassB: public MyClassA { public: std::string myString = ""; }; int main() { MyClassA * myClass = new MyClassB(); std::cout << myClass->myInt << std::endl; std::cout << myClass...