函数参数相关拓展

 1 #include <iostream>
 2 using namespace std;
 3 
 4 //一、默认参数
 5 void myPrint(int x = 3)
 6 {
 7     cout << "x" << x << endl;
 8 }
 9 //1 若填写参数,使用你填写的,若不填写,使用默认
10 //2 在默认参数规则里,如果默认参数出现 那么右边的都必须有默认参数
11 void myPrint2(int m, int n, int x = 3, int y = 4)
12 {
13     cout << "x" << x << endl;
14 }
15 
16 void main_1()
17 {
18     myPrint(4);
19     myPrint();
20     system("pause");
21     return;
22 }
23 
24 //二、占位参数
25 //函数占位参数 函数调用时必须写够参数
26 void func1(int a, int b, int)
27 {
28     cout << "a" << a <<" b"<<b<< endl;
29 }
30 void main_2()
31 {
32     //func1(1,2);
33     func1(1, 2, 3);
34     system("pause");
35     return;
36 }
37 
38 // 三、默认参数和占位参数
39 void func2(int a, int b, int = 0)
40 {
41     cout << "a=" << a << "b=" << b << endl;
42 }
43 void main()
44 {
45     func2(1, 2);//ok
46     func2(1, 2, 3);//ok
47     system("pause");
48     return;
49 }
原文地址:https://www.cnblogs.com/hnzsb-vv1130/p/6638599.html