Public protected private & Virtual

Source: Internet
Author: User
PRIVATE: Private controller. Such members can only be accessed by member functions in the class and friends functions of the class. Protected: the protected operator. Such members can be accessed by member functions and friends functions of the class, or by member functions of the derived class and friends functions of the class. Public: A total of control operators. Such members can be accessed by member functions and membership functions of the class, or referenced by other functions in the class scope. Virtual: C ++ implements polymorphism through virtual functions. "No matter what type of message objects are sent, they send messages in the same form, and the message processing method may change with the objects that take over the message" is called polymorphism. Virtual functions are limited by virtual keywords. Anyone who has learned C ++ knows that a function with the virtual keyword added to class base is a virtual function (such as function print ), therefore, in the base-derived class derived, You can override the virtual function to overwrite the base-class virtual function. When the base class base pointer point points to the object of the derived class derived, the call to the print function of the point actually calls the print function of the derived instead of the print function of the base. This is the embodiment of polymorphism in the object-oriented model. A virtual function is a member function declared as virtual in a class. When the compiler sees that such a function is called through a pointer or reference, it is bound late. 1. In the public/protected/private/virtushortname ++ file, the member is the private access permission by default. In structure, the member has the public access permission by default. Both support inheritance, constructor, and destructor. Objects are class instantiation. For the entire class, member functions are shared by all instantiated class objects, that is, a class retains only one member function. Each instance object is only initialized to the data member. In the memory image, each object only retains its own copy of the data member. So how does an object relate to a member function that can be considered as "separated", that is, how does a member function operate on the data member of an object? Remember this pointer, regardless of whether the object passes through (.) operations or (->) Operations call member functions. during compilation, the compiler converts such calls into common global functions, and there is an extra parameter (usually the first parameter), and then the this pointer is passed into this parameter. Therefore, the binding (or contact) between the object and the member function is completed ). The class definition uses three access modifiers private/public/protected. They need to control the access permissions of a function to members of a class (including member variables and member methods. PRIVATE: It can only be accessed by functions in this class and their friends functions. Other user programs cannot access it through class objects. protected: it can be accessed by functions in the class, sub-class functions (under the Public inheritance), and their friends functions. In addition, user programs cannot access it through class objects; public: it can be accessed by the functions in the class, the sub-class functions (under the Public inheritance), and their friends functions, and can also be accessed by the objects in the user program. In summary, a class member (including all member functions of the class member function or the member function of the class member) can access any member of the class (including member variables and member methods ). Except for friends, Private Members can only access member functions of the class. Protected members can only access member functions of the class and member functions of Its Derived classes, while public members can access all functions. Class Members, no matter which access modifier is used, must be accessed through the class object. Even within a member function of the class, the accessed data is carried out through the class object. The first parameter of each member function is the this pointer by default, all the accessed data members are in the "This->" mode, but are omitted by default. The access modifier of C ++ takes the class as the unit, rather than the object as the unit. In general, similar objects can access each other's data members, but the access path is not direct access. 2. changes in access attributes of members after class inheritance: private inheritance is used, and all members of the parent class become private in the subclass; protected inheritance is used, and the protected and public members of the parent class become protected in the subclass, the private member remains unchanged. The method attributes in the parent class do not change with the public inheritance. After the inheritance of the class, the members of the base class can be understood as: to become a member of the inheritance class, only to make the corresponding access attribute change, although the base class member seems to have become an inherited class member, it is still different from its own inherited class data member, for example: the inherited class member function cannot access the Private Members of the inherited base class, but can access the inherited public and protected members. 3. Virtual reserved words here we will talk about polymorphism refers to the dynamic polymorphism that takes effect at runtime. The dynamic polymorphism technology of c ++ is based on the Inheritance Mechanism and virtual function. Polymorphism can be understood as: Different Action behaviors can be associated with the same mark. Generally speaking, pointers or references of the base class can call the base class functions or execute the inherited class functions. The function call here must have a dynamic binding. To implement this dynamic binding, two conditions must be met: only a member function specified as a virtual function can be dynamically bound; function calls must be performed through pointers or references of the base class type. The static and dynamic types of references and pointers can be different, which is the cornerstone of C ++ to support polymorphism. Because each derived class object contains the base class part, you can bind the reference of the base class type to the base class part of the derived class object, you can also use a pointer of the base class type to point to the object of the derived class (but you cannot use an inherited class type reference or pointer to bind a base class object unless the type conversion is forced ). The reference or pointer of the base class type is known during compilation. This is a static type, but the object types they are bound to are only known at runtime, and they may be different from their static types, so the final types they are bound are dynamic types. To understand the dynamic binding in polymorphism, we must first understand how C ++ determines the function call at the inheritance level: first, determine the object, reference, or pointer static type of the function call; find the function with the same name (regardless of the parameter) as the called function in this class. If the function cannot be found, search for it in the class's direct base class, and follow the inheritance chain to search for it, the call is incorrect until a function with the same name is found or the last class is found. If the name cannot be found in the class or other base classes; once a function with the same name as the function to be called is found in a class, find the function overload versions with the same name in this class, check whether a function can be found with the same type as the real parameter of the function to be called. If the function cannot be found, the call is invalid. (the first three steps are determined during compilation.) If the function call is legal, if a function is a virtual function with a common reference or pointer, the compiler generates code to determine which function version to run based on the dynamic type of the object. Otherwise, the compiler generates code to directly call the function. Starting from the virtual keyword, consider the two-layer relationship: a base class and an inherited class, using the base class type reference or pointer for function calls, first, find a function in the base class that can be of the same type as the real parameter of the function to be called. If the function cannot be found, an error will be called. If it is found, check whether the function is a virtual function, if this function is a virtual function in the base class and has the same prototype in the inherited class, functions in the inherited class are automatically converted to virtual functions even if the virtual reserved words are not used, then, during the running process, the corresponding function is called based on the base class pointer or reference bound object. If the inherited class does not have the same prototype function, even if the runtime is bound with an inherited class object, then, call the functions in the base class.

 

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.