C ++ overload, overwrite, and hide

Source: Internet
Author: User

1. Reload

Function overloading means that the same function name can correspond to multiple functions. For example, you can define multiple function implementations for the function name add (). This function is used to calculate the sum of two operands. One function is to calculate the sum of two int-type numbers, the other is to calculate the sum of the two floating-point numbers, and the other is to calculate the sum of the two plural numbers. Each implementation corresponds to a function body. These functions have the same name, but their parameters have different types. This is the concept of function overloading. Function Overloading is particularly important in applications of classes and objects.

Function overloading requires the compiler to uniquely determine which function code should be executed when a function is called, that is, which function is used for implementation. When determining the function implementation, the number and type of function parameters must be distinguished. This means that during function overloading, functions with the same name are required to have different numbers of parameters or different parameter types. Otherwise, the overload cannot be implemented.

Here, we need to note that the overload is generated only between functions in a class.

Sample Code:

# Include <iostream> # include <cstdlib> using namespace std; class OverLoad {public: int add (int x, int y); int add (int x, int y, int z) ;}; int OverLoad: add (int x, int y) {return x + y;} int OverLoad: add (int x, int y, int z) {int temp = x + y; return temp + z;} int main () {OverLoad ad; int ri = ad. add (9, 4); int rd = ad. add (9, 4, 6); cout <"int2 add result:" <ri <endl; cout <"int3 add result:" <rd <endl; system ("pause"); return 0;} // output 13 and 19

 

2. Overwrite
Different from overload, overwrite the function of the parent class through the virtual function (virtual) mechanism, that is, the function type corresponding to the parent class must be consistent with the parameter type, note that the override function cannot be declared as static. In the following code example, there is an overload between fun and fun. fun is included in the same Base class, And fun2v is overwritten, using virtual functions (virtual ).

Sample Code:

# Include <iostream> # include <cstdlib> using namespace std; class Base {public: void fun (int x) {cout <"Base: f (int) "<x <endl;} void fun (float x) {cout <" Base: f (float) "<x <endl ;} virtual void fun2v (void) {cout <"Base: f (void)" <endl ;}; class Derived: public Base {public: void fun2v (void) {cout <"Derived: f (void)" <endl ;}}; int main () {Derived d; Base * pb = & d; pb-> fun (100); pb-> fun (100.00f); pb-> fun2v (); system ("pause");} // output: // Base :: f (int) 100 // Base: f (float) 100 // Derived: f (void)

3. Hide

Hiding is similar to the first two points, but unlike overloading, it hides a function between a parent class and a derived class, that is, a cross-class function, different from overwriting, the function types of the derived class functions and the parent class functions can be different or have no virtual keywords. Of course, this is true when the function name is the same.

Therefore, there are two forms to hide

1). 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, the virtual class is dispensable (not overloaded). For example, the following function fun3v

2). 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 (not overwrite). For example, the following function fun4v

Sample Code:

# Include <iostream> # include <cstdlib> using namespace std; class Base {public: void fun (int x) {cout <"Base: f (int) "<x <endl;} void fun (float x) {cout <" Base: f (float) "<x <endl ;} virtual void fun2v (void) {cout <"Base: f (void) 2 verson" <endl;} virtual void fun3v (int x) {cout <"Base:: f (int) 3 version "<x <endl ;}// virtual can have void fun4v (int x) {cout <" Base: f (int) 4 version "<x <endl ;}; class Derived: public Base {public: void fun2v (void) {cout <" Derived: f (void) 2 version "<endl;} // overwrite void fun3v (int x, int y) {cout <" Derived: f (int, int) 3 version "<x <y <endl;} // hide 1st forms of void fun4v (int x) {cout <" Derived: f (int) 4 version "<endl ;}// hide 2nd forms}; int main () {Derived d; Base * pb = & d; Derived * pd = & d; pb-> fun (100); pb-> fun (100.00f); pb-> fun2v (); cout <"--------------------------" <endl; pd-> fun3v (3, 4); pd-> fun4v (1); system ("pause");} // output: // Base: f (int) 100 // Base: f (float) 100 // Derived: f (void) 2 version // --------------------------------- // Derived: f (int, int) 3version34 // Derived: f (int) 4 version

 

4. Differences between Overloading, overwriting, and hiding

In summary, the differences between them can be summarized as follows: (refer to the popular saying on the Internet)

  • 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)

 

 

 

 

 

 

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.