Functions with default parameter values and function overloading

.Functions with default values
1.
When defining a function, you can set a predetermined value for the formal parameters of the function. When calling a function, if the actual parameter is given, the actual parameter value is used, otherwise the preset default is used Parameter value, such as:

void func(int a=3,int b=4)
{
    
    
	cout << a << " " << b<<endl;
}

int main()
{
    
    
	func();//使用默认参数
	func(1, 2);//使用形参
	//结果是3 4 1 2
}

Note:
1. Formal parameters with default parameters must be listed at the far right of the formal parameter list, that is, there can be no parameters without default values ​​to the right of the default parameter value, because the order of actual participation in the combination of formal parameters is from left to right when calling ,Such as:

int add(int x,int y=5,int z=6);//正确
int add(int x=1,int y=5,int z);//错误
int add(int x=1,int y,int z=6);//错误

2. When the function is called before the function definition, the default value needs to be given in the function declaration. At this time, the default value does not need to be given in the function definition.
If the default value is given when the function is declared, the default value is also given when the function is defined. The compiler may give an error message, so it is recommended to only give the default value when the function is declared to avoid confusion.

3. If the virtual function is redefined in class inheritance, then the default parameters in the virtual function should not be redefined. Because virtual functions are dynamically bound, but the default parameters are statically bound.

Function Overloading
1. Definition
Overloading is an important mechanism of C++ polymorphism, which is realized by static polymorphism, and it is the polymorphism realized in the compilation stage.
(By the way, polymorphism refers to the definition of different functions with one name. These functions perform different but similar operations, so that functions with different contents can be called with the same function name.)

2. Function
C++ allows functions with similar functions to be declared with the same function name in the same scope, thereby forming overloads. Easy to use and easy to remember.

3. Usage rules
1. When a function is overloaded, it cannot only have the same function type, and the number of parameters, types, and order are all the same;
because when the function is defined in this way, the compiler system cannot determine which function to use.

2. When a function is overloaded, the number of parameters, parameter types, and sequence of parameters are not allowed to be all the same;
after the same overloaded function is defined with the same parameters, the compiler system cannot determine which function to use.

3. When the function is overloaded, try to make the function similar or the same;
improve the readability of the program, and make it easier to understand and read.

4. The function cannot be both an overloaded function and a function with default parameters.
When no parameters are passed in, the system cannot determine which parameter to call.

Guess you like

Origin blog.csdn.net/qq_43530773/article/details/113786507