Types of Inheritance and Protected Members in C++
Inheritance is a fundamental concept in C++ that allows one class to derive properties and behavior from another. The protected
access specifier plays a key role in controlling access to members in inheritance. In this article, we will explore the types of inheritance and the behavior of protected members in C++.
Types of Inheritance
- Single Inheritance: A derived class inherits from one base class.
- Multilevel Inheritance: A derived class inherits from another derived class.
- Multiple Inheritance: A derived class inherits from multiple base classes.
- Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
- Hybrid Inheritance: A combination of two or more types of inheritance.
Protected Members in Inheritance
Members declared as protected
in a base class are accessible in derived classes but are not accessible from outside the class hierarchy. The type of inheritance affects how protected
members are treated in the derived class.
1. Single Inheritance
In single inheritance, a derived class inherits directly from a base class. Protected members of the base class remain protected in the derived class.
Example:
#include <iostream> class Base { protected: int protectedVar = 42; public: void showBase() { std::cout << "Base class protectedVar: " << protectedVar << std::endl; } }; class Derived : public Base { public: void showDerived() { std::cout << "Accessing protectedVar in Derived: " << protectedVar << std::endl; } }; int main() { Derived obj; obj.showBase(); obj.showDerived(); return 0; }
Output:
Base class protectedVar: 42 Accessing protectedVar in Derived: 42
2. Multilevel Inheritance
In multilevel inheritance, protected members are accessible to all classes in the inheritance chain.
Example:
#include <iostream> class Base { protected: int protectedVar = 100; }; class Intermediate : public Base { public: void showIntermediate() { std::cout << "Accessing protectedVar in Intermediate: " << protectedVar << std::endl; } }; class Derived : public Intermediate { public: void showDerived() { std::cout << "Accessing protectedVar in Derived: " << protectedVar << std::endl; } }; int main() { Derived obj; obj.showIntermediate(); obj.showDerived(); return 0; }
Output:
Accessing protectedVar in Intermediate: 100 Accessing protectedVar in Derived: 100
3. Multiple Inheritance
In multiple inheritance, a derived class inherits from multiple base classes. Protected members from all base classes are accessible in the derived class.
Example:
#include <iostream> class Base1 { protected: int protectedVar1 = 10; }; class Base2 { protected: int protectedVar2 = 20; }; class Derived : public Base1, public Base2 { public: void show() { std::cout << "Accessing protectedVar1: " << protectedVar1 << std::endl; std::cout << "Accessing protectedVar2: " << protectedVar2 << std::endl; } }; int main() { Derived obj; obj.show(); return 0; }
Output:
Accessing protectedVar1: 10 Accessing protectedVar2: 20
4. Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes inherit from a single base class. Each derived class has access to the base class's protected members.
Example:
#include <iostream> class Base { protected: int protectedVar = 50; }; class Derived1 : public Base { public: void showDerived1() { std::cout << "Accessing protectedVar in Derived1: " << protectedVar << std::endl; } }; class Derived2 : public Base { public: void showDerived2() { std::cout << "Accessing protectedVar in Derived2: " << protectedVar << std::endl; } }; int main() { Derived1 obj1; Derived2 obj2; obj1.showDerived1(); obj2.showDerived2(); return 0; }
Output:
Accessing protectedVar in Derived1: 50 Accessing protectedVar in Derived2: 50
Conclusion
Understanding the behavior of protected members and types of inheritance in C++ is crucial for designing robust and reusable object-oriented code. Protected members provide a controlled level of access, making them essential for inheritance hierarchies.