C++编程基础二 04-默认实参

 1 // C++函数和类 04-默认实参.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <limits>
 7 #include <array>
 8 #include <math.h>
 9 #include <string>
10 using namespace std;
11 
12 //默认实参:某些函数有这样一种形参。在函数的很多次调用中它们都被赋予一个相同的值。我们把这个反复出现的值称为函数的默认实参。
13 //调用含有默认实参的函数时,可以包含该实参,也可以省略该实参。
14 //对于有多个形参的函数,必须从右向左添加默认值。
15 void compare(int num1, int num2 =100);
16 void greet(string name="uimodel");
17 int main()
18 {
19     int a = 59;
20     int b = 120;
21     compare(a);
22     compare(b);
23 
24     greet();
25     greet("Nicky");
26     return 0;
27 }
28 
29 void compare(int num1, int num2) 
30 {
31     if (num1 > num2)
32     {
33         cout << num1 << "大于" << num2 << endl;
34     }
35     else if (num1 < num2)
36     {
37         cout << num1 << "小于" << num2 << endl;
38     }
39     else
40     {
41         cout << num1 << "等于" << num2 << endl;
42     }
43 }
44 
45 void greet(string name)
46 {
47     cout << name << ",Hello!" << endl;
48 }

猜你喜欢

转载自www.cnblogs.com/uimodel/p/9348535.html