The difference between override and hidden (hide)

Source: Internet
Author: User

Conditions that must be met for rewriting:
1. The functions in the base class are modified by virtual, that is, the functions in the base class are virtual functions;
2. The virtual function in the derived class and the base class must have the same function prototype (that is, the return type is the same, the function name is the same, and the form parameter list is the same );
Let's look at a simple program:

# Include <iostream> Using STD: cout; Using STD: Endl; class person {public: Virtual void Study () {cout <"this is the method in the base class" <Endl ;}; class man: public person {public: Virtual void Study () {cout <"this is the method in the derived class" <Endl ;}; int main (void) {person * P = new man; P-> Study (); // output result: This is the return 0 method in the derived class ;}

The study () function in the man class overwrites the study () function in the person class.

Some people may think that the return types can be different. We can perform a test to change the study () method in the man class to the return value of the int type:

Virtual void Study () {cout <"this is the method in the derived class" <Endl; return 0 ;}

Re-compile, we will find that the program compilation fails, and the system prompts a type conflict and a rewriting error.

Therefore, overwriting or overwriting only occurs in polymorphism. For non-virtual functions in the base class, recreating a new definition in the derived class only causes hiding.

Hide: the function in the derived class shields the function in the base class.

Hidden Rules:
1. for non-virtual functions in the base class, if the derived class contains the same function name in the base class, regardless of whether the return value and the form parameter list are equal; all functions with the same name in the base class are hidden by the derived class;
2. for virtual functions in the base class, if the derived class contains the same function name in the base class and the parameter list is different, all virtual functions with the same name in the base class will be hidden by the derived class;

Note: If the functions in the derived class and virtual functions with the same name in the base class have the same list of parameters and different return values, the functions in the base class will not be hidden and will directly cause compilation errors (cause: at this time, the compiler will think that you are rewriting the method, rather than hiding it, but the return type is wrong ).

Let's look at a simple program:

# Include <iostream> Using STD: cout; Using STD: Endl; class person {public: void eat () {cout <"this is the Eat method in the base class" <Endl;} void work () {cout <"this is a work function without parameters in the base class and with a return value of Void" <Endl;} void work (int) {cout <"this is a work function with parameters and return void in the base class" <Endl;} virtual void Study () {cout <"this is a virtual method without parameters in the base class" <Endl;} virtual void Study (int) {cout <"this is a virtual method with a parameter in the base class" <Endl ;}}; class man: public person {public: int work () {cout <"this is a work function with no parameters in the derived class and return an int value" <Endl; return 0;} virtual void Study (int A, int B) {cout <"this is the virtual method in the derived class" <Endl ;}; int main (void) {MAN m; M. eat (); // M. study (); // This function has been hidden by the study (int A, int B) defined in the derived class. Calling this function will cause compilation errors. // M. study (1); // This function has been hidden by the study (int A, int B) defined in the derived class. Calling this function will cause a compilation error. study (1, 2); M. work (); // M. work (1); // This function has been hidden by the work () defined in the hidden derived class. Calling this function will cause a compilation error return 0 ;}

Analysis: The eat () method is not defined in the derived class. Therefore, the derived class can normally call the Eat () method in the base class, but the study () method in the following section, it is defined in a derived class. Although the function prototype is different, it also hides all functions named Study () in the base class. Note that: if the functions in the derived class and virtual functions with the same name in the base class have the same list of parameters and different return values, the functions in the base class will not be hidden and will directly cause compilation errors (cause: at this time, the compiler will think that you are rewriting the method, rather than hiding it, but the return type is wrong ). You can add an int in man.
Try the study () function. For work () functions, a function with the same name is also defined in the derived class, so a group of work () functions that are overloaded in the base class are blocked.

For hiding, we can also see from the literal meaning that only functions with the same name in the base class are blocked and not destroyed, so we can still call them.

There are three main methods:

1. Call a function of the same name in the base class in the function of the derived class, for example:

    virtual  void Study()    {        Person::Study();           }

2. explicitly declare the function using the Using Keyword before defining the function in the derived class;

Using person: study; virtual void Study () {cout <"this is a virtual method without parameters in the base class" <Endl ;}

3. uses a derived class object. base Class Name: Method in the base class; direct call; for example: for the above program, if we want to call the study () method in the base class, we can call it in this way: M. person: Study ();

With these three methods, we can access the functions defined in the base class blocked by the derived class.

Summary: the so-called overwrite (or overwrite) refers to the method body rewriting of the virtual functions in the base class by the derived class (the same function prototype is required, the function in the derived class shields all functions with the same name in the base class. (For virtual functions, the function names must be the same and the parameter list must be different. For non-virtual functions, the function names must be the same ). Note: The concept of overloading is not involved in inheritance, because the most basic requirement of overloading is: in the same scope, inheritance obviously belongs to different scopes.

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.