C + + public inheritance, protection inheritance, private inheritance Difference __c++

Source: Internet
Author: User

1 The visibility of the base class member to the derived class


1. Communal inheritance (public): When a public member of a base class and a protected member are members of a derived class, they remain in their original state, and the private members of the base class are still private and cannot be accessed by subclasses of this derived class.


2. Private inheritance: The public and protected members of the base class are both private members of the derived class and cannot be accessed by subclasses of this derived class.


3. Protection Inheritance (Protected): All public and protected members of the base class are protected members of the derived class and can only be accessed by its derived class member functions or friends, and the private members of the base class are still private.


The following lists the base class attributes and derived class attributes for three different inheritance methods.

Table One: The visibility of a base class member to a derived class

Public protected private

Public Inheritance visible Invisible

Private Inheritance visible Invisible

Protect inheritance visible visible not visible


2 The visibility of the base class member to the derived class object

Table Two: The visibility of base class members to derived class objects

                                                        public    protected    &N Bsp;private
                              & nbsp Public inheritance       visible       not visible       not visible             &NBS P                                   &NB Sp                                  &NBS P

Private inheritance cannot be seen or visible

Protected inheritance Invisible invisible


In public inheritance, the objects of the derived class can access public members in the base class, and the member functions of the derived class can access public and protected members in the base class.


In private inheritance, a member of a base class can only be accessed by a direct derived class and cannot be inherited further down.


The aforementioned visibility is accessibility. The object of the derived class is called horizontal access to the base class, and the derived class of the derived class accesses the base class for vertical access.


In the case of public inheritance, horizontal and vertical access is unrestricted for public members in the base class;


When private inheritance, horizontal and vertical access is not accessible to public members in the base class;


When you protect inheritance, for vertical access same as public inheritance, for horizontal access to private inheritance.


A private member in a base class can only be accessed by member functions and friend functions in the base class, and not by other functions.


Public, private, and protected inheritance can access the protection variables of the parent class through the function, but not the private variables of the parent class.




Related Article

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.