cpp/language/derived class

Any class type (whether declared with  or ) may be declared as derived from one or more base classes which, in turn, may be derived from their own base classes, forming an inheritance hierarchy.

Syntax
The list of base classes is provided in the of the. The consists of the character  followed by a comma-separated list of one or more s.

@1@ Specifies a non-virtual inheritance with default member accessibility. @2@ Specifies a virtual inheritance with default member accessibility. @3@ Specifies a non-virtual inheritance with given member accessibility. @4@ Specifies a virtual inheritance with given member accessibility. @5@ Same as 4), and  can appear in any order.

An cannot directly appear as  due to syntax limitations.

If is omitted, it defaults to  for classes declared with   and to  for classes declared with.

Classes denoted by 's listed in the are direct base classes. Their bases are indirect base classes. The same class cannot be specified as a direct base class more than once, but the same class can be both direct and indirect base class.

Each direct and indirect base class is present, as base class subobject, within the object representation of the derived class at an ABI-dependent offset. Empty base classes usually do not increase the size of the derived object due to. The constructors of base class subobjects are called by the constructor of the derived class: arguments may be provided to those constructors in the.

Virtual base classes
For each distinct base class that is specified, the most derived object contains only one base class subobject of that type, even if the class appears many times in the inheritance hierarchy (as long as it is inherited every time).

An example of an inheritance hierarchy with virtual base classes is the iostreams hierarchy of the standard library: std and std are derived from std using virtual inheritance. std is derived from both std and std, so every instance of std contains a std subobject, a std subobject, and just one std subobject (and, consequently, one std).

All virtual base subobjects are initialized before any non-virtual base subobject, so only the most derived class calls the constructors of the virtual bases in its :

There are for unqualified name lookup for class members when virtual inheritance is involved (sometimes referred to as the rules of dominance).

Public inheritance
When a class uses  to derive from a base, all public members of the base class are accessible as public members of the derived class and all protected members of the base class are accessible as protected members of the derived class (private members of the base are never accessible unless friended).

Public inheritance models the subtyping relationship of object-oriented programming: the derived class object IS-A base class object. References and pointers to a derived object are expected to be usable by any code that expects references or pointers to any of its public bases (see ) or, in terms, a derived class should maintain class invariants of its public bases, should not strengthen any precondition or weaken any postcondition of a member function it.

Protected inheritance
When a class uses  to derive from a base, all public and protected members of the base class are accessible as protected members of the derived class (private members of the base are never accessible unless friended).

Protected inheritance may be used for "controlled polymorphism": within the members of Derived, as well as within the members of all further-derived classes, the derived class IS-A base: references and pointers to Derived may be used where references and pointers to Base are expected.

Private inheritance
When a class uses  to derive from a base, all public and protected members of the base class are accessible as private members of the derived class (private members of the base are never accessible unless friended).

Private inheritance is commonly used in policy-based design, since policies are usually empty classes, and using them as bases both enables static polymorphism and leverages.

Private inheritance can also be used to implement the composition relationship (the base class subobject is an implementation detail of the derived class object). Using a member offers better encapsulation and is generally preferred unless the derived class requires access to protected members (including constructors) of the base, needs to override a virtual member of the base, needs the base to be constructed before and destructed after some other base subobject, needs to share a virtual base or needs to control the construction of a virtual base. Use of members to implement composition is also not applicable in the case of multiple inheritance from a or when the identities of the base classes are determined at compile time through template metaprogramming.

Similar to protected inheritance, private inheritance may also be used for controlled polymorphism: within the members of the derived (but not within further-derived classes), derived IS-A base.

Member name lookup
Unqualified and qualified name lookup rules for class members are detailed in.