What is inheritance in C++?
Inheritance in C++ is a key feature of Object-Oriented Programming (OOP) that allows a class (called the derived class or child class) to inherit properties and behaviors (methods and data members) from another class (called the base class or parent class). This concept enables code reuse, extensibility, and hierarchical relationships among classes.
Key Concepts of Inheritance:
- Base Class (Parent Class): The class from which properties and methods are inherited.
- Derived Class (Child Class): The class that inherits the properties and methods from the base class.
Syntax:
class Base { // Members and methods of the base class }; class Derived : public Base { // Inherits members and methods from Base class };
Types of Inheritance in C++:
-
Single Inheritance: A derived class inherits from only one base class.
class A { public: void display() { cout << "Class A"; } }; class B : public A { // Class B inherits from class A };
-
Multiple Inheritance: A derived class inherits from more than one base class.
class A { // Class A definition }; class B { // Class B definition }; class C : public A, public B { // Class C inherits from both A and B };
-
Multilevel Inheritance: A derived class inherits from another derived class, creating a chain of inheritance.
class A { // Class A definition }; class B : public A { // Class B inherits from A }; class C : public B { // Class C inherits from B };
-
Hierarchical Inheritance: Multiple derived classes inherit from the same base class.
class A { // Class A definition }; class B : public A { // Class B inherits from A }; class C : public A { // Class C also inherits from A };
-
Hybrid Inheritance: A combination of two or more types of inheritance, typically using multiple inheritance along with other forms.
Example of Inheritance:
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Eating..." << endl; } }; class Dog : public Animal { // Inheritance public: void bark() { cout << "Barking..." << endl; } }; int main() { Dog myDog; myDog.eat(); // Inherited from Animal class myDog.bark(); // Defined in Dog class return 0; }
Access Specifiers in Inheritance:
The relationship between the base class and the derived class depends on the access specifier (public
, protected
, or private
) used when inheriting:
- Public Inheritance: Members of the base class retain their access control in the derived class (public members remain public, protected members remain protected).
- Protected Inheritance: Public and protected members of the base class become protected in the derived class.
- Private Inheritance: Public and protected members of the base class become private in the derived class.
Benefits of Inheritance:
- Code Reusability: Inherited classes can reuse the code from the base class, reducing redundancy.
- Extensibility: New functionality can be added in derived classes without modifying the existing base class.
- Modularity: Allows the program to be organized in a hierarchical manner, making it easier to maintain.
Sources:
GET YOUR FREE
Coding Questions Catalog