What is the purpose of ‘protected’ access specifier used in C++?
Protected access specifier: 1. Protected access specifier is mainly used in inheritance in C++. 2. It uses protected keyword defined in C++ language. 3. Class members declared as protected can be accessed by the member functions within its class and any class immediately derived from it. 4. These members cannot be accessed by the functions outside these two classes.
Example:
class base
{
protected:
int b;
public:
void display()
{
cout<<b;
}
};
class derived:public base
{
public:
void show()
{
cout<<b;
}
};
void main()
{
derived d;
d.display();
d.show();
}