C + + publicly (public) inheritance, proprietary (private) inheritance, protection (protected) inheritance __c++

Source: Internet
Author: User

Public and private inheritance (private), protected inheritance (protected) are three commonly used methods of inheritance.
1. Public inheritance
Public inheritance is characterized by public members of the base class and protected members as members of derived classes, and they all remain in their original state, while the private members of the base class are still private and cannot be accessed by subclasses of this derived class.
2. Private 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)
Protection 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 that the private members of the base class are still private.

In the illustration 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
(1) The visibility of the base class member to its object:
Public members are visible, 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 a base class member to a 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
(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 a base class member to a 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
This inheritance is the same as the private inheritance method. 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 visibility described above is accessibility.
There is another way of saying 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; }

Reprint Link http://www.cnblogs.com/likebeta/archive/2011/10/12/2209044.html

Reference links http://www.jb51.net/article/54224.htm

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.