Public,protected,private access in C + +

Source: Internet
Author: User
Tags class definition


Summarize:
1. The difference between protected and private is that the access designator limit of the protected can be accessed by the member functions of the derived class, and private cannot
2. The difference between protected and publice is that protected's access restrictions make it inaccessible to objects of the base class.

-----------------------------------------------------
First: Private, public, access to the Protected Access label.
Private: only 1. The function in the class, 2. Its friend function is accessed.
The object of the class cannot be accessed by any other access.

Protected: Can be 1. A function in this class, a function of 2. Subclass, and 3. Its friend function is accessed.
But cannot be accessed by objects of that class.

Public: Can be 1. The function in the class, the function of the 2. Subclass, 3. Its friend function is accessed by 4. The object of the class.

Note: The friend function consists of 3 types: ordinary non-member functions set as friends, member functions of other classes set as friends, and all member functions in the friend class.

Second: The class inherits the method property changes.
The private property cannot be inherited.
With private inheritance, the protected and public properties of the parent class become private in subclasses;
With protected inheritance, the protected and public properties of the parent class become protected in the subclass;
With public inheritance, the protected and public properties in the parent class do not change;

As shown below:
Public:protected:private:
Public inherited public protected not available
Protected inherited protected protected not available
Private private is not available

Protected inheritance and private inheritance can reduce access rights.

To further understand the differences in the visibility of the three different continuation modes in their members, the following is discussed from three different perspectives.
For public continuation mode:
(1) The visibility of the 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 on a derived class:
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 visibility of a base class member on a derived class object:
Public members are visible and other members are not visible.
Therefore, when the public continues, 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 continuation methods:
(1) The visibility of the base class members to their objects:
Public members are visible and other members are not visible.
(2) The visibility of a base class member on a derived class:
Public and protected members are visible, and private members are invisible.
(3) The visibility of a base class member on a derived class object:
All the members are invisible.
Therefore, when private continues, the members of the base class can only be accessed by directly derived classes, and cannot proceed further down.
For protection continue mode:
This continuation is the same as in the case of a private continuation mode. The difference between the two is that the members of the derived class have different visibility to the base class members.
  
  
  
Private members in a base class can only be accessed by member functions and friend functions in the base class, not by other functions.
The relationship between a base class and a derived class
Any class can derive a new class, and derived classes can also derive new classes, so the base and derived classes are relative.
The relationship between a base class and a derived class can be described in the following ways:
1. Derived classes are materialization of base classes
The hierarchy of classes usually reflects some kind of real model in the objective world. In this case, it is not difficult to see that the base class is an abstraction of several derived classes, whereas derived classes are materialized by the base class. The base class extracts the public characteristics of its derived class, and the derived class changes the abstract class to some useful type by increasing the behavior.
2. A derived class is a continuation of a base class definition
An abstract base class is defined first, and some operations in that base class are not implemented. Then define non-abstract derived classes to implement the operations defined in the abstract base class. For example, a virtual function is a condition of this kind. At this point, the derived class is the implementation of the abstract base class and can be seen as a continuation of the base class definition. This is also a common method for derived classes.
3. Derived classes are a combination of base classes
When multiple continuation, a derived class has more than one base class, at which point the derived class will be a combination of all base class behavior.
The method by which a derived class distinguishes itself from a base class is to add data members and member functions. Therefore, the continuation mechanism will make it possible to create a new class by simply stating the difference between the new class and the existing class, so that a large number of the original program code can be reused, so some people call the class "reusable software artifacts."



Not understood:

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:
When the public continues, horizontal access and vertical access are not restricted to the public members in the base class;
When private continues, horizontal access and vertical access are not accessible to public members in the base class;
When protection continues, for vertical access same as public continues, for horizontal access is the same as private continuation.

Public,protected,private access in C + +

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.