Access Control in Inheritance in C++
Access control in inheritance determines how members of a base class are accessed in derived classes. In C++, access control is influenced by access specifiers (public
, protected
, and private
) and the type of inheritance (public
, protected
, or private
).
Access Specifiers
- Public: Members are accessible from anywhere.
- Protected: Members are accessible in the base class and derived classes but not outside.
- Private: Members are accessible only within the base class.
Types of Inheritance
The type of inheritance defines how the access specifiers of the base class affect the derived class:
Base Class Specifier | Access in Derived Class (Public Inheritance) | Access in Derived Class (Protected Inheritance) | Access in Derived Class (Private Inheritance) |
---|---|---|---|
Public | Public | Protected | Private |
Protected | Protected | Protected | Private |
Private | Not Inherited | Not Inherited | Not Inherited |
Examples of Access Control in Inheritance
1. Public Inheritance
In public inheritance, public members of the base class remain public in the derived class, and protected members remain protected.
Example:
#include <iostream> class Base { public: int publicVar = 1; protected: int protectedVar = 2; private: int privateVar = 3; }; class Derived : public Base { public: void show() { std::cout << "Public: " << publicVar << std::endl; std::cout << "Protected: " << protectedVar << std::endl; // privateVar is not accessible } }; int main() { Derived d; d.show(); std::cout << "Public: " << d.publicVar << std::endl; // d.protectedVar is not accessible outside the class return 0; }
Output:
Public: 1 Protected: 2 Public: 1
2. Protected Inheritance
In protected inheritance, public and protected members of the base class become protected in the derived class.
Example:
#include <iostream> class Base { public: int publicVar = 1; protected: int protectedVar = 2; private: int privateVar = 3; }; class Derived : protected Base { public: void show() { std::cout << "Public (now Protected): " << publicVar << std::endl; std::cout << "Protected: " << protectedVar << std::endl; } }; int main() { Derived d; d.show(); // d.publicVar is not accessible outside the class return 0; }
Output:
Public (now Protected): 1 Protected: 2
3. Private Inheritance
In private inheritance, public and protected members of the base class become private in the derived class.
Example:
#include <iostream> class Base { public: int publicVar = 1; protected: int protectedVar = 2; private: int privateVar = 3; }; class Derived : private Base { public: void show() { std::cout << "Public (now Private): " << publicVar << std::endl; std::cout << "Protected (now Private): " << protectedVar << std::endl; } }; int main() { Derived d; d.show(); // d.publicVar is not accessible outside the class return 0; }
Output:
Public (now Private): 1 Protected (now Private): 2
Conclusion
Access control in inheritance ensures encapsulation while allowing flexibility in class relationships. Understanding access specifiers and inheritance types helps in designing robust and secure object-oriented programs in C++.