C ++ basics 2

Source: Internet
Author: User

Derived classes and inheritance

This protects a Member from being accessed by the member functions of the derived class, but is hidden from the outside and cannot be accessed by external functions.

 

When creating a derived class object, first execute the base class constructor, and then execute the constructor of the derived class. When revoking the object of the derived class, first execute the destructor of the derived class, then execute the destructor of the base class.

 

When the base class contains constructors with parameters, the derived class must define constructors.

If the base class of a derived class is also a derived class, each derived class only needs to be responsible for its direct construction of the base class, which is traced in turn.

 

Virtual base class

Solve the Problem of ambiguity. If a derived class is derived from multiple base classes and these base classes have a common base class, when this derived class accesses members of this common base class, this vulnerability may cause ambiguity.

Class base1: virtual public base {}

If the same level contains both the virtual and non-virtual base classes, you should first call the constructor of the virtual base class before calling the constructor of the non-virtual base class, finally, call the constructor of the derived class.

Class X: Public y, virtual public Z {}

 

 

 

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.