In C ++, reload, rewrite, hide the difference, and reload rewrite

Source: Internet
Author: User

In C ++, reload, rewrite, hide the difference, and reload rewrite

Overload:

Overload means that in the same scope, the function name is the same, but the number of function parameters, or the type of parameters, the order of parameters is different. At this time, functions constitute an overload relationship. Note that, if the list of parameters of a function is identical, only different return value types cannot constitute an overload. How does one understand the same scope? There are two main cases to be discussed here: one is outside the class but the function belongs to the same scope in the same CPP file, and the other is in the same scope in the class.

The function is outside the class, but it is in the same CPP file.

// The function is out of the class, but is in the same CPP file # include <iostream> using namespace std; void test (int I) // function 1 {cout <"int" <endl;} void test (double I) // It has the same number of parameters as function 1 and has different parameter types, overload {cout <"double" <endl;} void test (int I, int j) // it is of the same type as function 1, but the number of parameters is different, overload {cout <"int" <endl;} int test (int I) // only return value types are different, cannot constitute a overload {cout <"return int" <endl ;}

 

The function can also constitute an overload in the class. The function name and parameter list are identical, but the function has the const keyword modifier.

// Function # include <iostream> using namespace std; class A {public: A () {}~ in the same class (){}~ A () {}; void test (int I) // function 1 {cout <"int" <endl;} void test (double I) // The number of parameters is the same as the number of parameters in function 1. The parameter types are different and constitute the reload {cout <"double" <endl;} void test (int I, int j) // The same as function 1 parameter type, but the number of parameters is different, constitute a heavy load {cout <"int" <endl;} void test (int I) const // same as the number of parameter type parameters in function 1, which is modified with the const keyword to form a heavy load {cout <"const int" <endl ;}};

 

Rewrite (overwrite ):

Rewriting is also called overwriting. It occurs in the parent class and subclass. When there is a virtual function in the parent class, a new virtual function name with the parent class is defined in the subclass, the list of function parameters is identical, and the specific implementation of the function is rewritten.

// Rewrite # include <iostream> using namespace std; class A {public: (){}~ A () {}; virtual void test () {cout <"A: test ()" <endl ;}}; class B: public A {public: B (){}~ B () {} virtual void test () // virtual keywords can be written but not written {cout <"B: test ()" <endl ;}};

 

 

Hide:

Hiding also occurs in the parent class and subclass. There are two main situations:

1. The function name of the subclass and parent class. The parameter type is identical, but the parent class function does not have the virtual keyword modifier.

2. The sub-class and parent-class functions have the same name but different parameter lists. In this case, hidden functions can be formed no matter whether virtual keywords are modified.

// The Function Name of the subclass and parent class. The parameter type is identical, but the parent class function does not have the virtual keyword modifier # include <iostream> using namespace std; class A {public: () {}~ A () {}; void test () {cout <"A: test ()" <endl ;}}; class B: public A {public: B () {}~ B () {}void test () {cout <"B: test ()" <endl ;}};

 

 

// The subclass and the parent class function have the same name, but the parameter list is different. In this case, no matter whether the virtual keyword is modified, they can constitute A hidden # include <iostream> using namespace std; class A {public: A (){}~ A () {}; void test (int I) {cout <"A: test ()" <endl ;}}; class B: public A {public: B (){}~ B () {}void test () {cout <"B: test ()" <endl ;}};

// The test function of the parent class needs to input an int type parameter. The test parameter of the subclass does not need to be input any parameter. If the following statement is available:
// B B;
// B. test (1); // Error
// Because the void test () function in the subclass has hidden the void test (int I) function in the parent class, in this way, the subclass object B cannot access void test (int I ), in this case, you can use B. a: test (1 ).

 

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.