I. Function default parameters
1. Default parameter: Just give him a default value when declaring a parameter of the function .
1#include <iostream>2 using namespacestd;3 4 voidShow (intA =1,intb =2,intc =3)5 {6cout << a <<Endl;7cout << b <<Endl;8cout << C <<Endl;9 }Ten One intMain () A { - Show (); - return 0; the}
2. The parameters of the function can be stored in the default value when you call it, you don't have to pass it.
But you can also pass the parameter to pass the default value overwrite pass a few all lines but the first one does not pass can not because this is a kind of pressure stack of the process of the back can
However, the default value before declaring a function can be
1#include <iostream>2 using namespacestd;3 4 voidShow (intAintb =2,intc =3)5 {6cout << a <<Endl;7cout << b <<Endl;8cout << C <<Endl;9 }Ten One intMain () A { -Show ( -); - return 0; the}
3. Summary:
The default parameter value must be in the right middle is not vacant
The default parameter must be written in the declaration function
Two. function overloading
function names in the same scope are different (that is, the number of parameters or the type of the parameter)
Depending on what type of argument is called, the corresponding function will be executed.
1#include <iostream>2 using namespacestd;3 4 voidArea (intR)5 {6cout << r*r*3.14<<Endl;7 }8 9 voidArea (intLinth)Ten { Onecout << l*h <<Endl; A } - - intMain () the { -Area (1); -Area (1,2); - return 0; +}
Three. References
1. Definition: A description of a particular data type in the C + + language can be understood as "nickname"
Used to specify the same address in different parts of the program using more than two variable names so that any one of these variable operations is performed on the same address unit
A variable name that is declared as a reference type on a relationship of more than two variable names is the alias of the actual variable name
Note: Define references must be initialized!!!
2. Reference operator:&
1#include <iostream>2 using namespacestd;3 intMain ()4 {5 intA = -;6 int& k =A;7K = $;8cout << a <<Endl;9 Ten return 0; One}
A reference operation is an operation on a referenced variable
The storage state of the target does not change when the reference is not a value that does not represent a storage space declaration
Once a reference is initialized, no more space can be re-referenced
3. the difference between a pointer and a reference:
The ① reference is defined to be initialized but the pointer is not
② cannot re-reference other space after the reference is initialized but the pointer can move
③ reference is not empty but has a null pointer
The reference must be guaranteed: a valid storage unit is referenced
4. Advantages of citation: space Efficiency and safety
5. How to select pointers and references when using:
① if you want to mount an address or have an address offset, use the pointer (new)
② if you want to operate a reference to a single space, you can
6.the "reference" is usually used as a function parameter so the way to pass the argument can be divided into the following three kinds:
① Value Delivery:
1#include <iostream>2 using namespacestd;3 4 voidChange (intXinty)5 {6 inttemp =x;7x =y;8y =temp;9 }Ten One intMain () A { - intA = -; - intb = $; the Change (A, b); - -cout << a <<Endl; -cout << b <<Endl; + - return 0; +}
Exchange two number swap x and y execution end space are recycled A and B are not exchanged
② Address Delivery:
1#include <iostream>2 using namespacestd;3 4 voidChange (int* x,int*y)5 {6 inttemp = *x;7*x = *y;8*y =temp;9 }Ten One intMain () A { - intA = -; - intb = $; theChange (&a,&b); - -cout << a <<Endl; -cout << b <<Endl; + - return 0; +}
Two space A and B interchanges that the indirect operation points to
③ Reference Delivery:
1#include <iostream>2 using namespacestd;3 4 voidChange (int& X,int&y)5 {6 inttemp =x;7x =y;8y =temp;9 }Ten One intMain () A { - intA = -; - intb = $; the Change (A, b); - -cout << a <<Endl; -cout << b <<Endl; + - return 0; +}
For the X and Y operations is a and B operations on A and B successful Exchange (PS: code to here really feel the reference really good convenient ah ~)
7. Summary: The space variable is to look at the life cycle to choose to control the life cycle will be selected heap area
You can see if you want to modify the incoming content without changing the value to pass the change with the address or reference
And then see if the new is not the address is the pointer is not an address to use the reference on it
<C++> function Default argument function overload reference