Inheritance and OOP

Inheritance and classes

  1. The more general class is referred to as a base class (or superclass).
  2. The more specialized class is referred to as a derived class (or subclass).
  3. A base class contains all that is common among its derived classes.
  4. The attributes (data) and behaviors (methods) of a base class are inherited by all its derived classes.
  5. Derived classes use, extend, modify, or replace the base class behaviors.

Inheritance and OOP

  1. Inheritance is an abstraction for sharing similarities among classes while preserving their differences.
  2. Inheritance allows us to group classes into families of related types, allowing for the sharing of common operations and data.
  3. Multiple inheritance is possible, but not advisable. (We will not address it.)
  4. Inheritance promotes code reuse.

Public inheritance in C++

C++ provides for "public", "private" and "protected" inheritance
We will limit our discussion to "public" inheritance

Assume that class D (Derived) publicly inherits from class B (Base).
Every object of type D is a B, but not vice versa.
D is a more specialized version of B.
Anywhere an object of type B can be used, an object of type D can be used just as well, but not vice versa.

(Adapted from: Effective C++, 2nd edition, pg. 155)