Default Inheritance Protection Levels
In §7.2 (p. 268) we saw that classes defined with the struct
and class
keywords have different default access specifiers. Similarly, the default derivation specifier depends on which keyword is used to define a derived class. By default, a derived class defined with the class
keyword has private
inheritance; a derived class defined with struct
has public
inheritance:
class Base { /* ... */ };struct D1 : Base { /* ... */ }; // public inheritance by defaultclass D2 : Base { /* ... */ }; // private inheritance by default
It is a common misconception to think that there are deeper differences between classes defined using the struct
keyword and those defined using class
. The only differences are the ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.