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?