Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does protected inheritance allow the derived class access the private members of its base class?

Tags:

c++

I am really confused about private inheritance and protected inheritance.

1) in protected inheritance, the public and protected members become protected members in the derived class. In the private inheritance, everything is private. However, the derived class can never access the private members of the base class, is that right? The derived class can access the public and protected members in both cases. Is that right?

2) I noticed that the private members of the base class will never be touched by the derived class. So why are the private members inherited?

like image 816
skydoor Avatar asked Jan 28 '10 18:01

skydoor


People also ask

Can derived class access private members of base class?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

Are protected members of a base class accessed in the derived class when inherited?

If a class is derived privately from a base class, all protected base class members become private members of the derived class. Class A contains one protected data member, an integer i . Because B derives from A , the members of B have access to the protected member of A .

Is Protected members are accessible to the member of derived class?

Protected members that are also declared as static are accessible to any friend or member function of a derived class.

Does a derived class inherit private members?

The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them. An instance of the derived class contains instances of the private members of the base class, for obvious reasons.


2 Answers

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
}
like image 169
e.James Avatar answered Nov 05 '22 12:11

e.James


1a) Protected inheritance means the "child" can access everything it could in public inheritance, but others using that object can only see the public interface to the child, anything in it's parent is hidden.

1b) Private inheritance results in all public functions of a class being inherited as private functions - meaning they can not be called from the child or accessed from a client of your object.

2) Private members are inherited because methods in the base class might need them to operate on.

like image 32
EToreo Avatar answered Nov 05 '22 13:11

EToreo