#include "iostream"
using namespace std;intfunc(int a,int b =20,int c =30){return a+b+c;}//1.如果某个位置已经有了默认参数,在这个参数之后都必须有默认参数intfunc2(int a,int b =10,int c =10){return a+b+c;}//b有默认参数,则c也需要有默认参数//2.如果函数的声明有默认参数,函数实现就不能有默认参数//声明和实现只能有一个有默认参数intfunc3(int a,int b,int c =10);intfunc3(int a,int b,int c){return a+b+c;}//若传入了值,则用传入的,否则用默认值intmain(){
system ("pause");return0;}
函数占位参数
intfunc(int a,int){
cout <<"this is func"<< endl;}//传入参数时需要传入两个参数//目前阶段用不到占位参数,后面会用到//占位参数还可以有默认参数
函数重载
#include "iostream"
using namespace std;/*
函数重载的条件
1.在同一作用下
2.函数名相同
3.函数参数类型不同,或者个数不同,或者顺序不同
*/intfunc(double a){
cout <<"this is func"<< endl;}intfunc(int a){
cout <<"this is func chongzai"<< endl;}intmain(){
system ("pause");return0;}
注意事项:
1.函数的返回值不可以作为函数重载的条件
2.引用作为重载的条件
#include "iostream"
using namespace std;voidfunc(int&a){
cout <<"this is func"<< endl;}voidfunc(constint&a){
cout <<"this is func chongzai"<< endl;}intmain(){int a =2;func(a);//调用的是形参为int的函数func(10);//调用的是形参为const int的函数
system ("pause");return0;}
3.函数重载碰到默认参数
#include "iostream"
using namespace std;voidfunc(int a,int b =10){
cout <<"this is func"<< endl;}voidfunc(int a ){
cout <<"this is func chongzai"<< endl;}intmain(){func(10);//有默认参数和没有默认参数的函数都需要传入一个参数//故会报错
system ("pause");return0;}
函数高级函数默认参数#include "iostream"using namespace std;int func(int a, int b = 20, int c = 30){ return a+b+c;}//1.如果某个位置已经有了默认参数,在这个参数之后都必须有默认参数int func2(int a, int b = 10, int c = 10){ return a+b+c;}//b有默认参数,则c也需要有默认参数//2.如果函数的声明有默认参数,函数实现就不能有默认参