Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Code Reuse Through Inheritance Summary
Review and test your understanding of all inheritance and code reuse concepts covered in this chapter.
Inheritance recap
Inheritance is one of the most powerful features of object-oriented programming. Let's review the key concepts.
Inheritance Fundamentals
Inheritance allows us to model an is-a relationship between two objects. The object being inherited from is called the parent class, base class, or superclass. The object doing the inheriting is called the child class, derived class, or subclass.
When a derived class inherits from a base class, the derived class acquires all of the members of the base class.
Construction and Destruction Order
When a derived class is constructed, the base portion of the class is constructed first, and then the derived portion is constructed. In more detail:
- Memory for the derived class is set aside (enough for both the base and derived portions).
- The appropriate derived class constructor is called.
- The base class object is constructed first using the appropriate base class constructor. If no base class constructor is specified, the default constructor will be used.
- The initialization list of the derived class initializes members of the derived class.
- The body of the derived class constructor executes.
- Control is returned to the caller.
Destruction happens in the opposite order, from most-derived to most-base class.
Access Specifiers and Protected Members
C++ has 3 access specifiers: public, private, and protected. The protected access specifier allows the class the member belongs to, friends, and derived classes to access protected members, but not the public.
Inheritance Types
Classes can inherit from another class publicly, privately, or protectedly. Classes almost always inherit publicly.
Here's a table of all of the access specifier and inheritance types combinations:
Access specifier in base class
Access specifier when inherited publicly
Access specifier when inherited privately
Access specifier when inherited protectedly
Public
Public
Private
Protected
Private
Inaccessible
Inaccessible
Inaccessible
Protected
Protected
Private
Protected
Extending and Modifying Inherited Behavior
Derived classes can add new functions, change the way functions that exist in the base class work in the derived class, change an inherited member's access level, or hide functionality.
Multiple Inheritance
Multiple inheritance enables a derived class to inherit members from more than one parent. You should generally avoid multiple inheritance unless alternatives lead to more complexity.
Key Terminology
- Inheritance: Mechanism allowing a class to acquire members from another class
- Base class/Parent class/Superclass: The class being inherited from
- Derived class/Child class/Subclass: The class doing the inheriting
- Is-a relationship: The relationship modeled by inheritance
- Protected: Access specifier allowing access to the class, friends, and derived classes
- Public inheritance: Most common inheritance type where public remains public
- Private inheritance: Inheritance where all inherited members become private
- Protected inheritance: Inheritance where public members become protected
- Multiple inheritance: Deriving from more than one base class
Looking Forward
You've learned the fundamental concepts of inheritance in C++. This foundation will serve you well as you explore more advanced topics like polymorphism, virtual functions, abstract classes, and the powerful design patterns that inheritance enables. Understanding when and how to use inheritance effectively is a key skill in object-oriented programming.
Code Reuse Through Inheritance Summary - Quiz
Test your understanding of the lesson.
Practice Exercises
Shape Inheritance Hierarchy
Build a shape inheritance hierarchy with a base Shape class and derived classes for Circle and Rectangle. Demonstrate inheritance, constructor chaining, function overriding, and static member counting.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!