When I declare a member protected in base class and inherit as private in derived class , access to the member is not allowed
class base{
protected:
int a;
};
class derived : public base
{
protected:
int b;
public:
derived():base(){ a=0; b=0;}
void show(){cout<<"a= "<<a<<"\tb= "<<b;}
};
int main ()
{
derived d;
d.a=10; //error: 'int base::a' is protected within this context
d.show();
}
But when I write the derived class, to grant public access for 'a' (protected in base)
class derived : public base
{
protected:
int b;
public:
base::a;
};
int main ()
{
derived d;
d.a=20; // no error
}
Now I can change value of 'a' in main() without any error.
I read in c++ complete reference book, granting access will restore access rights ,but one can not raise or lower the access status.
Can anyone tell me why I'm able to access protected member of base class ,inherited privately , and later given public access like a public variable of derived class ( doesn't it violate encapsulation ,ie Protected member should be restored as protected). Please guide me if my understanding is incorrect