Suppose I have a base and derived class:
class Base
{
public:
virtual void Do();
}
class Derived:Base
{
public:
virtual void Do();
}
int main()
{
Derived sth;
sth.Do(); // calls Derived::Do OK
sth.Base::Do(); // ERROR; not calls Based::Do
}
as seen I wish to access Base::Do through Derived. I get a compile error as “class Base in inaccessible” however when I declare Derive as
class Derived: public Base
it works ok.
I have read default inheritance access is public, then why I need to explicitly declare public inheritance here?
You might have read something incomplete or misleading. To quote Bjarne Stroustrup from “The C++ programming Language”, fourth Ed., p. 602:
This also holds for members inherited without access level specifier.
A widespread convention is, to use struct only for organization of pure data members. You correctly used a
classto model and implement object behaviour.