What is Inheritance?
Inheritance is a mechanism in OOP where a new class (child/derived class) acquires the properties and behavior of an existing class (parent/base class).
Parent class (Base class) → provides properties and behavior
Child class (Derived class) → inherits these and can have its own additional features
Purpose:
Reuse code
Extend functionality
Make hierarchical relationships like real life
Real-World Analogy
Parent: Vehicle → has speed, fuel, start() function
Child: Car → inherits speed, fuel, start() + adds brand, doors
Why it’s useful:
No need to write the same code again for Car
Car can use Vehicle’s features and add its own
Syntax of Inheritance in C++
access_specifier → determines how base members are accessible:
public,protected,private
Example 1: Basic Inheritance
Explanation:
Carinherits fromVehicleCarobject can accessVehicle’s speed and start()Car can also have its own properties like brand
Types of Inheritance
Inheritance can be classified based on number of classes and levels:
1. Single Inheritance
One child class inherits from one parent class
Example:
Simple, clear, easy to understand
2. Multiple Inheritance
One child class inherits from more than one parent class
Example:
Note:
Can lead to ambiguity if both parents have the same function name
Can be solved using scope resolution operator
3. Multilevel Inheritance
Child class inherits from parent, then grandchild inherits from child
Example:
Memory view:
Object stores all variables from Vehicle → Car → SportsCar
Functions are shared in code segment
4. Hierarchical Inheritance
Multiple child classes inherit from same parent class
Example:
Both Car and Bike reuse Vehicle’s code
5. Hybrid Inheritance
Combination of two or more types
Example:
Multilevel + Multiple Inheritance
Note:
Can lead to ambiguity problems → diamond problem
Diamond Problem Example:
Solution: Use virtual inheritance in C++
Access Specifiers in Inheritance
| Type of Inheritance | Base member access in derived class |
|---|---|
| Public | Public → Public, Protected → Protected |
| Protected | Public → Protected, Protected → Protected |
| Private | Public → Private, Protected → Private |
Tip: Use public inheritance for “is-a” relationship (most common)
Real-World Examples (Extra)
Vehicle Hierarchy
Vehicle → Car → SportsCar
Vehicle → Bike
Common properties inherited: speed, start(), stop()
Employee Hierarchy
Employee → Manager
Employee → Developer
Shared properties: name, ID, salary
School Hierarchy
Person → Student
Person → Teacher
Shared properties: name, age
Advantages of Inheritance
Code Reusability → don’t rewrite same code
Extensibility → child class can add new features
Real-world modeling → like parent-child relationships
Maintenance → changes in base class reflect automatically in children
Important Notes
Constructor/Destructor order:
Base class constructor runs first
Derived class constructor runs after
Destructors run in reverse
Output: AB~B~A
Multiple inheritance → be careful of ambiguity
Use protected for members that derived classes should access but outsiders shouldn’t
Memory Layout
Derived object memory contains:
Base class variables
Derived class variables
Functions: stored once in code segment
Example:
Memory of Car object:
| Memory | Content |
|---|---|
| Base part | speed |
| Derived part | doors |