小工具知识网

当前位置: 首页 > 行业知识 > C++必修:从C语言到C++的过渡(下)

行业知识

C++必修:从C语言到C++的过渡(下)

2026-10-31 23:22:48 来源:互联网转载

✨✨ 欢迎大家来到贝蒂大讲堂✨✨

养成好习惯,先赞后看哦~

所属专栏:C++学习

贝蒂的主页:Betty’s blog

1. 缺省参数

1.1. 缺省参数的使用

缺省参数是声明或定义函数时为函数的参数指定一个缺省值。在调用该函数时,如果没有指定实参则采用该形参的缺省值,否则使用指定的实参。

void func(int a = 0){cout func();  // 没有传参时,使用参数的默认值,输出0func(1); // 传参时,使用指定的实参,输出1return 0;}cout func();//不穿参数func(10,20);//半传参数func(10, 20, 30);//全传return 0;}cout func(10,20);//半传参数cout cout }return a + b;}double Add(double a, double b){return a + b;}return a + b;}int Add(int a, int b, int c){return a + b;}return a + c;}int Add(int a, char c){return a + c;}return a + b;}double Add(int a,int b)//error{return a + b;}return a + b;}int Add(int a=1, int b=2)//error{return a + b;}return a + b;}void func(int a, double b, int* p){}int main(){Add(1, 2);func(1, 2, NULL);return 0;}int a = 1;int& b = a;//引用cout const int a = 1;//常变量const int& b = a;//rightint& c = a;//errorint c = 2;const int& d = c;//rightdouble pi = 3.14;int& e = pi;//error//pi是浮点型,赋值给整型类型会发生隐式类型//这个隐式类型转换的值是个常变量const int& f = pi;return 0;}int tmp = 0;tmp = a;a = b;b = tmp;}static int n = 0;n++;return n;}int c = a + b;return c;}int main(){int& ret = Add(1, 2);Add(3, 4);cout int c = a + b;return c;}int main(){int ret = Add(1, 2);Add(3, 4);cout  int a[10000]; };void TestFunc1(A a) {}void TestFunc2(A& a) {}void TestRefAndValue(){A a;// 以值作为函数参数size_t begin1 = clock();for (size_t i = 0; i 

上一篇:如何在 Angular 中使用 ng-container 元素

下一篇:手机谷歌怎么更换服务器?