Function overloading and function with default parameters

Function overloading and function with default parameters

Function overloading

  Reload function ( function overload ), the function is a special case, which is an important form of C ++ function, which is a form of the same function name, but the order of the number of parameters or parameter or parameters are not the same type, each of which function perform different functions.

  Conditions: the same function name, the number of parameters or parameter or parameter types are not the same as the order of (three points may be provided at the same time).

  Usage: argument type parameters to match the shape function

  Below is an example of simple

 

 1 //函数重载
 2 #include <iostream>
 3 using namespace std;
 4 
 5 void fun(int a,int b){
 6     cout<<a<<endl;
 7 }
 8 void fun(int a,int b,int c){
 9     cout<<c<<endl;
10 }
11 void fun(char a,char b){
12     cout<<b<<endl;
13 }
14 
15 void test(){
16     fun(3,4);
17     fun('E','F');
18     fun(2,5,7);
19 }
20 int main(){
21     test();
22     return 0;
23 }

 Overloading make the program more detail, master function overloading will make your program more refined and observability

 

Function with default parameters

We are talking about two types of parameters, parameter and arguments with default parameters here parameter function is actually an argument.

Shaped like:

 

1 void func(int a = 1,int b = 3,int c = 5){
2     cout<<a+b+c<<endl;
3 }

 

 As shown in the code above, the default value of the parameter set, which is a function with default parameters

Function with default parameters are the following characteristics, here I come them out.

1. Call the function FUNC (), if the argument value is not specified, the program uses the value of the parameter code execution

1  void FUNC ( int A = 1 , int B = . 3 , int C = . 5 ) {
 2      COUT A + B + C << << endl;
 . 3  }
 . 4  int main () {
 . 5      FUNC ();     // Output 1 . 3. 5 + + 
. 6      FUNC ( 2 );     // output +. 3. 5 + 2 
. 7      FUNC ( 2 , . 4 );         // output +. 4. 5 + 2 
. 8      FUNC ( . 4 , . 7 , . 8 );     //+ +. 7. 8 outputs. 4 
. 9      
10      return  0 ;
 . 11 }

2. If the function declaration which specifies the default parameter, the function parameter settings may not be re-defined as the ratio default parameters as follows:

. 1  void FUNC ( int A = . 1 , int B = 3 , int C = . 5 )
 2  int main () {
 3      FUNC ();     // Output + 3 +. 5. 1 
. 4      FUNC ( 2 );     // Output 2 + 3 +5 
. 5      FUNC ( 2 , . 4 );         // output +. 4. 5 + 2 
. 6      FUNC ( . 4 , . 7 , . 8 );     // output. 7 + +. 8. 4 
. 7      
. 8      return  0 ;
 9 }
10 void func(int a,int b,int c){
11     cout<<a+b+c<<endl;
12 }

 

 3. Set the default parameter, it should follow the principle of the right, i.e., from right to left setting parameter should be as follows:

 

 

. 1  void FUNC ( int X = . 1 , int Y); // Error 
2  void FUNC ( int X = . 1 , int Y = 2 ); // correct

 

 Popular speaking, when a parameter list a default value is set variable, the value of all of its right should be set defaults

 

 

This is a brief description of the use of function overloading and function with default parameters, I also was learning stage, any errors or impropriety, please ask, hope you will support!

Guess you like

Origin www.cnblogs.com/whtmomo/p/11296640.html