Public, private, and protected permissions of C ++

Source: Internet
Author: User

Because the Package concept does not exist, the protected of C ++ is different from that of protected in Java. protected in Java is not only accessible to sub-objects, but also to other parts of the package, it can also be displayed through sub-object calls, such

Package pack1

Public Class {

Protected int;

}

Package pack1

Public Class B {

Public static void main (string [] ARGs ){

System. Out. println ();

}

}

The above code is correct, but if B and A are not in the same package, they cannot be accessed. Therefore, in Java, the visibility of protected is similar to that of packaged, but in inheritance, it does not define packets, the packaged permission does not allow the sub-object to access the attributes of the package access permission of the parent object in other packages.

In C ++, the protected attribute is strictly defined, that is, it only has the vertical access permission, and can only be accessed by itself and its descendants. Only the protected attribute of a class is limited, only internal and sub-objects can be accessed, but they are invisible in other places. The following table lists the access to attributes inherited by C ++ (narrow access ):

Public attribute protected attribute private attribute

Public inherits from public protected and is invisible.

Protected inherits protected which is invisible

Private inherits Private invisible

 

Http://www.cnblogs.com/cucmehp/archive/2008/10/28/1321106.html ()

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.