Possible Duplicate:
Difference between private, public and protected inheritance in C++
What is difference between deriving as protected or private in c++? i am not able to figure out, since both seem to restrict base class member access from derived class object
If the class member declared as public then it can be accessed everywhere. If the class members declared as protected then it can be accessed only within the class itself and by inheriting child classes. If the class members declared as private then it may only be accessed by the class that defines the member.
In C++, there are three access specifiers: public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
Things that are private are only visible within the class itself. Things that are protected are visible in the class itself and in subclasses.
Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.
Let's consider a code example showing what would be allowed (or not) using different levels of inheritance:
class BaseClass {};
void freeStandingFunction(BaseClass* b);
class DerivedProtected : protected BaseClass
{
DerivedProtected()
{
freeStandingFunction(this); // Allowed
}
};
DerivedProtected
can pass itself to freeStandingFunction
because it knows it derives from BaseClass
.
void freeStandingFunctionUsingDerivedProtected()
{
DerivedProtected nonFriendOfProtected;
freeStandingFunction(&nonFriendOfProtected); // NOT Allowed!
}
A non-friend (class, function, whatever) cannot pass a DerivedProtected
to freeStandingFunction
, because the inheritance is protected, so not visible outside derived classes. Same goes for private inheritance.
class DerivedFromDerivedProtected : public DerivedProtected
{
DerivedFromDerivedProtected()
{
freeStandingFunction(this); // Allowed
}
};
A class derived from DerivedProtected
can tell that it inherits from BaseClass
, so can pass itself to freeStandingFunction
.
class DerivedPrivate : private BaseClass
{
DerivedPrivate()
{
freeStandingFunction(this); // Allowed
}
};
The DerivedPrivate
class itself knows that it derives from BaseClass
, so can pass itself to freeStandingFunction
.
class DerivedFromDerivedPrivate : public DerivedPrivate
{
DerivedFromDerivedPrivate()
{
freeStandingFunction(this); // NOT allowed!
}
};
Finally, a non-friend class further down the inheritance hierarchy cannot see that DerivedPrivate
inherits from BaseClass
, so cannot pass itself to freeStandingFunction
.
Use this matrix (taken from here) to decide the visibility of inherited members:
inheritance\member | private | protected | public --------------------+-----------------+---------------+-------------- private | inaccessible | private | private protected | inaccessible | protected | protected public | inaccessible | protected | public --------------------+-----------------+---------------+--------------
Example 1:
class A { protected: int a; }
class B : private A {}; // 'a' is private inside B
Example 2:
class A { public: int a; }
class B : protected A {}; // 'a' is protected inside B
Example 3:
class A { private: int a; }
class B : public A {}; // 'a' is inaccessible outside of A
private
allows only the class it is declared in to access it
protected
allows that class and derived/sub classes to access as if it were private
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With