C ++ syntax function overload

xiaoxiao2021-03-06  71

The so-called functional overload refers to the implementation of the same function name to correspond to multiple functions. For example, you can define a plurality of functions implementations to the function name add (), the function of the function is to sum, that is, the sum of the two operands. Among them, a function implementation is the sum of two int models, and the other implementation is the sum of two floating point types, and then an implementation is to ask two plurals. Each implementation corresponds to a function body, the name of these functions, but the type of function of the function is different. This is the concept of function overload. Function overload is especially important in the application of classes and objects.

The function overload requires the compiler to uniquely determine which function code should be executed when a function is called, which is implemented. When determining the function implementation, the number and type of the function parameters are required to distinguish. That is to say, when the function is overloaded, the same name function is required to be different in the number of parameters, or different parameter types. Otherwise, an overload will not be achieved.

Different overload functions on parameter types

The following is an example of the overload function different in the parameter type:

#include

INT Add (int, int);

Double add (double, double);

void main ()

{

Cout <

Cout <

}

Int Add (int x, int y)

{

Return X Y;

}

Double Add (Double A, Double B)

{

RETURN A B;

}

In this program, the two functions of the same name add in the main () function are called, and the front one add () function corresponds to the function implementation of the two INT types, and the back side corresponds to two. Double-designer function implementation. This is the overload of the function.

The above program output is:

15

15.5

Different overload functions on the number of parameters

The following is an example of a heavy load function that is different on the number of parameters:

#include

INT min (int A, int b);

INT min (int A, int b, int C);

INT min (int A, int B, int C, int D);

void main ()

{

Cout <

Cout <

}

INT min (int A, int b)

{

Return A

}

Int min (int A, int b, int C)

{

INT t = min (a, b);

Return MIN (T, C);

}

Int min (int A, int B, Int C, INT D)

{

INT T1 = min (a, b);

INT T2 = min (C, D);

Return MIN (T1, T2);

}

The function overload has occurred in the program, and the function namemin corresponds to three different implementations. The distortion of the function is different depending on the number of parameters. In the three functions here, the number of parameters is 2, 3 and 4, respectively, in the call function Different functions are selected according to the number of arguments.

Function overload is more than class and object applications, especially in the polymorphism of the class. In the future, we will encounter more functions in types, especially in the inheritance of the binding class, and these are often used in our future use of VC programming.

转载请注明原文地址:https://www.9cbs.com/read-91562.html

New Post(0)