Public,private,protected Comparison of classes in C + +

Source: Internet
Author: User

When Private,public,protected is simply set as a member permission in a class:
Private: can only be accessed by a function in the class, its friend function, and cannot be accessed by any other object of the class.


Protected: can be accessed by functions in the class, by functions of subclasses, and by their friend functions, but cannot be an object of this classAccess
Public: can be accessed by a function in the class, a function of a subclass, its friend function, or by an object of that class. Note: The friend function consists of two kinds: global function set as friend and set as member function in friend classWhen private,public,protected is inherited as a method:  for the public inheritance method:
(1) The public member of the parent class becomes the public member of the subclass, allowing code outside the class to access those members ;
(2) The private member of the parent class is still the private member of the parent class, and the members of the subclass cannot access the member;
(3) The protected member of the parent class becomes the protected member of the subclass, and only the members of the subclass are allowed access;
(1) The members of the base class Objectthe visibility of:

Public members are visible, others are not visible. Here the protection members are identical to the private members.

(2) The base class member Derived Classesthe visibility of:

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 base class member derived class objectthe visibility of:

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 public member of the parent class becomes the private member of the Child class,only subclass members are allowed access;
(2) The private member of the parent class is still the private member of the parent class, and the members of the subclass cannot access the member;
(3) The protected member of the parent class becomes the private member of the subclass, and only the child class members are allowed access;

(1) The members of the base class Objectthe visibility of:

public members are visible and other members are not visible.

(2) The base class member Derived Classesthe visibility of:

public and protected members are visible, and private members are invisible.

(3) The base class member Derived ClassesObjectthe visibility of:

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 the Protection inheritance method:

(1) The public member of the parent class becomes the protected member of the child class,only subclass members are allowed access;
(2) The private member of the parent class is still the private member of the parent class, and the members of the subclass cannot access the member;
(3) The public member of the parent class becomes the protected member of the subclass, and only the child class members are allowed access;

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.

The general rules are as follows:

in the case of public inheritance, horizontal access and vertical access are not restricted to the public members in the base class;
When private inheritance occurs, both horizontal and vertical access to the public members of the base class are not accessible;
protection inheritance, same as public inheritance for vertical access, same as private inheritance for horizontal access

Public,private,protected Comparison of classes in C + +

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.