Hide, overwrite, reload when C + + Parent-child class inherits

Source: Internet
Author: User

When there is a parent-child class inheritance relationship, it is possible to hide, overwrite, and reload if there is a member function with the same name. For beginners is also more easily confused, for this, I collated my personal views, for reference only. Hope to understand the help, also welcome correction.

1. Parent-Child class inheritance relationships: Subclasses replicate all members of the parent class

First, understand how the inheritance of parent-child classes occurs. On this basis it is easy to understand the relationship between them and the difference.

Each class has its own member variable and member function, which is a separate space overall. When a subclass inherits a parent class, it copies all of the members of the parent class as members of the subclass, but it also marks whether the members are inherited from the parent class, or whether they are members of the subclass itself. It is assumed that there is a subclass domain for the members of the subclass itself, and the parent class domain is copied from the parent class.

For example, there are two domains in the Childer class, the subclass domain and the parent class domain, which do not interfere with each other.

1 classFather2 {3     intf_a;4     intF_b;5 };6 7 classChilder: PublicFather8 {9     intc_a;Ten     intF_b; One }; A  - intMain () - { thecout<<"sizeof Childer:"<<sizeof(Childer) <<Endl; -- -cout<<"sizeof father:"<<sizeof(Father) <<Endl; 8 -}

The running results show that the subclass size is 16, the parent class size is 8, that is, the subclass does have 4 member variables, and even members of the same name are copied.

2. Hide: Sub-class objects take precedence over subclass Domain Self members (member variables and member functions)

The main reason for shadowing is that when a child class has a member of the same name as a parent, the subclass object accesses the member, and a conflict occurs. So the compiler handles it by prioritizing its own members in the subclass domain.

That is, when a subclass object accesses a member, such as Ch.m_m or CH.F (), the member variable and the member function are the same. The compiler is first retrieved in the subclass domain, and if the member is found in the subclass domain, the retrieval ends and the member is returned for access. If the member is not found in the subclass field, it is retrieved from the parent class domain. If the parent class exists in the domain, the member is returned for access, and if the parent class does not exist in the domain, a compilation error is made and the member is not valid.

When the parent-child class domain has the same member, the compiler takes precedence in retrieving the subclass, even if a member of the same name exists in the parent class domain. Therefore, the member in the parent class domain is hidden from the same name in the class domain, which means that the member is not present when it is accessed, and if you want to access that member in the parent class domain, only by displaying the call, that is: Ch. Father::m_m;

The following code shows that in order to have a specific description of the problem, the Members here are public, also do not involve the construction of the destruction and other issues.

1 classFather2 {3  Public:4     intf_a;5     intF_b;6 7     voidFF1 () {cout<<"Father Ff1"<<Endl;}8 };9 Ten classChilder: PublicFather One { A  Public: -     intc_a; -     intF_b; the  -     voidCF1 () {cout<<"Childer CF1"<<Endl;} -     voidFF1 () {cout<<"Childer ff1"<<Endl;} - }; +  - intMain () + { A Childer ch; at      -cout<<ch.c_a<<endl;//member variables in the subclass domain only -cout<<ch.f_b<<endl;//both the subclass domain and the parent class domain exist, giving precedence to the class in the subclass domain -Cout<<ch. father::f_b<<endl;//Show access to hidden member variables -  -cout<<"====================\n"; in      - ch.cf1 (); to ch.ff1 (); + Ch. Father::ff1 (); -}

The running result can be seen, ch.f_b; and Ch.  Father::f_b; Two members of the same name exist at the same time. But when accessed, the child class member hides the parent class member, and only the call is displayed to access the parent class member.

This effect is more pronounced with access to the member functions, CH.FF1 (); When called, the member function of the same name in the subclass field is called.

And when the compiler retrieves it, it is based only on the name, regardless of the function's parameter and return type.

1 int ff1 (int a) {cout<<"childer ff1"<<endl;  Return0;}

If the function in Childer is changed to the above type. When called in the main function, ch.ff1 (); Compile error. Because the subclass of int ff1 (int a), the parent class's void ff1 () is hidden. So there is no overload between them.

Should be changed to CH.FF1 (10); This matches the member in the subclass domain. or Ch. FATHER::FF1 (); Displays the call to members in the parent class domain.

3. Overwrite: virtual function, member function type one touch, the parent class pointer calls the subclass object member

Overrides occur only in the case of a virtual function, and the parent-child class member function type must be the same, that is, both the parameter and the return type must be identical. When a subclass object is called, the member function in the subclass field is called directly, and the member of the same name in the parent class domain is overwritten by the member of the parent class as if it does not exist (which can be displayed). Here many people will feel puzzled, think is hidden, because the member function of the parent class is still there, can still be called, but the first call to subclass, that is, "hidden". and "overwrite" the meaning of two words, should be one to replace another, that is, the other does not exist.

As a small example, it is obvious that the member functions of the parent-child class are present at the same time, covering the case.

Virtual void ff1 () {cout<<"father ff1"<<endl;}

Add the FF1 function in the above example father class to virtual, and the other does not change, and the result is unchanged.

The following explains the origin of the word "overwrite".

First, it is necessary to understand that the proposed virtual function is to achieve polymorphism. That is, the purpose of a virtual function is to call a virtual function when the parent pointer points to a different subclass object, calling the member function of the corresponding subclass object, that is, the specific subclass object can be automatically recognized. Therefore, in the above example, it is meaningless to call virtual functions directly with the subclass object, which is not used in general.

1 classFather2 {3  Public:4     Virtual voidFF1 () {cout<<"Father Ff1"<<Endl;}5 };6 7 classChilder_1: PublicFather8 {9  Public:Ten     voidFF1 () {cout<<"childer_1 ff1"<<Endl;} One }; A classChilder_2: PublicFather - { -  Public: the     voidFF1 () {cout<<"childer_2 ff1"<<Endl;} - }; -  - intMain () + { -father*FP; +  A childer_1 ch1; atFP = &ch1; -Fp->ff1 (); -  - childer_2 CH2; -FP = &CH2; -Fp->ff1 (); in      -     return 0; to}

With virtual functions, all are the form of a parent pointer, PF->F11 (). The example of 24 lines and 28 lines, the same code, because the FP points to different objects, so call the virtual function of different objects. But from the code point of view, the FP is a pointer to the Father class, but the subclass member function is called, as if the members of the parent class were overwritten. This is the source of the word coverage.

In the case of overrides, the subclass virtual function must have the same argument list as the parent virtual function, otherwise it is considered a new function that has no relation to the parent class's function of the same name. However, it is not possible to assume that two functions make up overloads. Because two functions are in a different domain.

Example:

1 classFather2 {3  Public:4     Virtual voidFF1 () {cout<<"Father Ff1"<<Endl;}5 };6 7 classChilder_1: PublicFather8 {9  Public:Ten     voidFF1 (intA) {cout<<"childer_1 ff1"<<Endl;} One }; A  - intMain () - { thefather*FP; -  - childer_1 ch1; -FP = &ch1; +Fp->ff1 (); -CH1.FF1 (); No matching Members +CH1.FF1 (2); A  at     return 0; -}

The result of the operation is:

Father Ff1childer_1 Ff1

From the running results of 19 rows of fp->ff1 (), the FP points to the subclass object and calls the virtual function. However, the virtual function does not have a corresponding implementation in the subclass, and it has to use that member of the parent class.

The ff1 of the 10th row does not overwrite the non-argument ff1 inherited from the parent class. Instead, it is considered a new function.

4. Overloading: Different parameter functions with the same name in the same domain

The overloads must be between two different parameters of the same name that occur in the same domain. If one in the parent class domain is one in the subclass domain, there is no overload, which belongs to the hidden condition. When called, only the subclass field is searched, and if the parameter does not conform, it is considered not to be a search in the parent class domain.

5. Summary

Overloading is a function relationship under the same domain, which is generally not considered in the case of parent-child classes.

Hidden, is a subclass that rewrites, overrides the code of the parent class. The overrides, however, assume that the subclass implements the virtual function of the parent class. The virtual function of the parent class can not implement the body, become pure virtual function, waiting for the subclass to implement. When hiding, the function of the parent class must also have the implementation body. Hide or overwrite, just a different statement, as long as the compiler in the call, if you retrieve, match the corresponding function.

In summary, the following points are summarized:

1. A subclass is a copy of all the members of the parent class and is saved in a different domain. If the same name exists, there will be two in the subclass, respectively, in the subclass domain and the parent class domain.

2. When invoked, it is retrieved from the type of the calling object (or pointer), retrieved from its own domain, and if found, judged whether it is a virtual function, not a virtual function, if it is a virtual function, the function of the real object is called through runtime type recognition. If not found, go to its parent domain to retrieve, repeating just the judgment. Know the calling function or no matching member.

Understand the calling process:

2.1 In general, which type is the one that is transferred to a member of its own domain.

Father F; F.A; F.FF1 (); Because f is of type father, all calls are father members of their own domain.

Childer C; C.A; C.FF1 (); Because C is a chiler type, all calls are childer members of their own domain.

The pointer is the same.  FATHER*FP;  fp->a;   FP->FF1 (); Because the FP is a pointer to the father type, all calls are father members of their own domain.

Even fp = new childer. FP->FF1 (); The child class object is pointed to, and the parent class's own members are still called. Because the FP is of type father.

Childer *CP; cp->a;   CP->FF1 (); Since the CP is a pointer of type Childer, all calls are childer members of their own domain.

2.2. A special case is when a member function is a virtual function, although it is a pointer to the parent class type, it is called according to the specific object pointed to by the pointer.
That is, if FF1 is a virtual function, FATHER*FP; fp = new Childer;   FP->FF1 (); Although the FP is a pointer of type father, because ff1 is a virtual function, it is called a member of a specific object, Childer class.

Compare the same statement in 2, which is the meaning of virtual function and polymorphism.

Hide, overwrite, reload when C + + Parent-child class inherits

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.