函数重载
函数重载概述
作用:函数名可以相同,提高复用性
函数重载条件:
同一个作用域下
函数名称相同
函数参数类型不同或者个数不同或者顺序不同
注意:函数的返回值不可以作为函数重载的条件
代码示例:
#include <iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性
//函数重载条件:
//*
//同一个作用域下
//*
//函数名称相同
//*
//函数参数类型不同或者个数不同或者顺序不同
//
void func()
{
cout <<"func的调用" <<endl;
}
void func(int a)
{
cout << "func(int a)的调用" << endl;
}
void func(double a)
{
cout << "func(double a)的调用" << endl;
}
void func(double a,int b)
{
cout << "func(double a,int b)的调用" << endl;
}
//注意事项
//函数的返回值不可以作为函数重载的条件
//int func(double a, int b)
//{
// cout << "func(double a,int b)的调用" << endl;
//}
int main()
{
//func();
//func(10);
//func(3.14);
func(3.14, 10);
system("pause");
return 0;
}
大家有兴趣的可以关注一下微信公众号