C + + Class interview control and inheritance

Source: Internet
Author: User

There are three types of access control for C + + classes: public,protected and private.

Class access control What objects a class member can be interviewed by
Public 1. The member function of the class. 2, Class object. 3, friend Yuan. 4. Sub-class member functions
Protected 1. The member function of the class. 2, friend Yuan. 3. Sub-class member functions
Private 1. The member function of the class. 2, friend Yuan



Ii. Inheritance of C + + classes

There are also three ways of inheriting: public,protected and private.

After inheritance, the accessibility of base class members changes, such as the following table:


Public inheritance

Base class Public Protected Private
Sub-class Public Protected Not visible


Protected inheritance

Base class Public Protected Private
Sub-class Protected Protected Not visible


Private inheritance

Base class Public Protected Private
Sub-class Private Private Not visible

It can be seen from the above that, after inheritance, the access of the base class members is changed, and the accessibility of the derived class is still subject to the class's control rules. About three types of inheritance, derived classes to the base class members of the interview, there are very large online netizens wrote a large number of articles, but I think the text is very easy to describe the person around the halo, but lost its essence. I thought the inheritance is not so troublesome as the Netizen says, that is, the accessibility of the base class has changed in the derived class, and then the derived class can then use the Class access control rule to interview it.


There are two notable places to note:

The private members in the base class are not visible in the derived class, that is, they cannot be interviewed by sending their own member functions, but can be visited through the inherited member functions.

Second, private inheritance, both the public and protected members of the base class become private members of the derived class, so that if a subclass is derived from the derived class, then, in the derived subclass, it is not possible to access the inherited member directly through the member function of the subclass;



Third, which members of a derived class cannot inherit from a base class

There are three members of a derived class that cannot inherit from a base class:

1. Constructors and destructors

2. Assignment operator (operator= () member)

3, friend Yuan


C + + Class interview control and 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.