Now suppose that a B inherits from a , sob* can be used as a *. Similarly, a b& can be used as a a&. However, a A is not necessarily a B, so a* cannot be used as a b*. In general, if a class derived has a public base class base, then we can assign a derived* to a variable of type base* without explicit type conversions. The opposite conversion, that is, from base* to derived*, must be explicit. For example:
void g (Manager Mm,employee ee) {Employee * PE = &mm; // correct: Each manager is an employee manager* pm = ⅇ // error: Not every employee is a manager pm ->level = 2 ; // disaster: EE does not include level = static_cast<manager*> (PE); // violent conversion; it works because PE points to manager mm pm ->level = 2 ; // No problem: PM points to manager mm with level }
In other words, a derived class object can be treated as its base class object if manipulated by pointers and references, and vice versa.
Using a class as a base class is equivalent to defining an (nameless) object of that class, so the class must be determined later to be used as a base class.
Virtual functions
C + + derived classes