Public inheritance, private inheritance, and protection inheritance

Source: Internet
Author: User
Tags inheritance visibility
1. Public inheritance
Public inheritance is characterized by the public and protected members of the base class as members of derived classes, all of which remain in their original state, while private members of the base class remain private.

2. Private inheritance
Private inheritance is characterized by the public and protected members of the base class as private members of the derived class and cannot be accessed by subclasses of the derived class.

3. Protection Inheritance (protected)
The feature of protection inheritance is that all public and protected members of the base class become 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 is a list of the base class attributes and derived class attributes of three different inheritance methods.

base classes and derived class attributes for different inheritance methods
—————————————————————————————————
How to Inherit |   Base class Properties | Derived class attributes
—————————————————————————————————
|   Public | Public
Public Inheritance |   protected | Protected
|   Private | Not accessible
—————————————————————————————————
|   Public | Private
Private Inheritance |   protected | Private
|   Private | Not accessible
—————————————————————————————————
|   Public | Protected
Protection Inheritance |   protected | Protected
|   Private | Not accessible
—————————————————————————————————

To further understand the differences in the visibility of the three different types of inheritance in their members, the following is discussed from three different perspectives.

For the public inheritance method:

(1) The visibility of the base class members to their objects:

Public members are visible, others are not visible. Here the protection members are identical to the private members.

(2) The visibility of a base class member on a derived class:

Public and protected members are visible, and private members are not visible. The protection of Members here is the same as public members.

(3) The visibility of a base class member on a derived class object:

Public members are visible and other members are not visible.

Therefore, when the public inherits, the objects of the derived class can access the public members in the base class, and the member functions of the derived class can access the public and protected members in the base class. Here, it is important to distinguish between objects of derived classes and member functions in derived classes that have different access to the base class.

For private inheritance methods:

(1) The visibility of the base class members to their objects:

Public members are visible and other members are not visible.

(2) The visibility of a base class member on a derived class:

Public and protected members are visible, and private members are invisible.

(3) The visibility of a base class member on a derived class object:

All the members are invisible.

Therefore, in private inheritance, members of the base class can only be accessed by directly derived classes, and cannot be inherited further down.

For the protection inheritance method:

This inherits in the same way as private inheritance. The difference between the two is that the members of the derived class have different visibility to the base class members.

The visibility referred to above is also accessibility. There is another way of saying about accessibility. In this rule, objects that are called derived classes are accessed horizontally for the base class, which is called a derived class's access to the base class as a vertical access.

The general rules are as follows:

In the case of public inheritance, horizontal access and vertical access are not restricted to the public members in the base class;
When private inheritance occurs, both horizontal and vertical access to the public members of the base class are not accessible;
When protecting inheritance, for vertical access is the same as public inheritance, for horizontal access is the same as private inheritance.

Private members in a base class can only be accessed by member functions and friend functions in the base class, not by other 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.