Single Inheritance Issues

Source: Internet
Author: User

A protected member of a class can only be accessed by its own member functions or the member functions of Its Derived classes, but cannot be accessed directly outside the class:

1. Public inheritance)
Public inheritance is characterized by the original state of both the public members of the base class and the protected members as the members of the derived class, while the private members of the base class are still private.

2. Private)
Private inheritance features that the public and protected members of the base class are both private members of the derived class and cannot be accessed by the subclass of the derived class.

3. Protected)
The protection inheritance feature is that all the public and protected members of the base class become the protected members of the derived class and can only be accessed by the member functions or friends of the derived class, the Private Members of the base class are still private.


To further understand the differences between the three inheritance methods in terms of Member visibility, we will discuss them from three different perspectives.

For public inheritance:

(1) Visibility of base class members on their objects:

Public members are visible, while others are invisible. Here, the protection of members is the same as that of private members.

(2) Visibility of base class members on Derived classes:

Public and protected members are visible, while private members are invisible. The members are protected in the same way as public members.

(3) Visibility of base class members on derived class objects:

Public members are visible, while other members are invisible.

Therefore, in public inheritance, the object of the derived class can access the public members in the base class; the member functions of the derived class can access the public members and protect members in the base class. Here, you must distinguish between the object of the derived class and the member functions in the derived class to access the base class.

For private inheritance:

(1) Visibility of base class members on their objects:

Public members are visible, while other members are invisible.

(2) Visibility of base class members on Derived classes:

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

(3) Visibility of base class members on derived class objects:

All members are invisible.

Therefore, in private inheritance, the base class members can only be accessed by direct Derived classes, but cannot be inherited.

For the protection Inheritance Method:

This inheritance method is the same as the private inheritance method. The difference between the two lies in that they have different visibility on the base class members for the members of the derived class.

The visibility mentioned above is accessibility. There is another saying about accessibility. In this rule, the object of the derived class uses horizontal access to the base class, and the access of the derived class to the base class is vertical access.

The general rules are as follows:

Horizontal access and vertical access are not restricted to public members in the base class during public inheritance;
During private inheritance, horizontal access and vertical access cannot be accessed by public members in the base class;
During inheritance protection, vertical access is the same as public inheritance, and horizontal access is the same as private inheritance.

Private Members in the base class can only be accessed by member functions and friends functions in the base class, and cannot be accessed 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.