-2

Currently, I am learning OOP, and I've tried many inheritance examples. I just tested this code:

#include <iostream>
using namespace std;

class B 
{
    int a;
protected:
    B(int i=0)
    {
        a=i;
    }

    int get_b()
    {
        return a;
    }
};

class D: private B
{
public:
    D(int x=0): B(x) {}

    int get_a()
    {
        return get_b();
    }
};

int main()
{
    D d(-89);
    cout << d.get_a();
    return 0; 
}

Why does this work? Why can I use the get_b() function? Why does the constructor B(x) work? Why doesn't it work if I change protected to private then?

Later Edit : By using the protected keyword on the constructor and function get_b() means that derived classes have acces to them if the inheritance is public. However, in this case by using private inheritance I would expect that the constructor and the get_b() function would be inaccesible from class D.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 3
    What is your *current* understanding of the access levels `public`, `protected`, and `private`? You should add that to the question. Like this, it seems like you haven't done any research yourself, in which case reading a [good book](https://stackoverflow.com/q/388242/1782465) would be a better course of action than asking on SO. – Angew is no longer proud of SO Jun 07 '17 at 13:14
  • Well, using the protected keyword on the constructor and function get_b() means that derived classes have acces to them. If the inherittance is public, but in this case by using private inheritance I would expect that the constructor and the get_b() method would be inaccesible from class D. – CiobyMorningStar Jun 07 '17 at 13:22

3 Answers3

1

Why can I use the get_b() function?

Because get_b() is protected and can be used by derived class.

Why does the constructor B(x) work?

The same reason, it is protected and accessible by derived class D in this case.

Why doesn't it work if I change protected to private then?

Because private members only accessible by class itself (B in this case) and it's friends, D does not have access to private members of B.

Later Edit : By using the protected keyword on the constructor and function get_b() means that derived classes have acces to them if the inheritance is public.

Bold part in your statement is incorrect. Inheritance specifier says how B members would be accessible in children and users of class D, it does not change access of methods of class D to members of class B.

However, in this case by using private inheritance I would expect that the constructor and the get_b() function would be inaccesible from class D.

Your expectation is wrong. Private inheritance would make members of class B inaccessible in children and users of class D, it does not affect accessibility of members of B for methods of D itself.

Slava
  • 43,454
  • 1
  • 47
  • 90
0

In effect, public, protected, and private inheritance specifies the most liberal access control that will apply to inherited members. What I mean:

Public

class Pub : public B

Public members of B inherited by Pub will be accessible both within Pub itself and from outside (as well as from any classes derived from Pub). Protected members of B inherited by Pub will be accessible within Pub and within classes derived from Pub. Private members of B will not be accessible within Pub.

Another way to put it: B's public members remain public, protected members remain protected, private members are inaccessible.

Protected

class Prot : protected B

Public and protected members of B inherited by Prot will be accesible like protected members of Prot: within the class itself and in classes derived from it, but not from outside. Private members of B are still not accessible to Prot.

Another way to put it: B's public members become protected, protected members remain protected, private members are inaccessible.

Private

class Priv : private B

Public and protected members of B inherited by Priv will be accessible to Priv only, and not to any derived classes (they are accessible just like private members of Priv). Private members of B are still not accessible to Prot.

Another way to put it: B's public and protected members become private, private members are inaccessible.

Community
  • 1
  • 1
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
-2

Private/protected/public inheritance is all about deciding who is aware of the inheritance. In public inheritance, all outside code is aware of it. In protected inheritance, only the children are aware. In private, only the inheriting class is aware. On top of that, the same keywords apply on members, but are not affected by the inheritance visibility. If the class is aware of the inheritance, a member is visible as usual. If it is not aware of the inheritance, it is even less aware of members, be them public, private or protected.

To answer your specific question, if you created a class E inheriting (publicly) your class D, you wouldn't be able to call get_b, but you could still call get_a

David LUC
  • 335
  • 1
  • 5