1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <iostream> // 输出流库 using namespace std; // 使用标准命名空间 // 课程:03 函数高级 - 函数重载 - 基础语法 /* 函数重载 可以让函数名相同,提高复用性 函数重载的满足条件 1、必须同一个作用域下 2、函数名称相同 3、函数的参数类型不同、或者个数不同、或者顺序不同 */ // 没有参数 void func() { cout << "func 的调用" << endl; } // 只有int参数 void func(int a) { cout << "func int的调用" << endl; } // 只有double参数 void func(double a) { cout << "func double的调用" << endl; } // 参数(double a,int b) void func(double a, int b) { cout << "func 参数(double a,int b)的调用" << endl; } // 参数(int a,double b) void func(int a, double b) { cout << "func 参数(int a,double b)的调用" << endl; } // 注意事项,返回值不能作为函数重载的条件 // int func(int a, double b) // { // cout << "func 参数(int a,double b)的调用" << endl; // } // 程序入口函数 int main() { system("chcp 65001"); // 临时设置UTF-8编码 func(); // 没有参数 func(10); // 只有int参数 func(3.14); // 只有double参数 func(3.14, 10); // 参数(double a,int b) func(10, 3.14); // 参数(int a,double b) system("pause"); // 控制台暂停,等待下一步操作 return 0; // 结束返回值:0 } // |
12.3 函数高级-函数重载-基础语法
未经允许不得转载:Ai分享 » 12.3 函数高级-函数重载-基础语法