So if I have a following class Super:
class Super {
public:
string member = "bla bla";
void doSth() { cout << member; }
};
And a class Sub that inherits Super:
class Sub : Super {
public:
string member2 = "bla bla 2";
};
Than, when I have a Sub object, I can't reach members of Super even thought they're public.
using namespace std;
int main(){
Sub sub;
cout << sub.member2 << endl;
cout << sub.member << endl; // error: public Super::member is inaccessible
sub.doSth(); // error: public Super::doSth() is inaccessible
}
But why if they're public? Or am I doing something wrong?
You are inheriting from
Superprivately. If you do not mention the access level for inheritance, that is the default forclasses in C++. However, note thatstructs have the default set topublic.Change your code to
And then
memberwill be visible