Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Rust Go Linux CPU Others Videos
Advertisement

Article by Ayman Alheraki on January 11 2026 10:33 AM

Modern C++ OOP Concepts Inheritance and Its Types

Modern C++ OOP Concepts: Inheritance and Its Types

Inheritance is a core concept in Object-Oriented Programming (OOP) that allows one class (called the derived or child class) to inherit the properties and behaviors (methods) of another class (called the base or parent class). This promotes code reusability, modularity, and scalability, making OOP a powerful paradigm for software development.

In C++, inheritance is categorized into three main types based on the access specifiers:

  • Public Inheritance

  • Private Inheritance

  • Protected Inheritance

Let’s explore each type with detailed explanations and examples.


1. Public Inheritance

When a class is publicly inherited, all public members of the base class remain public in the derived class, and all protected members of the base class remain protected in the derived class. The private members of the base class are not directly accessible in the derived class but can be accessed through public or protected member functions of the base class.

Example:

Explanation:

  • In Derived, we have access to publicVar and protectedVar but not privateVar.

  • We can access the private variable privateVar indirectly using the showPrivate() function.


2. Private Inheritance

In private inheritance, all members of the base class, whether public or protected, are treated as private members of the derived class. This means they can only be accessed within the derived class and are not visible outside the class.

Example:

Explanation:

  • In Derived, publicVar and protectedVar become private members, so they cannot be accessed outside Derived, but we can access them within the class.

  • Attempting to access them outside the class will result in a compile-time error.


3. Protected Inheritance

With protected inheritance, all public and protected members of the base class become protected members in the derived class. They are accessible within the derived class and its subclasses but not from outside.

Example:

Explanation:

  • In Derived, publicVar and protectedVar become protected members.

  • In SubDerived, we can access these variables because they are protected, but they are not accessible outside the class hierarchy.


Why Inheritance Matters

  • Code Reusability: With inheritance, developers can extend existing code without rewriting it. This leads to more modular and maintainable code.

  • Polymorphism: Derived classes can override base class methods to provide specific implementations, which is key to dynamic binding and runtime polymorphism.

  • Abstraction and Encapsulation: Inheritance helps in creating an abstraction layer by hiding the implementation details of the base class and exposing only the necessary functionality.


Key Considerations

  • Use the correct inheritance type based on the intended visibility of the base class members. Public inheritance is most common, but protected and private inheritance have specific use cases when more controlled access is needed.

  • Multiple Inheritance: In C++, a derived class can inherit from more than one base class. This requires careful handling of potential ambiguities, such as the "diamond problem."

Conclusion

Inheritance is a foundational concept in OOP that drives many advanced features, such as polymorphism and code reuse. Understanding the differences between public, private, and protected inheritance allows you to design more flexible and robust programs.

Advertisements

Responsive Counter
General Counter
1276058
Daily Counter
1298