First, subclasses can inherit only public and protected members of the parent class.
Public inheritance: The public and protected members of the base class are public and protected members of the derived class.
Private inheritance: Both the public and protected members of the base class are private members of the derived class. Protection inheritance: Both the public and the protected members of the base class are protected members of the derived class. * In a subclass, a private member of the parent class can be obtained through the public function of the parent class (as long as the public function of the parent class is not overridden in the subclass) for virtual inheritance. (Virtual is used for only two places: one is a virtual function and the other is a virtual inheritance)
class a{ char k[3 ]; public : virtual void AA () {};}; class B: public virtual a{ char j[ 3 ]; public : virtual void BB () {};};
The Virtual keyword virtual high-speed compiler does not bind in advance and can be accessed at run time. The compiler creates a table (vtable) for each class that contains virtual functions, and sets a pointer (vfptr) to point to it. So the size of a is char k[3] aligned to the 4 bytes of the 4 byte +vfptr, the 8B virtual inherits a, then B has a virtual class pointer (Vbptr_b_a) to its parent class, and then also contains all the contents of the parent class. So the size of B is j[3] aligned 4 bytes + vtable of the virtual pointer to B vfptr the size of the 4-byte +a (8 bytes), which is 16. Virtual inheritance is applied to multiple inheritance, such as: B,c inherit a,d inherit from B and C, then common inheritance is: a a \/B C \/d in Class D in a repetition, waste memory space, after the use of virtual inheritance: A/\ b C \/D
C + + Inheritance