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:

  1. Reuse code

  2. Extend functionality

  3. 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++

 
class Base {
// base class members
};
class Derived :
access_specifier Base {
// derived class members

};
  • access_specifier → determines how base members are accessible: public, protected, private

Example 1: Basic Inheritance

 
#include <iostream>
using namespace std;
class Vehicle {
public:
int speed;
void start() {
cout << "Vehicle started" << endl;
}
};
class Car : public Vehicle {
public:
string brand;
void showBrand() {
cout << "Brand: " << brand << endl;
}
};
int main() {
Car c;
c.speed = 100; // inherited from Vehicle
c.brand = "Toyota"; // own property

c.start(); // inherited method

c.showBrand(); // own method
}

Explanation:

  • Car inherits from Vehicle

  • Car object can access Vehicle’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:

 
class A { /* parent */ };
class B :
public A { /* child */ };
  • Simple, clear, easy to understand

2. Multiple Inheritance

  • One child class inherits from more than one parent class

Example:

 
class A {
public:
void funcA() {
cout << "A";
}
};
class B {
public:
void funcB() {
cout << "B";
}
};
class C :
public A, public B { };
int main() {
C obj;
obj.funcA(); // from A
obj.funcB(); // from B
}

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:

 
class Vehicle {
public:
void start() {
cout << "Vehicle started";
}
};
class Car :
public Vehicle {
public:
void honk() {
cout << "Car honk";
}
};
class SportsCar :
public Car {
public:
void turbo() {
cout << "Turbo boost";
}
};
int main() {
SportsCar sc;
sc.start(); // Vehicle method
sc.honk(); // Car method
sc.turbo(); // SportsCar method
}

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:

 
class Vehicle {
public:
void start() {
cout << "Vehicle started";
}
};
class Car :
public Vehicle {};
class Bike :
public Vehicle { };
int main() {
Car c;
Bike b;
c.start();
b.start();
}
  • 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:

 
class A {
public:
void show() {
cout << "A";
}
};
class B :
public A { };
class C :
public A { };
class D :
public B, public C { };
int main() {
D obj;
// obj.show(); ❌ Ambiguity: which show() to call?
}

Solution: Use virtual inheritance in C++

Access Specifiers in Inheritance

Type of InheritanceBase member access in derived class
PublicPublic → Public, Protected → Protected
ProtectedPublic → Protected, Protected → Protected
PrivatePublic → Private, Protected → Private

Tip: Use public inheritance for “is-a” relationship (most common)

Real-World Examples (Extra)

  1. Vehicle Hierarchy

    • Vehicle → Car → SportsCar

    • Vehicle → Bike

    • Common properties inherited: speed, start(), stop()

  2. Employee Hierarchy

    • Employee → Manager

    • Employee → Developer

    • Shared properties: name, ID, salary

  3. School Hierarchy

    • Person → Student

    • Person → Teacher

    • Shared properties: name, age

Advantages of Inheritance

  1. Code Reusability → don’t rewrite same code

  2. Extensibility → child class can add new features

  3. Real-world modeling → like parent-child relationships

  4. 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

 
class A {
public:
A() {
cout << "A";
}
~A() {
cout << "~A";
}
};
class B :
public A {
public:
B() {
cout << "B";
}
~B() {
cout << "~B";
}
};
int main() {
B obj;
}

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:

 
class Vehicle {
int speed;
};
class Car :
public Vehicle {
int doors;
};

Memory of Car object:

MemoryContent
Base partspeed
Derived partdoors