日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

函数提高

發(fā)布時(shí)間:2025/4/16 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 函数提高 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

函數(shù)默認(rèn)參數(shù)

#include <iostream> using namespace std;int func(int a, int b = 10, int c = 10) {return a + b + c; }//1. 如果某個(gè)位置參數(shù)有默認(rèn)值,那么從這個(gè)位置往后,從左向右,必須都要有默認(rèn)值 //2. 如果函數(shù)聲明有默認(rèn)值,函數(shù)實(shí)現(xiàn)的時(shí)候就不能有默認(rèn)參數(shù) int func2(int a = 10, int b = 10); int func2(int a, int b) {return a + b; }int main() {cout << "ret = " << func(20, 20) << endl;cout << "ret = " << func(100) << endl;system("pause");return 0; }


函數(shù)占位參數(shù)

C++中函數(shù)的形參列表里可以有占位參數(shù),用來做占位,調(diào)用函數(shù)時(shí)必須填補(bǔ)該位置

#include <iostream> using namespace std; //函數(shù)占位參數(shù) ,占位參數(shù)也可以有默認(rèn)參數(shù) void func(int a, int) {cout << "this is func" << endl; }int main() {func(10,10); //占位參數(shù)必須填補(bǔ)system("pause");return 0; }


函數(shù)重載

函數(shù)重載概述

作用:函數(shù)名可以相同,提高復(fù)用性

函數(shù)重載滿足條件:

  • 同一個(gè)作用域下

  • 函數(shù)名稱相同

  • 函數(shù)參數(shù)類型不同 或者 個(gè)數(shù)不同 或者 順序不同

注意: 函數(shù)的返回值不可以作為函數(shù)重載的條件

#include <iostream> using namespace std; //函數(shù)重載需要函數(shù)都在同一個(gè)作用域下 void func() {cout << "func 的調(diào)用!" << endl; } void func(int a) {cout << "func (int a) 的調(diào)用!" << endl; } void func(double a) {cout << "func (double a)的調(diào)用!" << endl; } void func(int a ,double b) {cout << "func (int a ,double b) 的調(diào)用!" << endl; } void func(double a ,int b) {cout << "func (double a ,int b)的調(diào)用!" << endl; }//函數(shù)返回值不可以作為函數(shù)重載條件 //int func(double a, int b) //{ // cout << "func (double a ,int b)的調(diào)用!" << endl; //}int main() {func();func(10);func(3.14);func(10,3.14);func(3.14 , 10);system("pause");return 0; }


函數(shù)重載注意事項(xiàng)

  • 引用作為重載條件

  • 函數(shù)重載碰到函數(shù)默認(rèn)參數(shù)

#include <iostream> using namespace std; //函數(shù)重載注意事項(xiàng) //1、引用作為重載條件void func(int &a) {cout << "func (int &a) 調(diào)用 " << endl; }void func(const int &a) {cout << "func (const int &a) 調(diào)用 " << endl; }//2、函數(shù)重載碰到函數(shù)默認(rèn)參數(shù)void func2(int a, int b = 10) {cout << "func2(int a, int b = 10) 調(diào)用" << endl; }void func2(int a) {cout << "func2(int a) 調(diào)用" << endl; }int main() {int a = 10;func(a); //調(diào)用無constfunc(10);//調(diào)用有const//func2(10); //碰到默認(rèn)參數(shù)產(chǎn)生歧義,需要避免system("pause");return 0; }

總結(jié)

以上是生活随笔為你收集整理的函数提高的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。