Inheritance
Inheritance
by which one class (called the derived or child class) can acquire the properties and behaviors (data
members and member functions) of another class (called the base or parent class). This enables the
creation of a hierarchical relationship between classes, promoting code reuse and modularity.
Inheritance in Classes:
In C++, inheritance allows a derived class to inherit members from a base class. The derived class can use
or override these members and may also introduce additional members specific to itself.
Real-World Example:
Consider the relationship between a **Vehicle** and a **Car**:
- **Base Class**: Vehicle (which has general attributes like speed, weight, and methods like move (),
stop ()).
- **Derived Class**: Car (which inherits from Vehicle and adds specific attributes like the number of
doors, model type, and methods like playMusic ()).
Code Example:
class Vehicle {
public:
int speed;
void move () {
void stop () {
};
public:
int numberOfDoors;
void playMusic () {
};
In this example, the `Car` class inherits the `speed`, `move () `, and `stop () ` from the `Vehicle` class and
adds its own specific functionality.
```
+---------------------+
+---------------------+
+---------------------+
+---------------------+
+---------------------+
+---------------------+
+---------------------+
Advantages of Inheritance:
1. Code Reusability: The derived class inherits properties and behaviors from the base class, allowing
shared functionality to be written once in the base class and reused across many derived classes.
2. Modularity: Changes made in the base class (like fixing a bug or improving functionality) are
automatically inherited by the derived classes, making maintenance easier.
3. Extensibility: You can extend the base class with new functionality in derived classes without altering
the original base class.
4. Polymorphism: Through inheritance, C++ allows objects to be treated as instances of their parent
class, enabling dynamic method overriding.
The main purpose of inheritance is to “reuse” code across multiple related classes without redundancy.
Instead of writing the same methods or attributes multiple times, you define them once in a base class
and inherit them where necessary.
- A Student class could inherit from Person and add specific attributes like `studentID` and methods like
`study () `.
- Similarly, a Teacher class could inherit from Person and add attributes like `teacherID` and methods like
`teach () `.
By using inheritance, the `Student` and `Teacher` classes can reuse the common properties and behavior
defined in the `Person` class.
Example:
class Person {
public:
string name;
int age;
void introduce() {
cout << "My name is " << name << " and I am " << age << " years old.";
};
public:
int studentID;
void study() {
};
public:
int teacherID;
void teach() {
};
```
In this example:
- **Student** and **Teacher** reuse the `name`, `age`, and `introduce()` method from **Person**.
- They also add their own specific attributes (`studentID`, `teacherID`) and methods (`study()`, `teach()`).
Summary:
- **Inheritance** allows derived classes to reuse and extend the properties and behaviors of base
classes.
- The main benefit is **code reuse** while maintaining the flexibility to add new features in derived
classes.