Inheritance is one of the most important concepts of Object-Oriented Programming (OOP) in C++. It allows one class to inherit properties and behaviors from another class.
What is Inheritance?
Inheritance is the process of creating a new class from an existing class. The new class automatically gets the data members and functions of the existing class.
Key Terms in Inheritance
Base Class (Parent Class)
The class whose properties are inherited.
Derived Class (Child Class)
The class that inherits features from the base class.
Why Use Inheritance?
Inheritance is useful because it:
- Promotes code reusability
- Reduces code duplication
- Improves program structure
- Makes code easier to maintain
- Supports real-world relationships
Syntax of Inheritance
class BaseClass {
// members
};
class DerivedClass : public BaseClass {
// additional members
};
Example of Inheritance
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
Output
Eating...
Barking...
How Inheritance Works
- Child class inherits parent class members
- Derived class can use parent functions
- Derived class can also add its own functions
- Code reuse becomes easier
Types of Inheritance in C++
1. Single Inheritance
One derived class inherits from one base class.
class A {
};
class B : public A {
};
2. Multilevel Inheritance
A class is derived from another derived class.
Example of Multilevel Inheritance
#include <iostream>
using namespace std;
class A {
public:
void showA() {
cout << "Class A" << endl;
}
};
class B : public A {
public:
void showB() {
cout << "Class B" << endl;
}
};
class C : public B {
public:
void showC() {
cout << "Class C" << endl;
}
};
int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}
Output
Class A
Class B
Class C
3. Multiple Inheritance
A class inherits from more than one base class.
class A {
};
class B {
};
class C : public A, public B {
};
4. Hierarchical Inheritance
Multiple classes inherit from one base class.
class A {
};
class B : public A {
};
class C : public A {
};
5. Hybrid Inheritance
Combination of different inheritance types.
Access Modes in Inheritance
Inheritance can use:
- Public inheritance
- Protected inheritance
- Private inheritance
Public Inheritance
Public members remain public in derived class.
class B : public A {
};
Real-Life Example
Think of vehicles:
Parent Class
Vehicle
- Engine
- Wheels
- Speed
Child Class
Car
- Inherits vehicle features
- Adds AC and music system
Advantages of Inheritance
- Reusable code
- Better organization
- Easier maintenance
- Faster development
- Supports real-world modeling
Why Inheritance is Important
Inheritance is important because it:
- Simplifies programming
- Reduces repetition
- Improves scalability
- Supports advanced OOP concepts
Applications of Inheritance
Inheritance is widely used in:
- Game development
- Banking systems
- Software frameworks
- GUI applications
- Management systems
Conclusion
Inheritance in C++ allows one class to reuse the features of another class, making programs more organized, reusable, and maintainable. It is a powerful OOP feature that helps model real-world relationships and build scalable applications.