Lesson on Types of Inheritance in C++
Introduction
Inheritance is one of the key features of Object-Oriented Programming (OOP) in C++. It allows a class (child class) to derive properties and behavior from another class (parent class). This promotes code reusability and hierarchical classification.
Types of Inheritance in C++
C++ supports five types of inheritance:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid (or Multipath) Inheritance
Let’s go through each type in detail with examples.
1. Single Inheritance
Definition:
A single derived class inherits from a single base class.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark!" << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited function
d.bark(); // Derived class function
return 0;
}
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark!" << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited function
d.bark(); // Derived class function
return 0;
}
Use Cases:
- Representing parent-child relationships (e.g., Vehicle → Car).
- Code reusability for similar objects.
Problems:
- If the base class changes, the derived class may be affected.
2. Multiple Inheritance
Definition:
A derived class inherits from more than one base class.
Example:
#include <iostream>
using namespace std;
// Base class 1
class A {
public:
void showA() {
cout << "Class A" << endl;
}
};
// Base class 2
class B {
public:
void showB() {
cout << "Class B" << endl;
}
};
// Derived class
class C : public A, public B {
public:
void showC() {
cout << "Class C" << endl;
}
};
int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}
#include <iostream>
using namespace std;
// Base class 1
class A {
public:
void showA() {
cout << "Class A" << endl;
}
};
// Base class 2
class B {
public:
void showB() {
cout << "Class B" << endl;
}
};
// Derived class
class C : public A, public B {
public:
void showC() {
cout << "Class C" << endl;
}
};
int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}
Use Cases:
- Combining functionalities from different classes.
- Example: A class
Smartphone
inheriting from both Camera
and Phone
.
Smartphone
inheriting from both Camera
and Phone
.Problems:
- Diamond Problem: If both base classes have the same function, ambiguity arises.
- Solution: Use virtual base classes.
3. Multilevel Inheritance
Definition:
A class inherits from another derived class.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
};
// Derived class from Animal
class Mammal : public Animal {
public:
void walk() {
cout << "I can walk!" << endl;
}
};
// Further derived class from Mammal
class Dog : public Mammal {
public:
void bark() {
cout << "I can bark!" << endl;
}
};
int main() {
Dog d;
d.eat();
d.walk();
d.bark();
return 0;
}
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
};
// Derived class from Animal
class Mammal : public Animal {
public:
void walk() {
cout << "I can walk!" << endl;
}
};
// Further derived class from Mammal
class Dog : public Mammal {
public:
void bark() {
cout << "I can bark!" << endl;
}
};
int main() {
Dog d;
d.eat();
d.walk();
d.bark();
return 0;
}
Use Cases:
- Modeling real-world relationships (e.g.,
Person → Employee → Manager
).
- Extending features in a stepwise manner.
Person → Employee → Manager
).Problems:
- If many levels exist, debugging can be hard.
- Changes in the base class may require changes in all derived classes.
4. Hierarchical Inheritance
Definition:
Multiple classes inherit from a single base class.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "I can bark!" << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "I can meow!" << endl;
}
};
int main() {
Dog d;
Cat c;
d.eat();
d.bark();
c.eat();
c.meow();
return 0;
}
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "I can bark!" << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "I can meow!" << endl;
}
};
int main() {
Dog d;
Cat c;
d.eat();
d.bark();
c.eat();
c.meow();
return 0;
}
Use Cases:
- Used when multiple classes share common properties but have different behaviors.
- Example:
Vehicle
as a base class for Car
, Bike
, and Truck
.
Vehicle
as a base class for Car
, Bike
, and Truck
.Problems:
- If the base class changes, all derived classes are affected.
5. Hybrid (Multipath) Inheritance
Definition:
A combination of two or more types of inheritance.
Example (Using Virtual Base Class to avoid Diamond Problem):
#include <iostream>
using namespace std;
// Base class
class Person {
public:
void show() {
cout << "I am a Person" << endl;
}
};
// Intermediate class 1
class Employee : virtual public Person {
};
// Intermediate class 2
class Student : virtual public Person {
};
// Derived class
class Intern : public Employee, public Student {
};
int main() {
Intern i;
i.show(); // No ambiguity due to virtual inheritance
return 0;
}
#include <iostream>
using namespace std;
// Base class
class Person {
public:
void show() {
cout << "I am a Person" << endl;
}
};
// Intermediate class 1
class Employee : virtual public Person {
};
// Intermediate class 2
class Student : virtual public Person {
};
// Derived class
class Intern : public Employee, public Student {
};
int main() {
Intern i;
i.show(); // No ambiguity due to virtual inheritance
return 0;
}
Use Cases:
- When a class needs to inherit from multiple sources while avoiding redundancy.
- Example:
StudentWorker
inheriting from both Student
and Employee
.
StudentWorker
inheriting from both Student
and Employee
.Problems:
- Complexity increases.
- Requires careful handling of constructors.
Problems for Practice
Problem 1: Single Inheritance
Create a Vehicle
class with attributes speed
and color
. Derive a Car
class that adds brand
and seatingCapacity
. Write functions to display details.
Problem 2: Multiple Inheritance
Design a Printer
class and a Scanner
class. Derive a Copier
class that inherits from both and can perform printing and scanning.
Problem 3: Multilevel Inheritance
Create a Shape
class, then derive Rectangle
from it, and further derive Cuboid
from Rectangle
. Implement functions for area and volume calculations.
Problem 4: Hierarchical Inheritance
Design a BankAccount
class with basic attributes. Derive SavingsAccount
and CurrentAccount
classes from it.
Problem 5: Hybrid Inheritance
Create a Person
class. Derive Student
and Employee
classes. Then derive a TeachingAssistant
class from both using virtual inheritance.
0 Comments