Public,protected,private access in C + +

Source: Internet
Author: User

First:Private, Public,protectedthe access scope of the access designator. Private: only by 1. Functions in this class,2. Its friend function access. The object of the class cannot be accessed by any other access. protected: can be 1. Functions in this class,2. A function of a subclass, and 3. Access to its friend function. But cannot be accessed by objects of that class.  Public: can be 1. Functions in this class,2. A function of a subclass,3. Its friend function access can also be accessed by 4. 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. Privateproperty cannot be inherited. With private inheritance, the protected and public properties of the parent class become private in the subclass, and the protected and public properties of the parent class become protected in the subclass, using protected inheritance,   The protected and public properties in the parent class do not change; As shown below: Public:protected:Private:p ublic Inheritance Public              protectedDo not use protected inheritanceprotected       protectedDo not use private inheritancePrivate           Privatenot 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: (1The visibility of base class members to their objects: Public members are visible, others are invisible.  Here the protection members are identical to the private members. (2The visibility of base class members to derived classes: Public members and protected members are visible, and private members are not visible.  The protection of Members here is the same as public members. (3The visibility of base class members to derived class objects: 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 mode: (1The visibility of base class members to their objects: Public members are visible and other members are not visible. (2The visibility of base class members to derived classes: Public members and protected members are visible, and private members are invisible. (3The visibility of base class members to derived class objects: All members are not visible.  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 continues in the same way as private continuation.  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: When public continues, horizontal access and vertical access are unrestricted for public members in the base class; When private continues, horizontal access and vertical access to the public members of the base class are also inaccessible; protection continues for vertical access same as public continues, for horizontal access same as private continuation.  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:1The hierarchy of derived classes that are materialized classes of base classes often reflects some 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. 2A . Derived class is a continuation of a base class definition that first defines an abstract base class in which some operations 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. 3The . Derived class is 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." 

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.