Inheritance and OOP
Inheritance and classes
- The more general class is referred to as a base class (or superclass).
- The more specialized class is referred to as a derived class (or subclass).
- A base class contains all that is common among its derived classes.
- The attributes (data) and behaviors (methods) of a base class are
inherited by all its derived classes.
- Derived classes use, extend, modify, or replace the base class behaviors.
Inheritance and OOP
- Inheritance is an abstraction for sharing similarities among
classes while preserving their differences.
- Inheritance allows us to group classes into families of related types,
allowing for the sharing of common operations and data.
- Multiple inheritance is possible, but not advisable. (We will not address it.)
- 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)