Home | Projects | Notes > C++ Programming > Inheritance

Inheritance

 

Overview

 

What is Inheritance and Why is it Used?

Case Study: Accounts

Without inheritance (Code duplication)

Here, each class is independent of each other.

With inheritance (Code reuse!)

Here, these classes are now interrelated. Savings_Account, Checking_Account, Trust_Account classes depend on the Account class, and there is an inheritance hierarchy.

 

Terminology

 

base-derived

 

Examples

 

class-hierarchy-1

 

 

class-hierarchy-2

 

 

Public Inheritance ("Is - A") vs. Composition ("Has - A")

Public Inheritance

Composition

Example

 

public-inheritance-vs-composition

 

 

When to Choose Inheritance over Composition?

 

Composition Shown in a Class

 

Types of Inheritance in C++

 

Deriving Classes from Existing Classes

 

Protected Members & Class Access

 

 

public-protected-private-inheritances

 

 

Constructors & Destructors

Constructors

Destructors

Note

Example

 

Copy/Move Constructors and Operator = with Derived Classes

Example

 

Using and Redefining Base Class Methods

Static Binding of Method Calls

Example

 

Multiple Inheritance

Example

 

multiple-inheritance