C + + Inheritance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.