C++ – “a struct has public inheritance by default”

c++, inheritance

"a struct has public inheritance by default" what does this statement really mean? And why is the following code in error just because I have omitted the keyword 'public' while deriving the class d from c??

struct c {protected:    int i;public:    c(int ii=0):i(ii){}     virtual c *fun();};c* c::fun(){    cout<<"in c";    return &c();}class d : c{  public:    d(){}    d* fun()    {        i = 9;        cout<<"in d"<<'\t'<<i;        return &d();    }};int main(){    c *cc;    d dd;    cc = &dd;    cc->fun();}

Best Solution

It means that

struct c;struct d : c

is equivalent to

struct d : public c

Your code is a class extending a struct:

struct c;class d : c;

is equivalent to

class d : private c;

because class has private inheritance by default.

And it means that all inherited and not overriden/overloaded/hidden methods from c are private in d.