Private/protect/public & Virtual in C + +

Source: Internet
Author: User
Tags access properties modifiers

Private

The private control character. Such members can only be accessed by member functions in this class and by friend functions of the class.

Protected

The protected control character. Such members can be accessed by member functions in this class and by the friend functions of the class, as well as by the member functions of the derived class and the friend functions of the class.

Public

A common control character. Such members can be accessed by member functions in this class and by friend functions of the class, or by other functions within the scope of the class.

Virtual

C + + uses virtual functions to achieve polymorphism. " Regardless of the class of the object that sent the message, they send a message of the same form, and the processing of the message may vary with the object that took over the message, which is known as polymorphism. Virtual functions are defined by the virtual keyword.

As long as anyone who has studied C + + knows that a function that adds the virtual keyword to the class base is a dummy function (such as a function print), it is possible to override the virtual function by overriding it in the derived class derived of base. When the pointer point of base class base points to an object of the derived class derived, the call to the print function of a is actually called the print function of derived instead of base. This is the embodiment of polymorphism in object-oriented.

A virtual function is a member function that is declared as virtual in a class, and when the compiler sees a call to such a function by a pointer or reference, it performs a late binding on it.

1.
Public/protected/private/virtual

In C + +, members default to private access.
Structure, the member defaults to public access.
Both support inheritance/construction/destruction and so on.
An object is an instantiation of a class. The member function is shared by all instantiated class objects for the entire class, that is, a class retains only one copy of the member function. Each instance object initializes only the data members in it, and each object in the memory image retains only the copy of the data member that belongs to it.
So how does each object relate to these member functions that can be thought of as "detached", that is, how member functions manipulate the data members of an object? Remember this pointer, regardless of the object passing (.) Operation or (-) Action call member function, compile time, the compiler will convert this call to our common form of global functions, and a parameter (usually this parameter is placed in the first), and then pass the this pointer to this parameter. The binding (or contact) of the object to the member function is completed.
The definition of a class uses the three access modifiers private/public/protected, which control the access of a function to members of a class, including member variables and member methods.

Private: Only the function in the class, its friend function access, in addition to the user program can not be accessed through the class object;
Protected: can be accessed by functions in the class, functions of subclasses (under public inheritance), and their friend functions, except that the user program cannot access them through the class object;
Public: can be accessed by functions in the class, by functions of subclasses (under public inheritance), by their friend functions, and by objects of that class in the user program.

To sum up is:

A class friend (all member functions that contain a friend function or a friend Class) can access any member of the class (including member variables and member methods).
Except for friends, private members are accessible only to member functions of the class itself, and protected members are accessible only to member functions of that class and to members of their derived classes, and public members are accessible to all functions.

A member of a class, regardless of which access modifier is used, must be accessed through the object of the class. Even within the member functions of the class, the data accessed is through the class object, and the first parameter of each member function is the this pointer, and the data members accessed are all performed by the "this->" method, but omitted by default. The access modifiers for C + + are in the class, not in the object units. In layman's words, similar objects can "access" each other's data members, but the access path is not directly accessible.

2. Changes to member access properties after class inheritance:

With private inheritance, all members of the parent class become private in subclasses;
With protected inheritance, the protected and public members of the parent class become protected,private members in the subclass;
With public inheritance, the method properties in the parent class do not change.

After inheriting the class, the members of the base class can be understood as being members of the inheriting class, only to make the corresponding access property changes, although the base class member seems to be a member of the inheriting class, but it is also different from inheriting the class data members, for example: inheriting class member functions cannot access the private members of the inherited base class. However, you can access the inherited public and protected members.

3. Virtual Reserved words

What we're going to talk about here is that polymorphism is the dynamic polymorphism that takes effect at runtime, and the dynamic polymorphism of C + + is based on the inheritance mechanism and virtual function. Polymorphism can be understood as: different action behavior can be associated with the same token. In layman's words: A pointer or reference to a base class type can call a function of a base class, or it can execute a function that inherits a class. The function call here must have a dynamic binding, to achieve this dynamic binding must meet two conditions:

Only member functions that are specified as virtual functions can be dynamically bound;
A function call must be made through a pointer or reference to a 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 a base class part, you can bind to the base class part of the derived class object with a reference to the base class type, or you can point to the derived class object with a pointer to the base class type (but you cannot bind the base class object with an inherited class type reference or pointer, unless you force the type conversion). A reference to a base class type, or a pointer to a compiler, is known as a static type, but the type of the object they are bound to is at run time and may differ from their static type, so they eventually bind to a dynamic type.

To understand dynamic binding in polymorphism, first understand how C + + determines the function invocation in the inheritance hierarchy:

First, determine the static type of the object, reference, or pointer that makes the function call;
A function that finds and invokes a function of the same name (regardless of parameters) in the class, finds it in the immediate base class of the class if it is not found, and then follows it through the chain of inheritance, until it finds the same name function or finds the last class, and if it cannot be found in the class or other base class, the call is wrong;
Once a function with the same name as the function to be called is found in a class, the function overloaded version of all the names is looked up in this class to see if a function can be found that is the same as the function argument type to be called, and the call is not valid if it cannot be found; (the first three steps are determined at compile time)
If a function call is valid, if the function is a virtual function, and a generic reference or pointer is called, the compiler generates code to determine which version of the function is run based on the object's dynamic type, otherwise the compiler generates code to call the function directly.

Simply from the virtual keyword, consider a two-tier relationship: A base class an inheriting class, using a base class type reference or a pointer to a function call, first in the base class to find a function argument type to be called the same function, if not found, the call error, if found, to see if the function is a virtual function, If the function in the base class is a virtual function, and there are functions of the same archetype in the inheriting class, even if the function in the inheriting class is not reserved with virtual, the functions in the inherited classes automatically become virtual functions, and then the corresponding function is invoked in the process according to the base class pointer or the object bound by the reference, if there is no function in The function in the base class is called even if the runtime binds to an inherited class object.

Original Reprint Address:

http://blog.csdn.net/bao_jinyu/article/details/7569186

Private/protect/public & Virtual 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.