C + + inheritance in detail: common (public) inheritance, private inheritance, protection (protected) inheritance

Source: Internet
Author: User

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

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 of inheritance

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.

base class characteristics and derived classes of three different inheritance methods

Pulic Protected Private
Public inheritance Pulic proctected Not visible
Private inheritance Private Private Not visible
Protect inheritance Protected Protected

Not visible

In the:

1. A base class member is a member of a derived class: Common and protected members are visible and private members are invisible.

2. The base class member is for 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, the public and private members of the base class become private members of the derived class, so that the public 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 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 to a derived class: Public members 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 base class members to derived class objects: Public members are visible and other members are not visible.

Therefore: In the case of public inheritance, the object of the derived class can access the public member in the base class, and the member functions of the derived class can access the public and protected members of 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. Visibility of base class members to their objects: Public members are visible, other members are not visible

2. The base class member functions are visible to derived classes: Public members and protected members are visible, and private members are invisible.

3. The visibility of a base class member on a derived class object: All members are not visible.

Therefore: in private inheritance, a member of a base class can only be accessed by a direct derived class and cannot inherit down.

For protection of inheritance methods

This inherits in the same way as private inheritance. The difference between the two is only for the members of the derived class, and for the base class members have different visibility. (visibility 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.

1#include <iostream>2 classa{3 Private:4     intPrivatedataa;5 protected:6     intProtecteddataa;7  Public:8     intPublicdataa;9 };Ten //derived class B of base Class A (public inheritance) One classD | Publica{ A  -  Public: -     voidFunca () the     { -         intb; -b =Privatedataa; -         //Error: Private members in base class are not visible in derived classes +b =Protecteddataa; -         //correct: A protected member of a base class is a protected member in a derived class +b =Publicdataa; A         //correct: A public member of a base class is a public member in a derived class at     } - }; - //derived class C of base class A private inheritance - classE |Privatea{ -  -  Public: in     voidFunca () -     { to         intC; +c =Privatedataa; -         //Error: Private members in base class are not visible in derived classes thec =Protecteddataa; *         //correct: A protected member of a base class is a private member in a derived class $c =Publicdataa;Panax Notoginseng         //correct: A public member of a base class is a private member in a derived class -     } the }; + //derived Class D of base Class A protects inheritance A classB |protecteda{ the  Public: +     voidFunca () -     { $         intD; $D =Privatedataa; -         //Error: Private members in base class are not visible in derived classes -D =Protecteddataa; the         //correct: A protected member of a base class is a protected member in a derived class -D =Publicdataa;Wuyi         //correct: A public member of a base class is a protected member in a derived class the     } - }; Wu voidMain () - { About     intvalue; $ B OBJB; -value = Objb.privatedataa;//Error: The private member of the base class is not visible in the derived class and is not visible to the object -value = Objb.protecteddataa;//Error: The protected member of the base class is a protected member in a derived class and is not visible to the object -value = Objb.publicdataa;//Error: The public member of the base class is a public member in the derived class, visible to the object A  + C OBJC; thevalue = Objc.privatedataa;//Error: The private member of the base class is not visible in the derived class and is not visible to the object -value = Objc.protecteddataa;//Error: The protected member of the base class is a private member in the derived class and is not visible to the object $value = Objc.publicdataa;//Error: The public member of the base class is a private member in the derived class and is not visible to the object the  the D OBJD; thevalue = Objd.privatedataa;//Error: The private member of the base class is not visible in the derived class and is not visible to the object thevalue = Objd.protecteddataa;//Error: The protected member of the base class is a protected member in a derived class and is not visible to the object -value = Objd.publicdataa;//Error: The public member of the base class is a protected member in a derived class and is not visible to the object inSystem"Pause"); the}

C + + inheritance in detail: common (public) inheritance, private inheritance, protection (protected) inheritance

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.