C ++ private class inheritance
Please excuse my knowledge of English; since I am not a native speaker.
I got this code from my college textbook and they didn't manage to figure it out 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 guesses:
class derived
inherits the class base
as private, so everything inside the class base is private. Afaik private members cannot be accessed from subclasses. so the number 1, 2 won't work. class grandderived
also inherits the class derived
as private, so 3,4 and 5 won't work either for the same reason.
but the answer tells me that only the numbers 3 and 4 won't work and others will work. i compiled it and yes it tells me the same thing.
I don't understand something about how inheritance works, or are there other things I don't know about?
source to share
I believe this is what is happening:
-
The class
base
is declaredseta
andshowa
both protected and public. This means that they are available to subclasses. -
derived
inherited frombase
. This means that he has access to all protected and publicly accessible frombase
, which includesseta
andshowa
. The fact that it privately inheritsbase
means that inherited members will be marked private and therefore cannot be accessed in subclassesderived
(i.e.grandderived
). -
The same picture arises between
derived
andgrandderived
.
Hence: derived
can access methods from base
, and grandderived
can access methods from derived
, but NOT base
.
source to share
You are close.
grandderived
inherits from derived
and marks it private. However, this means that methods inherited from derived
will not be accessible from classes inheriting from grandderived
. Similarly, derived
inherits from base
, marking it as private.
The effect is that it grandderived
can access methods derived
, but not base
.
source to share
derived
the class inherits from the base
class privately, which means that all public and protected members of the class base
become private in the derived class.
Although it is private inheritance, it private
can have access to all protected and public members base
. This is how numbers 1 and 2 work.
base::seta
is a protected member in the database, it becomes private in the area derived
. grandderived
can't access private members derived
, so numbers 3 and 4 won't work.
derived::setb
, number 5, works because it is declared protected in the class derived
. Although he becomes private in the area grandderived
, he is accessible from himself. It cannot be inherited further.
source to share
Just as you can access your members private
, but others cannot, you can access base classes private
, but others cannot. This is why the class derived
can access base::seta
, but the class gradderived
cannot.
The main effect of private
inheritance is that objects of a derived type cannot be cast (outside the class) to the type of the base class.
source to share