Understanding Access Specifiers in Inheritance
Inheritance allows classes to inherit members and functionality from parent classes, but the accessibility of these inherited members depends on the access specifiers used. The access specifiers private, protected, and public control how members are accessed in inherited classes.
Private Inheritance
Example:
class Base { private: int private_member; }; class Derived : private Base { public: void accessPrivate() { // Error: Cannot access 'private_member' because it's private in 'Base' } };
Protected Inheritance
Example:
class Base { protected: int protected_member; }; class Derived : protected Base { public: void accessProtected() { protected_member = 100; // Allowed } };
Public Inheritance
Example:
class Base { public: int public_member; }; class Derived : public Base { public: void accessPublic() { public_member = 200; // Allowed } };
Considerations:
The above is the detailed content of How Do Access Specifiers (private, protected, public) Affect Member Accessibility in Inheritance?. For more information, please follow other related articles on the PHP Chinese website!