14-【每天亿点点C++,简单又有趣儿】函数、默认参数、重载

知识点:

  • 默认参数语法、注意事项
  • 函数占位参数
  • 函数重载

程序

#include<iostream>
#include<string>
using namespace std;

int funcAdd(int a ,int b=20 ,int c=30);
void funcA(int a,int,int);
void funcB();
void funcB(int a);
void funcE(int &a);
void funcE(const int &a);
void funcF(int a);
void funcF(int a,int b);

int main()
{
    cout << funcAdd(10)<<endl;   //默认参数学习
    funcA(12,12,12);             //函数的占位参数学习
    funcB();                     //函数重载【函数名相同、提高复用性】
    funcB(10);
    /*引用参数 的 函数重载*/
    int a = 10;
    funcE(a);
    funcE(10);

    /*默认参数 的 函数重载*/
    // funcF(10) //歧义 报错!
    funcF(10,10); //这个没问题,但是避免使用默认参数
}



int funcAdd(int a ,int b ,int c)
// 注意:
// 1、默认参数排列到后面 即: b有 c也得有
// 2、函数的【声明】和【实现】只能有一处给默认参数
{
    return a+b+c;
}

void funcA(int a,int ,int )
//语法:返回函数类型 函数名(数据类型,数据类型 =默认值)
{
    cout <<"函数A" << endl;
}
//函数重载的满足的条件
// 1-同一个作用域相同
// 2-函数名称相同
// 2-函数参数类型不同、或个数不同、或顺序不同
void funcB()
{
    cout <<"函数B01" << endl;
}
void funcB(int a)
{
    cout <<"函数B02" << endl;
}
void funcE(int &a)
{
    cout <<"函数E" << endl;
}
void funcE(const int &a)
{
    cout <<"函数const E" << endl;
}

void funcF(int a)
{
    cout <<"函数const F" << endl;
}
void funcF(int a,int b = 10)
{
    cout <<"函数 默认参数 F" << endl;
}

输出
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/magic_shuang/article/details/107590754
今日推荐