I’m attempting a simple example of inheritance in C++. But I just can’t get it down. When I try to get the protected members of class B inherited from class A it says that A::baz is protected.
#include <iostream>
class A {
public:
int foo;
int bar;
protected:
int baz;
int buzz;
private:
int privfoo;
int privbar;
};
class B : protected A {}; // protected members go to class B, right?
int main() {
B b;
b.baz; // here is the error [A::baz is protected]
}
I can’t seem to find what I’m doing wrong. I’ve tried changing class B : protected A to : public A but it still doesn’t work.
Protected inheritance just impacts how clients of your class see the public interface of the base class. Protected inheritance marks the public members of the base class as protected for users of your inherited class.
So
bazin your example is not public, it is protected from B, hence the compiler error.