//函数提高
/**********************************************************************************
函数默认参数:在C++中,函数的形参列表中的形参是可以有默认值的
语法:return type function name(parameter list default values) 返回值类型 函数名(参数列表默认值)
注意:
a:如果某个位置已经有了默认参数,那么这个位置之后,从左到右都必须有默认值
b:如果函数声明有默认参数,函数实现就不能有默认参数。
c:正常默认参值写在声明中
***********************************************************************************
例程:
#include <iostream>
using namespace std;
int func(int a, int b=10 , int c=10 );
int fun2(int a = 20, int b = 10);
int main()
{
int a = func(20);//默认值参数没有传实参就是用默认值
cout << a << endl;
int b = func(20,50,20);//默认值参数传实参就是用传的实参
cout << b << endl;
getchar();
return 0;
}
//函数默认参数
int func(int a,int b,int c)
{
return a + b + c;
}
//int fun1(int a,int b=10,int c,int d)//错误,如果某个位置已经有了默认参数,那么这个位置之后,从左到右都必须有默认值
//int fun2(int a = 20, int b = 10)//声明和实现都有默认参数会出错。
int fun2(int a, int b)
{
return 0;
}
**********************************************************************************/
/**********************************************************************************
函数占位参数:C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
语法:return type function name (parameter list ,data type)
在现阶段函数的占位参数存在意义不大,但是后面的课程中会用到该技术
***********************************************************************************
例程:
#include <iostream>
using namespace std;
void func(int a, int);
void func1(int a, int = 10);
int main()
{
func(10, 10);
func1(10);
getchar();
return 0;
}
void func(int a,int)
{
cout << "this is func" << endl;
}
//占位参数,还可以有默认参数
void func1(int a, int)
{
cout << "this is func" << endl;
}
**********************************************************************************/
/**********************************************************************************
函数重载:函数名可以相同,提高复用性
函数重载满足条件:
a:同一个作用域下
b:函数名称相同
c:函数参数类型不同或者个数不同或者顺序不同
注意:函数的返回值不可以作为函数重载的条件,float传值加后缀:f,double传值加后缀:lf
***********************************************************************************
***********************************************************************************
***********************************************************************************
*例程:
#include <iostream>
using namespace std;
int func(double a, float b);
//int func(float b, double a);与下一个类型一致
int func(float a, double b);
int func(int a);
void func();
int main()
{
getchar();
return 0;
}
//int func(1.02lf, 2.01f);
int func(double a, float b)
{
cout << "int func(double a, float b)的调用" << endl;
return 0;
}
//函数“int func(float, double)
//int func(float b, double a)
//{
// cout << "int func(float b, double a)的调用" << endl;
// return 0;
//}
//int func(1.02f, 2.01lf);
//函数“int func(float, double)
int func(float a,double b)
{
cout << "int func(float a,double b)的调用" << endl;
return 0;
}
int func(int a)
{
cout << "int func(int a)的调用" << endl;
return 0;
}
void func()
{
cout << "void func的调用" << endl;
}
错误提示:
无法重载仅按返回类型区分的函数
“void func(void)”: 重载函数与“int func(void)”只是在返回类型上不同
func” : 重定义;不同的基类型
//int func()
//{
// cout << "int func()的调用" << endl;
// return 0;
//}
//
//void func()
//{
// cout << "void func()的调用" << endl;
// return ;
//}
**********************************************************************************/
/**********************************************************************************
函数重载注意事项:
引用作为函数重载条件
函数重载碰到函数默认参数
***********************************************************************************
例程:
#include <iostream>
using namespace std;
void func(const int &a);
void func(int &a);
void fun2(char a);
void fun2(char a, int b = 10);
int main()
{
int a = 10;
// func(a);//void func(int &a)
// func(10);//void func(const int &a)
//函数重载碰到默认参数会出问题func2(10);
getchar();
return 0;
}
void func(int &a)
{
cout << "void func(int &a)的调用" << endl;
}
void func(const int &a)
{
cout << "void func(const int &a)的调用" << endl;
}
void fun2(char a, int b )
{
cout << "int fun2(int a, int b = 10)的调用 " << endl;
}
void fun2(char a)
{
cout << "int fun2(int a)的调用 " << endl;
}
**********************************************************************************/
函数提高
最新推荐文章于 2021-11-15 09:09:35 发布