- Base classes and derived classes
- Object of one class "is an" object of another class
- Example: Rectangle is quadrilateral.
- Class Rectangle inherits from class Quadrilateral
- Quadrilateral: base class
- Rectangle: derived class
- Base class typically represents larger set of objects than derived classes
- Example:
- Base class: Vehicle
Cars, trucks, boats, bicycles, ...
- Derived class: Car
Smaller, more-specific subset of vehicles
- Inheritance examples (see Fig. 4.1)
Figure 4.1:
Inheritance examples
|
- Inheritance hierarchy (see Fig. 4.2 Top)
- Inheritance relationships: tree-like hierarchy structure
- Each class becomes
- Base class; supply data/behaviors to other classes
- OR
- Derived class; inherit data/behaviors from other classes
Figure 4.2:
Inheritance hierarchy for university CommunityMembers and
Inheritance hierarchy for Shapes
|
- public inheritance
- Specify with:
- Class TwoDimensionalShape : public Shape
Class TwoDimensionalShape inherits from class Shape (see Fig. 4.2 Bottom)
- Base class private members
- Not accessible directly
- Still inherited; manipulate through inherited member functions
- Base class public and protected members; inherited with original member access
- friend functions; not inherited
2004-07-29