C + + public inheritance, the difference between private inheritance and protection inheritance

Source: Internet
Author: User

Yesterday we learned three ways of inheriting, some of which are very image and share them.

First, describe a few terms:

1. Base class

The base analogy from which the inheriting class is a more abstract concept, is described in a larger scope. So you can see some abstract classes that are designed to exist as base classes (some names have abstract in them).

The base class is also called the parent class, although I think this analogy is inappropriate. Because the subclass is actually an extension of the base class, it describes something more specific. But the real father-son relationship is not so. But in some ways the metaphor is more appropriate.

Now let's assume that the base class is a wealthy who is retiring soon. The code is as follows:

class richman{public:    RichMan ();     ~RichMan ();     int M_company;     Private :     int M_money;     int M_car; protected :     int m_house;    };

Company is public, then he (base class), Venture partner (friend), son (subclass), other people (external) can access.

Money and car are private, their (base class), Venture Partners (friends) can access. Neither the son nor the outsider is open.

The house is protected, own (base class), Venture partner (friend) can access, son (subclass) can also access, outsiders are not accessible.

Now that he's retiring, there are three ways to inherit:

1. Public Inheritance:

His son inherited him: the Littlerichman class

class Littlerichman:      Public richman{public:    Littlerichman ();     ~Littlerichman ();};

After public inheritance, the Littlerichman member access rights become:

" RichMan.h " class Littlerichman:      Public richman{public:    Littlerichman ();     ~Littlerichman ();     int M_company; // As if protected :     int m_house; // As if };

That is, the public member remains unchanged, the private member is not visible, and the protected member remains unchanged.

His son continued to drive the company and live in the house, but his father's car and money were out of the bag.

Continue, Private inheritance:

class Littlerichman:     Private richman{public:    Littlerichman ();     ~Littlerichman (); Private :     int M_company; // As if    int m_house; // As if };

It can be imagined that his son is a selfish person, inherited through the private way: public and protected became private.

Own occupy the company and the house, so in addition to oneself and friend Yuan, in any way cannot visit.

Finally, protected inherits:

class Littlerichman:     Private richman{public:    Littlerichman ();     ~Littlerichman (); protected :     int M_company; // As if    int m_house; // As if };

The original public became protected,protected and remained the same. In this way, the company and the House are not accessible to outsiders, themselves, friends, sub-categories can be accessed.

C + + public inheritance, the difference between private inheritance and protection 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.