3

Please excuse my English skills; as I'm not a native speaker.

i got this code from my college textbook and im having trouble understanding it for hours...

#include<iostream>
using namespace std;

class base{
    int a;
protected:
    void seta(int a){this->a=a;}
public:
    void showa(){cout<<a;}
};

class derived: private base{
    int b;
protected:
    void setb(int b){this->b=b;}
public:
    void showb(){
        seta(5); //1
        showa(); //2
        cout<<b;
    }
};

class grandderived: private derived{
    int c;
protected:
    void setab(int x){
        seta(x); //3
        showa(); //4
        setb(x); //5
    }
};

int main(){
    return 0;
}

here are my understandings so far:

class derived inherits class base as private, so everything inside class base are private. afaik private members cannot be accessed from subclasses. so, number 1, 2 wont work. class grandderived also inherits class derived as private, therefore 3,4, and 5 will also not work for the same reason.

but the answer tells me only number 3 and 4 wont work, and others will work. i compiled it and yes it tells me the same.

am i misunderstanding something about how the inheritance works, or are there other things that i dont know of?

Bilal Akil
  • 4,716
  • 5
  • 32
  • 52
hoholee12
  • 334
  • 3
  • 14
  • 1
    possible duplicate of [Difference between private, public, and protected inheritance](http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) – Cory Kramer Jun 01 '15 at 11:57

4 Answers4

3

I believe this is what's happening:

  1. The base class declared seta and showa as protected and public. This means they are accessible to subclasses.

  2. derived inherits from base. This means it has access to everything protected and public from base, which includes seta and showa. The fact that it privately inherits base means that inherited members will be marked as private, and thus cannot be accessed in classes subclassing derived (i.e. grandderived).

  3. This same pattern occurs between derived and grandderived.

Therefore: derived can access the methods from base, and grandderived can access the methods from derived, but NOT base.

Bilal Akil
  • 4,716
  • 5
  • 32
  • 52
1

You're close.

grandderived inherits from derived and marks it as private. However, this means that the methods that has inherited from derived will not be accessible from classes inheriting from grandderived. In the same way, derived inherits from base, marking it as private.

The effect is that grandderived can access the methods of derived but not base.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
1

derived class inherits base class privately, which means all public and protected members of the base class becomes private in the derived class.

Even though it is a private inheritance, private can access to all protected and public members of base. So number 1 and 2 work.

base::seta is a protected member in base, it becomes private in derived's scope. grandderived can't access derived's private members, therefore number 3 and 4 won't work.

derived::setb, number 5, works as it is declared protected in class derived. Although it becomes private in the scope of grandderived, it is accessible from itself. It can't be inherited further.

yildirim
  • 316
  • 7
  • 14
0

Just as you can access your private members but others cannot, you can access your private base classes but others cannot. This is why class derived can access base::seta but class gradderived can't.

The main effect private inheritance has is that objects of the derived type can't be cast (from outside the class) to the base class type.

Motti
  • 110,860
  • 49
  • 189
  • 262