Chapter 6: Derived Classes
Single inheritance is the relationship between classes that results when a new class is created using the properties of an existing class. The new class, called the derived class, shares the structure and behavior of the original class, called the base class. Inheritance relationships enable derived classes to borrow attributes and operations from existing classes, thus reducing the amount of redundant code in programs.
One of the requirements for an object-oriented language to be called object-oriented is that it must support inheritance. An inheritance tree is a directed graph in which derived classes point toward their base classes.
A base list is a list of one or more classes from which the current class is derived. A member list is a list of data members and function members that make up a class definition.
Private inheritance is the term we use when a derived class restricts access to its base class members by making them private. This prevents users of the derived class from accessing the base class members.
Whenever possible, do not allow a derived class to directly access protected data members in a base class. Instead, make the members private, and provide public Set and Get functions for each data member. This helps to prevent a derived class from being overly dependent upon the implementation of its base class.
A derived class constructor often receives arguments that must be passed on to the constructor of a base class via a constructor-initializer list.
When a name in a base class is hidden by the same name in a derived class, you can use the :: operator to reference the name in the base class (assuming that it is not private), as in base::name.
To help deal with the portability problems that appear when transferring a graphics application program from one system to another, we implemented a class called GraphScreen in this chapter. This was combined with classes that displayed points, rectangles, and squares.
Beginning C++ programmers often make mistakes when describing an is-a relationship between classes. In the case of the Point and Line classes, for example, it is better to let a Line object contain two points, rather than try to derive Line from Point. Similarly, we discussed the problems with using an inheritance relationship between the List and Stack classes.
Multiple inheritance results when a class is derived from two or more base classes. While this is a powerful tool, it can also create naming problems when identical names appear in more than one base class. In our Student/Employee/Person class hierarchy, we showed a specific problem that occurred when both Student and Employee were derived from the same base class, Person. We designated Person as a virtual base class to resolve the ambiguity caused by multiple paths through a class tree.