C++中函數重載實例詳解。本站提示廣大學習愛好者:(C++中函數重載實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C++中函數重載實例詳解正文
C++中函數重載實例詳解
函數重載:
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
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!