Next, we will discuss how to study C ++ function overloading. C ++ function overloading means that the same function name can correspond to the implementation of multiple functions, if you want to overload the function of the compiler, use extern "C.
Another implementation is to calculate the sum of the two floating point types, 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.
For different overload functions of different parameter types, the following is an example of an overload function with different parameter types:
- #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 main () function calls two functions with the same name as add. The add () function at the front corresponds to the function implementation of the sum of two int numbers, the next add () function corresponds to the implementation of two double-type number summation functions. This is the function overload.
The following is an example of an overloaded function with different numbers 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);
- }
This program contains C ++ function overloading. The function name min pair should have three different implementations. The function differentiation varies depending on the number of parameters. In the three function implementations, the number of parameters is 2, 3, and 4, respectively. Different function implementations are selected based on the number of real parameters when a function is called.
C ++ function Overloading is widely used in classes and objects, especially in class polymorphism. In the future, we will encounter more function overloading in different types, especially in combination with the inheritance of classes and pointer types, these are all frequently used in VC programming.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design