标签:reload) const std color fun include reload iostream clu
对于相同的函数名字,根据其输入的变量不同进行函数重载
/* 根据函数的输入变量不同进行函数重载 */ #include <iostream> using namespace std; void foo(int i) { cout << "int foo(int i)" << endl; } void foo(int i, int j) { cout << "void foo(int i, int j)" << endl; } void foo(int i, double j) { cout << "void foo(int i, double j)" << endl; } int main(void) { int a = 0; double b = 0; foo(a, b); return 0; }
对于函数重载,如果char->int 表示向上采样, short -> char 表示向下采样
/* 变量不匹配进行重载 */ #include <iostream> using namespace std; void func(int i) { // char - > int cout << "func(1)" << endl; } void func(const char i) { // char - > const char cout << "func(2)" << endl; } //sort->char 降级匹配 void foo(char i) { cout << "foo(1)" << endl; } //sort->int 升级匹配 void foo(int i) { cout << "foo(2)" << endl; } //省略匹配 void hum(int i, ...) { cout << "hum(1)" << endl; } //double->int 降级匹配 void hum(int i, int j) { cout << "hum(2)" << endl; } int main() { char c = 2; func(c); short s = 2; foo(s); int i = 0; double j = 0; hum(i, j); }
在开头进行函数声明,同时定义变量,使得未输入的函数有初始值
/* 进行函数声明和变量预定义 */ #include <iostream> using namespace std; //函数预声明 void func(int a = 10, int b = 10, int c = 20); int main(void) { func(11, 20, 30); func(11, 20); func(11); } void func(int a, int b, int c) { cout << a << "," << b << "," << c << endl; }
C++-函数重载(reload), 函数定义声明(void func(int))
标签:reload) const std color fun include reload iostream clu
原文地址:https://www.cnblogs.com/hyq-lst/p/12598823.html