Class Inheritance

Class Relationships:

Inheritance is one of four class relationships, doc, we use to construct Object Oriented Designs: Inheritance, Composition, Aggregation, and Using. Inheritance is an "is-a" relationship. Composition is a strong ownership or "part-of" relationship. Aggregation is also a "part-of" relationship, but is weaker than composition because it does not guarantee that the aggregated part is owned, only that it can be owned by the aggregator. Finally, using is a dependency on an object that is not part of the user, was not created by the user, and should not be destroyed by the user.

Inheritance:

People
  • Person Hierarchy Demonstration - one project in GettingStartedExamples
  • Inheritance is a specialization relationship. Soldier specializes Person and Officer, Sergeant, and Private specialize Soldier.
  • Class methods may be qualifed as virtual, and may be declared to be pure virtual (using = 0).
    • Non-virtual methods should not be redefined in derived classes.
    • Virtual methods may be, but don't have to be, redefined in derived classes.
    • Pure virtual methods must be defined in derived classes because the base doesn't define them.
  • Constructors, destructors, and assignment operators are not inherited. All other methods are.
  • Code reuse through inheritance is useful.
  • The most important behavior is substitution. Any pointer or reference to a base instance may be replaced by a pointer or reference to any instance of a class that derives from the base. See the show(Soldier&) function for an example.
Soldiers.h - first part
Soldiers.h - second part
Soldiers output
Virtual Function Pointer Table

More Details about Inheritance:

Some important examples of inheritance:

Inheritance plays a major role in building Plugins, as we will see later.