Three kinds of inheritance public,protected,private in C + + detailed parsing _c language

Source: Internet
Author: User
Tags inheritance

Three kinds of access rights

Public : can be accessed by any entity

protected: only child classes and member functions of this class are allowed to access

Private: only member functions of this class are allowed to access

Three ways of inheriting

Public inheritance

Protect inheritance

Private inheritance

Combining results

In the inheritance method subclass of the base class

Public and public inherit => public

Public & protected Inherits => protected

Public & Private inheritance = > Private


Protected & Public Inherits => protected

Protected & Protected inherited => protected

Protected & Private inheritance = > Private


Private & Public Inheritance => subclass does not have permission to access

Private & Protected Inheritance => Subclass is not authorized to access

Private & Private inheritance = > subclass does not have access to

By the combination of the above results can be seen

1, public inheritance does not change the access rights of base class members

2. Private inheritance makes it private for all members of the base class to access permissions in the subclass

3. Protected inherits the public members of the base class into protected members of subclasses, and the access rights of other members are unchanged.

4. Private members in the base class are unaffected by inheritance, and subclasses are never entitled to access them.

In addition, when using private inheritance, there is another mechanism: grant access.

As we already know, when a base class is inherited in private, its public and protected members become private members in subclasses. In some cases, however, it is necessary to restore one or more inherited members in a subclass to their access rights in the base class.

C + + supports this goal in two ways

method One, using a using statement, which is recommended by the C + + standard

method Two, using the Access declaration, in the form of Base-class::member, where the location is at the appropriate access declaration at the subclass. (Note, you can only restore the original access rights, but not improve or reduce access rights)

Why do you want to define public protect private three access rights in C + +?

Some of the things we need to look out for outside, that is, public, if not, then the work we do will be meaningless, (no one else can use it).

If we don't want people to know the inner implementation details, then it's private, for example:

Copy Code code as follows:

Public
Count ()

{
Mycount ();
}
Private
Mycount ();


So, count is the external interface, and when implemented, we do not want to let outside know how to implement, use private, prevent it with mycount!

If we don't want to let people know, want to let their children know what (here involves inheritance), then can be used as a protected!

In this case, private is privately owned, protected can let the child know, public is open!

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.