Inheritance (refers to the mechanism of sharing properties and operations between classes)

Source: Internet
Author: User
Tags access properties

Inheritance ( refers to the mechanism of sharing properties and operations between classes)

Virtual base class is inherited by adding the keyword virtual to avoid ambiguity.

{

Self-Understanding:

Public: The public and protected members of the base class are public and protected members of the derived class, and in the derived class class the base class can be publicly and protected, and the base class private is inaccessible. Within the main () function, the derived class object can access the base class members. Although a private member of a base class is not accessed by a derived class, the derived class is also a private data member that contains the base class.

Protection: The public and protected members of the base class are protected members of the derived class, and the base class can be protected in the derived class class, and the base class private is inaccessible. Within the main () function, the derived class object cannot access any members of the base class, and can access the base class indirectly by accessing the derived class member function.

Private: base class public and protected as private members of derived classes, the base class can be protected in a derived class class, and the base class private is inaccessible. Within the main () function, the derived class object cannot access any members of the base class, and can access the base class indirectly by accessing the derived class member function.

Access rules for members

Access modifiers access external objects from derived classes from their class access

Public Yes Yes Yes

Protected is whether

Private is no

Detailed Explanation:

The default is private inheritance

In public inheritance, the public and protected members of the base class maintain the original access properties in the derived class, and their private members remain private to the base class.

A private member of a base class cannot be accessed directly by a derived class, regardless of inheritance, and can be accessed through a friend function

A base class object can be assigned to a derived class object under certain circumstances.

In the case of private inheritance, a newly defined member function in a derived class can access the protected members of the base class

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.

How to Inherit

Base class Properties

Derived class attributes

Public inheritance

Public

Public

Protected
Private

Protected
Not accessible

Private inheritance

Public

Private

Protected
Private

Private
Not accessible

Protect inheritance

Public

Protected

Protected
Private

Protected
Not accessible

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.

Inheritance (refers to the mechanism of sharing properties and operations between classes)

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.