Default class inheritance access
Solution 1:
From standard docs, 11.2.2
In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class.
So, for struct
s the default is public
and for class
es, the default is private
...
Examples from the standard docs itself,
class D3 : B { / ... / }; // B private by default
struct D6 : B { / ... / }; // B public by default
Solution 2:
You might have read something incomplete or misleading. To quote Bjarne Stroustrup from "The C++ programming Language", fourth Ed., p. 602:
In a
class
, members are by defaultprivate
; in astruct
, members are by defaultpublic
(§16.2.4).
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 class
to model and implement object behaviour.
Solution 3:
The default inheritance level (in absence of an access-specifier for a base class )for class
in C++ is private. [For struct
it is public]
class Derived:Base
Base
is privately inherited so you cannot do sth.Base::Do();
inside main()
because Base::Do()
is private inside Derived
Solution 4:
The default type of the inheritance is private. In your code,
class B:A
is nothing but
class B: private A