1Definition
Inheritance is the mechanism by which one class (the derived/child class) acquires the attributes and behaviors of another class (the base/parent class). It models an IS-A relationship and promotes code reusability.
2Simple Explanation
Just as a Child inherits physical traits (like eye color or height) from their Parents, a derived class automatically inherits variables and methods defined in its base class. The child class can reuse inherited features as-is, modify (override) them, or add brand-new features.
3Real-Life Example / Analogy
- Mother & Daughter: A daughter inherits facial features and voice tone from her mother, but she can also acquire new skills like playing the violin.
- Vehicle Taxonomy: A
Vehicleparent class definesspeedandfuel.CarandBikeinherit these properties, adding specific features likeairbagsorkickStart.
4Diagram / Visual Representation
5Working / How It Works
- C++ Syntax:
class DerivedClass : visibility-mode BaseClass - Visibility Modes:
| Base Member Access | public Inheritance |
protected Inheritance |
private Inheritance |
|---|---|---|---|
private |
Inaccessible | Inaccessible | Inaccessible |
protected |
protected |
protected |
private |
public |
public |
protected |
private |
6Types / Components / Characteristics
- Single Inheritance: 1 Base Class 1 Derived Class.
- Multilevel Inheritance: Class A Class B Class C.
- Hierarchical Inheritance: 1 Base Class Multiple Derived Classes.
- Multiple Inheritance: Multiple Base Classes 1 Derived Class.
- Hybrid Inheritance: Combination of two or more inheritance types.
- Virtual Inheritance: Resolves the Diamond Problem (duplicate base sub-objects in multiple inheritance paths).
7Solved Examples / Problems
Problem Statement: Write a C++ program demonstrating Multiple Inheritance and Virtual Base Classes to solve the Diamond Problem.
#include <iostream>
using namespace std;
class Animal {
public:
int age;
Animal() { age = 10; }
};
// Virtual inheritance avoids duplicate Animal instances
class Mammal : virtual public Animal {};
class WingedAnimal : virtual public Animal {};
class Bat : public Mammal, public WingedAnimal {};
int main() {
Bat b;
cout << "Bat Age: " << b.age << endl; // Unambiguous access
return 0;
}Output:
Bat Age: 108Advantages, Disadvantages & Applications
- Advantages:
- Reusability of established base class functionality without rewriting code.
- Establishes clear domain hierarchies.
- Disadvantages:
- Tight coupling between parent and child classes.
- Misuse can lead to deep, unmaintainable class hierarchies.
- Applications: GUI Widget frameworks (e.g.,
WindowDialogFileDialog).
9Important Points to Remember
- Constructors are executed in order from Base to Derived; Destructors run in reverse order (Derived to Base).
- Virtual inheritance ensures only one shared copy of a common base class is created in memory.
10Test Your Understanding
- Conceptual Question: What is the Diamond Problem in Multiple Inheritance?
Show answer
When a class inherits from two derived classes that share a common base class, leading to duplicate base sub-objects and compiler ambiguity. - MCQ: Under
privatevisibility mode, public members of the base class become what in the derived class?- A) Public
- B) Protected
- C) Private
- D) Inaccessible
Show answer
C) Private - Application Question: Trace constructor call order for
class C : public B, public A.Show answer
B's constructor runs first, followed by A's constructor, and finally C's constructor.
