C ++: private and public inheritance

Source: Internet
Author: User
Tags types of functions

1. Access range of private, public, and protected access labels
Private: It can only be accessed by 1. functions in this class, 2. Other functions.
Cannot be accessed by any other user, nor can the object of this class be accessed.

Protected: it can be accessed by 1. functions in this class, 2. Sub-class functions, and 3. Its membership functions.
But cannot be accessed by objects of this class.

Public: it can be accessed by 1. functions in this class, 2. Sub-class functions, 3. Its friends functions, or 4. objects in this class.

Note: There are three types of functions: normal non-member functions set as friends, member functions of other classes set as friends, and all member functions in the friends class.


Class 2 method property changes after inheritance
Private attributes cannot be inherited.
Using private inheritance, the protected and public attributes of the parent class are changed to private in the subclass;
Using protected inheritance, the protected and public attributes of the parent class are changed to protected in the subclass;
Use public inheritance. The protected and public attributes of the parent class are not changed;


3 applicable situations of private inheritance and public inheritance
C ++ regards public inheritance as a is-a relationship. Private inheritance does not mean the is-a relationship. private inheritance means implemented-in-terms-of (implemented based on something ). Private inheritance means that only the implementation part is inherited and the interface part is omitted. Private inheritance has no significance in software design. Its significance lies only in software implementation.

Private inheritance:

1) the compiler will not automatically convert a subclass object to a parent class object, while the public inheritance will;

2) All the members (protected and public) inherited from the parent class in the subclass become private.

Implemented-in-terms-of can also be implemented by combination. In the application domain, combination means has-a; in the implementation domain, combination means is-implemented-in-terms-. Try to use composite to implement this relationship. private inheritance is used only when necessary (when it involves protected members or virtual functions.


 

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.