Inheritance: protected Access Specifier

The protected access specifier plays a prominent role in inheritance. The protected access specifier causes a member to be visible by the member function and friend function of a class where it is declared. That means it causes same effect as the private access specifier within a class or outside the class. But protected has a different effect in derived class. The protected members are visible to derived class members and friend of the classes derived from the base class where the protected members are declared. However, private members in the base class are not visible in the derived class. Like private members, protected members are visible inside the class and are not visible outside the class. The public access specifier causes the member to be visible to any members of the base class or to any function that is outside of the class.
The protected access specifier are sueful in inheritance. The purpose of making data member protected in a class is to make such members accessible to derived class function which is derived form this class. No function other than the base class functions or the functions from the derived class or the friend of derived class can access the protected data of the base class.
Points to be considered when making members private, public or protected.
  • A private member is accessible only to member of the class in which they are declared. They are not visible in derived class or from outside of the class where they are declared except friend functions/ classes.
  • A private member of the base class cab be accessed in the derived class through the public member functions of the base class.
  • A protected member is accessible to members of its own and any of the members in a derived class or the friend of base or friend of derived class.
  • The members that might be used in derived class should be declared as protected rather than private.
  • A public member is accessible to members of its own class, member of derived class and even outside the class.
SHARE Inheritance: protected Access Specifier

You may also like...

Leave a Reply

Your email address will not be published.