日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C++深入分析講解函數(shù)與重載知識點_C 語言

作者:Bright-SKY ? 更新時間: 2022-07-30 編程語言

函數(shù)的默認(缺省)參數(shù)

1、默認參數(shù)的定義

c++在聲明函數(shù)原型的時可為一個或者多個參數(shù)指定默認(缺省)的參數(shù)值,當函數(shù)調(diào)用的時候如果沒有傳遞該參數(shù)值,編譯器會自動用默認值代替。

//函數(shù)的默認參數(shù) 指定x的默認值為10 y為20
int my_add(int x=10,int y=20)
{
    return x+y;
}
void test02()
{
    //如果函數(shù)傳參 那么各自的默認參數(shù)將無效
    cout<<"my_add = "<<my_add(100,200)<<endl;//300
    //如果某個參數(shù)未被傳參 將啟用默認值x=100 y使用默認值20
    cout<<"my_add = "<<my_add(100)<<endl;//120
    //x=10  y=20
    cout<<"my_add = "<<my_add()<<endl;//30
}

2、默認參數(shù)的注意點

(1)、函數(shù)的默認參數(shù)從左向右,如果一個參數(shù)設置了默認參數(shù),那么這個參數(shù)之后的參數(shù)都必須設置默認參數(shù)

//函數(shù)的默認參數(shù)從左向右
int func01(int x,int y=20,int z=30)
{
    return x+y+z;
}
void test03()
{
    cout<<func01(100,200)<<endl;//330
    cout<<func01(100)<<endl;//150
    //cout<<func01()<<endl;//err x沒有設置默認參數(shù) 必須傳參
}
int fun(int a, int b, int c=10);//正確
int fun(int a, int b=20, int c=10);//正確
int fun(int a=30, int b=20, int c=10);//正確
int fun(int a, int b=20, int c);//不正確

(2)、如果函數(shù)聲明和函數(shù)定義分開寫,函數(shù)聲明和函數(shù)定義不能同時設置默認參數(shù)

(3)、默認參數(shù)一般在函數(shù)聲明的時候 設置

fun.cpp

int func02(int x,int y,int z)
{
    return x+y+z;
}

main.cpp

//分文件 函數(shù)定義處的默認參數(shù) 是無效的
//建議:分文件是 在聲明 給默認參數(shù)
extern int func02(int x,int y=25,int z=35);
//extern int func02(int x,int y,int z);//err
void test04()
{
    cout<<func02(100,200)<<endl;//335
    cout<<func02(100)<<endl;//160
}

占位參數(shù)

c++在聲明函數(shù)時,可以設置占位參數(shù)。

占位參數(shù)只有參數(shù)類型聲明,而沒有參數(shù)名聲明。

如果函數(shù)的參數(shù)只有類型名 沒有形參名,這個參數(shù)就是占位參數(shù)

注意:

由于有類型名 所以 函數(shù)調(diào)用的時候 必須給占位參數(shù)傳參。

由于沒有形參名 所以 函數(shù)內(nèi)部 是無法使用占位參數(shù)。

1、占位參數(shù) 函數(shù)內(nèi)部無法使用

void func03(int x,int y,int)
{
    cout<<"x = "<<x<<", y = "<<y<<endl;
    return;
}
void test05()
{
    //func03(10,30,"hehe");//err "hehe"和int類型不符
    func03(10,30,40);
}

2、占位參數(shù) 可以設置成缺省參數(shù)

void TestFunc01(int a,int b,int=100)
{ 
    //函數(shù)內(nèi)部無法使用占位參數(shù) cout << "a + b = " << a + b << endl; 
}
TestFunc01(10, 20,30);//ok
TestFunc01(10, 20);//ok

什么時候用,在后面我們要講的操作符重載的后置++要用到這個

函數(shù)重載

函數(shù)重載體現(xiàn)了 c++ 的多態(tài)的特性

函數(shù)重載:同一個函數(shù)名在不同場景下可以具有不同的含義。

函數(shù)重載意義:方便的使用函數(shù)名。

函數(shù)重載的條件: 同一個作用域 參數(shù)個數(shù)不同 參數(shù)類型不同 參數(shù)順序不同(重要)

void myFunc(int a)
{
    cout<<"int的myFunc"<<endl;
}
void myFunc(int a,int b)
{
    cout<<"int,int 的myFunc"<<endl;
}
void myFunc(int a,double b)
{
    cout<<"int , double的myFunc"<<endl;
}
void myFunc(double a,int b)
{
    cout<<"double,int的myFunc"<<endl;
}
void test06()
{
    myFunc(10);//int
    myFunc(10,20);//int int
    myFunc(10,20.2);//int double
    myFunc(10.1,20);//double int
}

注意:

1、函數(shù)的返回值類型 不能作為 函數(shù)重載的依據(jù)。

2、函數(shù)重載和默認參數(shù)一起使用,需要額外注意二義性問題的產(chǎn)生

void myFunc02(int a)
{
    cout<<"int的myFunc02"<<endl;
}
void myFunc02(int a,int b=10)//默認參數(shù)
{
    cout<<"int,int 的myFunc02"<<endl;
}
void test07()
{
    //myFunc02(int a) 和 myFunc02(int a,int b=10)都能識別
    myFunc02(10);//二義性產(chǎn)生
}

3、函數(shù)重載的原理(了解)

原文鏈接:https://blog.csdn.net/qq_34981463/article/details/124993780

欄目分類
最近更新