C + + Inheritance: Public, private, protected

Source: Internet
Author: User

Public, private, and protected inheritance (protected) are three common ways to inherit.

1. Public inheritance

Public inheritance is characterized by the public and protected members of the base class as members of the derived class, which remain in their original state, while the private members of the base class are still private and cannot be accessed by subclasses of the derived class.

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.

Public
Protected
Private

Common inheritance
Public
Protected
Not visible

Private inheritance
Private
Private
Not visible

Protect inheritance
Protected
Protected
Not visible

in: 1) A base class member is a member of a derived class: the members that are shared and protected 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 of the base class becomes in the derived class. For example, when a private inheritance occurs, both common and private members of the base class become private members of the derived class, so that 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 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 protection of inheritance methods

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.

Take A look at this example :

#include <iostream>using namespace std;//////////////////////////////////////////////////////////////////// Class A//Parent class {Private:int Privatedatea;protected:int protecteddatea;public:int publicdatea;};/ Class B:p ublic A//base class A derived class B (common inheritance) {public        : void Funct () {int B;   B=privatedatea; Error: A private member of a base class is not visible in a derived class B=protecteddatea;    OK: The protected member of the base class is b=publicdatea for the protected member in the derived class; OK: The public member of the base class}};//////////////////////////////////////////////////////////////////////////class C for the public member in the derived class:        Derived class C of private A//base class A (private inheritance) {public:void funct () {int C;    C=privatedatea;  Error: A private member of a base class is not visible in a derived class C=protecteddatea;     OK: The protected member of the base class C=publicdatea for the private member in the derived class; OK: The public member of the base class}};//////////////////////////////////////////////////////////////////////////class D for the private member in the derived class: protected A//base class A derived class D (Protection inheritance) {public:void funct () {inT D;   D=privatedatea; Error: A private member of a base class is not visible in a derived class D=protecteddatea;    OK: The protected 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: A private member in a base class is invisible in a derived class and is not visible to an object A=objb.protecteddatea;    Error: The protected member of the base class is a protected member in the derived class and is not visible to the object A=objb.publicdatea;    OK: The public member of the base class is a public member in the derived class, and C OBJC is visible to the object;   A=objc.privatedatea; Error: A private member in a base class is invisible in a derived class and is not visible to an object A=objc.protecteddatea;    Error: The protected member of the base class is a private member in the derived class and is not visible to the object A=objc.publicdatea;    Error: The public member of the base class is a private member in the derived class and is not visible to the object D OBJD;   A=objd.privatedatea; Error: A private member in a base class is invisible in a derived class and is not visible to an object A=objd.protecteddatea;    Error: The protected member of the base class is a protected member in the derived class and is not visible to the object A=objd.publicdatea; Error: The public member of the base class is a protected member in the derived class, and return 0 is not visible to the object;}

C + + inheritance: Public, private, protected

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.