Introduction and difference of overload, override, and hide

Source: Internet
Author: User

This article introduces how to introduce and differentiate overload, override, and hide. If you need it, you can refer to it.

These three concepts are related to polymorphism in OO. It is easier to distinguish between overload and coverage, but hiding this concept makes the problem a little complicated. Let's talk about their differences.

Overload means that different functions use the same function name, but the number or type of function parameters are different. Different functions are called according to their parameters.

Overwrite (also called overwrite) refers to the re-implementation of virtual functions in the base class in the derived class. That is, the function names and parameters are the same, but the function implementation bodies are different.

Hiding is a function in a derived class that blocks functions with the same name in the base class. Hiding is similar to the other two concepts on the surface and is difficult to distinguish. In fact, their key difference is the implementation of polymorphism. What is polymorphism? Simply put, it is an interface. There are multiple implementations.

Reference other people's code to illustrate the problem (reference from Lin Rui's "High Quality C/C ++ programming guide").

Take a closer look at the following code:

The Code is as follows: Copy code

# Include <iostream. h>

Class Base

{

Public:

Virtual void f (float x) {cout <"Base: f (float)" <x <endl ;}

Void g (float x) {cout <"Base: g (float)" <x <endl ;}

Void h (float x) {cout <"Base: h (float)" <x <endl ;}

};

 
Class Derived: public Base

{

Public:

Virtual void f (float x) {cout <"Derived: f (float)" <x <endl ;}

Void g (int x) {cout <"Derived: g (int)" <x <endl ;}

Void h (float x) {cout <"Derived: h (float)" <x <endl ;}

};
 


Do you see anything? It is described below:

(1) The Derived: f (float) function overwrites Base: f (float ).

(2) The Derived: g (int) function hides the Base: g (float) instead of the overload.

(3) The Derived: h (float) function hides Base: h (float) instead of overwrite.

Well, I understand the concept, but what problems do we encounter in actual programming? Let's look at the following code:

The Code is as follows: Copy code

Void main (void)

{

Derived d;

Base * pb = & d;

Derived * pd = & d;
 

// Good: behavior depends solely on type of the object

Pb-> f (3.14f); // Derived: f (float) 3.14

Pd-> f (3.14f); // Derived: f (float) 3.14

 

// Bad: behavior depends on type of the pointer

Pb-> g (3.14f); // Base: g (float) 3.14

Pd-> g (3.14f); // Derived: g (int) 3 (surprise !)

 

// Bad: behavior depends on type of the pointer

Pb-> h (3.14f); // Base: h (float) 3.14 (surprise !)

Pd-> h (3.14f); // Derived: h (float) 3.14

}

In the first call, the action of a function depends on the object pointed to by the pointer. In the second and third calls, the action of a function depends on the pointer type. Therefore, hiding destroys the polymorphism in object-oriented programming, which can lead to confusion for OOP personnel.

However, hiding is not useless. It can help programmers find some wrong calls during compilation. However, I think we should try to hide these features as much as possible. Add them when adding virtual.


C ++ overloads hidden differences and execution methods

 
 
 
Feature of overloaded member functions
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
Overwrite refers to the function of a derived class that overwrites the base class function. The feature is
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.
"Hide" means that the function of the derived class shields the base class function with the same name as it. The rule is as follows:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

In three cases:
1. Overload: view parameters
2. Hide: Call whatever you use
3. Overwrite: Call a derived class
 

 

Overload, override, and hidden (hide)
Http://www.cppblog.com/zgysx/archive/2007/03/12/19662.html
Before writing a question, give a few keywords in Chinese and English, overload, override, and hide ). In early C ++ books, people who translate may not be familiar with professional terms (nor can they blame them. They are not engaged in computer programming and they are specialized in English ), overload and override are often incorrect!
Let's first look at some code and its compilation results.

Instance 1:
# Include "stdafx. h"
# Include <iostream. h>

Class CB
{
Public:
Void f (int)
{
Cout <"CB: f (int)" <endl;
}
};

Class CD: public CB
{
Public:
Void f (int, int)
{
Cout <"CD: f (int, int)" <endl;
}
Void test ()
{
F (1 );
}
};

Int main (int argc, char * argv [])
{
Return 0;
}
Compiled
Error C2660: 'F': function does not take 1 parameters


Conclusion: In the CD-like domain, there is no function such as f (int), and void f (int) in the base class is hidden.

If you change the declaration of the member function void f (int, int) in the derived CD to the same as that in the base class, that is, f (int), void f (int) in the base class) if it is still overwritten, compilation will not go wrong. In the function, test calls f (int) in CD)

Therefore, if some functions in the base class do not have the virtral keyword and the function name is f (whatever the parameter is), if an f member function is declared in the derived class CD, in the class CD domain, all the f in the base class are hidden.
If you are in a hurry and want to know what is hidden, read the simple description at the end of the article, but I suggest you continue to read it step by step.

What we just said is that there is no virtual. What if there is virtual ??
Example 2:

# Include "stdafx. h"
# Include <iostream. h>

Class CB
{
Public:
Virtual void f (int)
{
Cout <"CB: f (int)" <endl;
}
};

Class CD: public CB
{
Public:
Void f (int)
{
Cout <"CD: f (int)" <endl;
}
};

Int main (int argc, char * argv [])
{
Return 0;
}

Of course there is no problem with writing this. Here I don't have to pay much for it. This is very simple, with polymorphism and virtual functions. Then what is the pointer to the base class to point to the derived class object, by referencing and calling a virtual function, there are many attributes. What ?? You don't understand ?? Find a C ++ book and explain the polymorphism and virtual function mechanism !!
In this case, we call override )! Overwrite indicates that the virtual function of the derived class overwrites the function of the base class with the same name and same parameters!
Here, I want to emphasize that such coverage must meet two conditions.
(A) With the virtual keyword, you can add it to the function declaration in the base class.
(B) The functions in the base class CB should be exactly the same as those in the derived class CD. The functions are called exactly the same. The function names, parameters, and return types are three conditions.
Some people may question the statement in (B), saying that the return type should be the same ??
Yes, it must be the same if it is overwritten. I tried it. If the declaration of f is changed to virtual int f (int) in the base class, the compilation fails.
Error C2555: 'CD: f': overriding virtual function differs from 'cb: f' only by return type or calling convention
Therefore, the preceding (a) (B) conditions must be met.

If the f keyword of the function in the base class CB is virtual, but the parameter is different from the f parameter of the function in the derived class CD,
Example 3:
# Include "stdafx. h"
# Include <iostream. h>

Class CB
{
Public:
Virtual void f (int)
{
Cout <"CB: f (int)" <endl;
}
};


Class CD: public CB
{
Public:
Void f (int, int)
{
Cout <"CD: f (int, int)" <endl;
}

Void test ()
{
F (1 );
}
};

Int main (int argc, char * argv [])
{
Return 0;
}

Compilation error,
Error C2660: 'F': function does not take 1 parameters
Why ?? What is a common mistake ?? Yes, it is the same as in instance 1. The conclusion is that the function in the base class is hidden.

Through the above three examples, we can draw a simple conclusion.
If the function in the base class and the function f in the derived class have the same name
The following two conditions are met:
(A) There are virtual keywords in function declaration in the base class.
(B) The functions in the base class CB are exactly the same as those in the derived class CD. function names, parameters, and return types are all the same.
So this is called override, which is also a virtual function, the nature of polymorphism.

What about other situations ?? As long as the name is the same and does not meet the conditions covered above, it is hidden.

Next I want to talk about the most important part. Many people think that f (int) in the base class CB will inherit and f (int, int) in the CD will constitute an overload in the derived class CD, as you can imagine in instance 1.
Right? Let's first look at the definition of heavy load.
Overload ):
A function must have the same name but different function parameters in a domain. The function of overloading has different behaviors for the same function. Therefore, a function in a domain cannot constitute an overload, this is an important feature of heavy load.
It must be in a domain, but inheritance is obviously in two classes, so the above idea is not true. The same is true for the structure we tested. f (int, int) in the derived class) hides f (int) from the base class.
Therefore, for functions with the same function name, the relationships between the base class and the derived class can only be overwritten or hidden.

In the article, I gave both the definition of overload and coverage, but I never gave them a hidden definition. At the end, I gave it, this is a long term from google on the Internet. You can simply understand that in the derived class domain, you cannot see the function with the same name in the base class, or, is not inherited for you to use, huh, like instance 1.
  

Hide (hide ):
It indicates that the member function of the derived class hides the member function of the base class function. the word hidden can be understood as follows: when calling a class member function, the compiler will look up the function definition step by step along the inheritance chain of the class, if yes, the query is stopped. Therefore, if both a derived class and a base class have a function with the same name (whether or not the parameters are the same, the compiler finally selects the function in the derived class, so we can say that the member function of this derived class "hides" The member function of the base class, that is to say, it prevents the compiler from continuing to look up the function definition.

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.