You are correct on point #1. Specifying private
, protected
or public
when inheriting from a base class does not change anything access-wise on the derived class itself. Those access specifiers tell the compiler how to treat the base-class members when instances of the derived class are used elsewhere, or if the derived class happens to be used as a base class for other classes.
UPDATE: The following may help to illustrate the differences:
class Base
{
private: int base_pri;
protected: int base_pro;
public: int base_pub;
};
For classes derived from base:
class With_Private_Base : private Base { void memberFn(); };
class With_Protected_Base : protected Base { void memberFn(); };
class With_Public_Base : public Base { void memberFn(); };
// this would be the same for all of the above 3 classes:
void With_PXXX_Base::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // OK
base_pub = 1; // OK
}
For classes derived from the 3 derived classes:
class A : public With_Private_Base { void memberFn(); }
void A::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // error: `int Base::base_pro' is protected
base_pub = 1; // error: `int Base::base_pub' is inaccessible
}
class B : public With_Protected_Base { void memberFn(); }
void B::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // OK
base_pub = 1; // OK
}
class C : public With_Public_Base { void memberFn(); }
void C::memberFn()
{
base_pri = 1; // error: `int Base::base_pri' is private
base_pro = 1; // OK
base_pub = 1; // OK
}
External access to the first three derived classes:
void main()
{
With_Private_Base pri_base;
pri_base.base_pri = 1; // error: `int Base::base_pri' is private
pri_base.base_pro = 1; // error: `int Base::base_pro' is protected
pri_base.base_pub = 1; // error: `int Base::base_pub' is inaccessible
With_Protected_Base pro_base;
pro_base.base_pri = 1; // error: `int Base::base_pri' is private
pro_base.base_pro = 1; // error: `int Base::base_pro' is protected
pro_base.base_pub = 1; // error: `int Base::base_pub' is inaccessible
With_Public_Base pub_base;
pub_base.base_pri = 1; // error: `int Base::base_pri' is private
pub_base.base_pro = 1; // error: `int Base::base_pro' is protected
pub_base.base_pub = 1; // OK
}