Any class type (whether declared with class-keyclass or struct) 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.

The list of base classes is provided in the base-clause of the class declaration syntax. The base-clause consists of the character : followed by a comma-separated list of one or more base-specifiers.

The classes listed in the base-clause 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 implementation-defined offset. Empty base classes usually do not increase the size of the derived object due to empty base optimization. The constructors of base class subobjects are called by the constructor of the derived class: arguments may be provided to those constructors in the member initializer list.

Contents

Virtual base classes

For each distinct base class that is specified virtual, 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 virtual every time).

struct B {int n;};class X :publicvirtual B {};class Y :virtualpublic B {};class Z :public B {};// every object of type AA has one X, one Y, one Z, and two B's:// one that is the base of Z and one that is shared by X and Ystruct AA : X, Y, Z {void f(){
X::n=1;// modifies the virtual B subobject's member
Y::n=2;// modifies the same virtual B subobject's member
Z::n=3;// modifies the non-virtual B subobject's memberstd::cout<< X::n<< Y::n<< Z::n<<'\n';// prints 223}};