您的位置:首页 > 编程语言 > C语言/C++

C++之函数重载

2016-02-16 13:43 204 查看
函数重载:

1、具有相同的名称,执行基本相同的操作,但是使用不同的参数列表。

2、函数具有多态性。

3、编译器通过调用时参数的个数和类型确定调用重载函数的哪个定义。

4、只有对不同的数据集完成基本相同任务的函数才应重载。

函数重载的优 点

1、不必使用不同的函数名

2、有助于理解和调试代码

3、易于维护代码

接下来直接上代码:

#include <iostream>

using namespace std ;

void say_hello(void)
{
cout << "this is hello" << endl ;
}

//数据类型不同的重载
void say_hello(int a = 100)
{
cout << "this is hotdog" << endl ;
}

int  say_hello(double a )
{
cout << "this is hotpig:" << a <<  endl ;
}
//参数个数不同的重载
int  say_hello(int a, int b, int c)
{
cout << "a+b+c = " << a+b+c << endl ;
}

int main(void)
{
say_hello(100);
say_hello(11.11);
say_hello(1 , 2 , 3);
return 0 ;
}</span>
执行结果:

this is hotdog
this is hotpig:11.11
a+b+c = 6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: