How many types of inheritance are there in C++?
In C++, inheritance is a fundamental concept that allows a class to inherit properties and behaviors from another class. This mechanism is crucial for code reuse and creating a hierarchical structure of classes. There are several types of inheritance in C++, each serving different purposes and offering unique ways to organize and extend classes. Let’s explore these types in detail.
1.
Single Inheritance
Single inheritance is the most basic form of inheritance, where a derived class inherits from a single base class. This type of inheritance is useful when a class needs to extend the functionality of another class without adding any new relationships.
Example:
“`cpp
class Base {
public:
void display() {
cout << "This is Base class";
}
};
class Derived : public Base {
public:
void display() {
cout << "This is Derived class";
}
};
```
2.
Multiple Inheritance
Multiple inheritance allows a derived class to inherit from more than one base class. This type of inheritance is useful when a class needs to combine features from two or more unrelated classes.
Example:
“`cpp
class Base1 {
public:
void display1() {
cout << "This is Base1 class";
}
};
class Base2 {
public:
void display2() {
cout << "This is Base2 class";
}
};
class Derived : public Base1, public Base2 {
public:
void display() {
display1();
display2();
}
};
```
3.
Multi-level Inheritance
Multi-level inheritance occurs when a derived class is derived from another derived class. This creates a hierarchical structure of classes, where a class inherits properties and behaviors from its parent class and further extends them.
Example:
“`cpp
class Base {
public:
void display() {
cout << "This is Base class";
}
};
class Derived : public Base {
public:
void display() {
cout << "This is Derived class";
}
};
class Derived2 : public Derived {
public:
void display() {
cout << "This is Derived2 class";
}
};
```
4.
Hierarchical Inheritance
Hierarchical inheritance occurs when multiple derived classes inherit from a single base class. This creates a tree-like structure of classes, where a base class serves as a common ancestor for multiple derived classes.
Example:
“`cpp
class Base {
public:
void display() {
cout << "This is Base class";
}
};
class Derived1 : public Base {
public:
void display() {
cout << "This is Derived1 class";
}
};
class Derived2 : public Base {
public:
void display() {
cout << "This is Derived2 class";
}
};
```
5.
Hybrid Inheritance
Hybrid inheritance is a combination of multiple and hierarchical inheritance. It allows a derived class to inherit from more than one base class, which are themselves derived from a common base class.
Example:
“`cpp
class Base1 {
public:
void display1() {
cout << "This is Base1 class";
}
};
class Base2 {
public:
void display2() {
cout << "This is Base2 class";
}
};
class Base3 : public Base1, public Base2 {
public:
void display() {
display1();
display2();
}
};
class Derived : public Base3 {
public:
void display() {
cout << "This is Derived class";
}
};
```
In conclusion, C++ offers various types of inheritance to cater to different programming needs. Understanding these types can help developers create more efficient and maintainable code by leveraging the power of inheritance.