C + + Inheritance: public, private, protection __c++

Source: Internet
Author: User
Http://www.cnblogs.com/qlwy/archive/2011/08/25/2153584.html

Common inheritance (public), private inheritance (private), and protection Inheritance (protected) are three inherited methods that are commonly used.

1. Communal Inheritance (public)

Public inheritance is characterized by the presence of public members of the base class and the protection of members as members of derived classes, while the private members of the base class remain private and cannot be accessed by subclasses of this derived class.

2. Personal Inheritance (private)

Private inheritance is characterized by that both the public and the protected members of the base class are private members of the derived class and cannot be accessed by subclasses of this derived class.

3. Protection Inheritance (protected)

The characteristic of protecting inheritance is that 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.

Public Protected Private
Total inheritance Public Protected Not visible
Private inheritance Private Private Not visible
Protect inheritance Protected Protected Not visible

In the figure above: 1 The base class member is to the derived class: shared and protected members are visible, and private members are invisible.

2 The base class member is to the object of the derived class: To see what type of member the base class's members have become in the derived class. For example, when private inheritance, both the common and private members of the base class become private members of the derived class, so that the common members and private members of the base class are invisible to the objects in the derived class.

To further understand the differences in the visibility of three different ways of inheritance in their members, the following are discussed from three different angles. for public inheritance methods

(1) The visibility of the base class member to its object:

Public members are visible and others are not visible. This protects the members with the private members.

(2) The visibility of the base class member to the derived class:

Public and protected members are visible, while private members are not visible. This protects the members with the public members.

(3) The visibility of the base class member to the derived class object:

Public members are visible and other members are not visible.

therefore, in the case of 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. Here, be sure to distinguish between the object of the derived class and the member function in the derived class that is different from the access to the base class. for private inheritance methods

(1) The visibility of the base class member to its object:

Public members are visible and other members are not visible.

(2) The visibility of the base class member to the derived class:

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

(3) The visibility of the base class member to the derived class object:

All members are not visible.

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

This type of inheritance is the same as the private inheritance approach. The difference between the two is that there is a different visibility to the base class members for the members of the derived class.

The aforementioned visibility is accessibility.

There is another argument about accessibility. In this rule, 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.

Take A look at this example :

#include <iostream> using namespace std; Class A//Parent class {Private:int Privateda
Tea;
Protected:int Protecteddatea;
Public:int Publicdatea;
}; Class B:p ublic A//base class A derived class B (shared inheritance) {Publi
        C:void funct () {int B;   B=privatedatea; Error: Private members in the base class are invisible b=protecteddatea in derived classes;    OK: the protection member of the base class is b=publicdatea for the protected member in the derived class;
OK: The public members of the base class are public members in the derived class}};
Class C:p rivate A//base class A derived class C (private inheritance) {
        Public:void funct () {int C;    C=privatedatea;  Error: Private members in the base class are invisible c=protecteddatea in derived classes;     OK: the protection members of the base class are c=publicdatea for private members in derived classes;
OK: The public members of the base class are private members in the derived class}}; Class D:p rotected A//base class A derived class D (Protection inheritance) {
Public:void funct () {int D;   D=privatedatea; Error: Private members in the base class are invisible d=protecteddatea in derived classes;    OK: the protection member of the base class is d=publicdatea for the protected member in the derived class;
OK: The public members of the base class are protected members} in the derived class; 
 
    int main () {int A;
    B OBJB;   A=objb.privatedatea; Error: Private members in the base class are not visible in the derived class and are invisible to the object A=objb.protecteddatea;    Error: The protection member of the base class is a protected member in a derived class and is not visible to the object A=objb.publicdatea;
    OK: The public members of the base class are public members in the derived class, and C OBJC is visible to the object;   A=objc.privatedatea; Error: Private members in the base class are not visible in the derived class and are invisible to the object A=objc.protecteddatea;    Error: The protection member of the base class is a private member in a derived class and is not visible to the object A=objc.publicdatea;
    Error: The public members of the base class are private members in the derived class, and D OBJD is not visible to the object;   A=objd.privatedatea; Error: Private members in the base class are not visible in the derived class and are invisible to the object A=objd.protecteddatea;    Error: The protection member of the base class is a protected member in a derived class and is not visible to the object A=objd.publicdatea;
Error: The public members of the base class are protected members in the derived class, and return 0 is not visible to the object; }


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.