C++中的引用和高级函数(下)

简介: C++中的引用和高级函数(下)

函数提高

函数的的默认参数

在C++中,函数的形参列表中的形参是可以有默认值的。
语法: 返回类型    函数名  (参数 = 默认值)
         {
                    函数体        
         }

示例:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20)
{
   
 return a + b + c;
}
int main()
{
   
 cout << func(10) << endl;
 system("pause");
 return 0;
}

输出结果:60
如果我们自己传入数据,就使用自己的数据,如果没有,那就用默认值。
如下:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20)
{
   
    return a + b + c;
}
int main()
{
   
    cout << func(10,100) << endl;
    system("pause");
    return 0;
}

输出结果:130
注意事项:

1.如果某个位置已经有了,默认参数,那么从这个位置往后,从左往右都必须有默认值

如下:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c,int d)   //这是错的
{
   
    return a + b + c;
}

上面代码就是错误案例。

2.如果函数声明有默参数,函数实现就不能有默认参数了
#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20,int d=30);
int func(int a, int b ,int c,int d)   //此处就不能有默认参数了,因为声明的时候已经写了默认值参数值了
{
   
    return a + b + c;
}

错误案例:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20,int d=30);
int func(int a, int b=30, int c=20,int d=30) //此处就是错误的
{
   
    return a + b + c;
}

函数的占位参数

C++中函数的形参列表里可以有占位参数,用来占位,调用函数时必须填补该位置

语法:返回值类型    函数名 (数据类型)   {   }

如下:

#include<iostream>
using namespace std;
void func(int a, int)  //此时就出现的是占位参数
{
   
    cout << a << endl;
}
int main()
{
   
    func(10,30);
    system("pause");
    return 0;
}

占位参数也可以有默认参数
如下:

#include<iostream>
using namespace std;
void func(int a, int = 10)  //此时的占位参数就是一个默认参数
{
   
    cout << a << endl;
}
int main()
{
   
    func(10);
    system("pause");
    return 0;
}

函数重载

函数重载概述

 作用:函数名可以相同,提高复用性

函数重载满足条件:

1.同一个作用域下
2.函数名称相同
3.函数参数类型不同,或者个数不同或者顺序不同

**注意:函数的返回值不可以作为函数重载的条件**

示例:

#include<iostream>
using namespace std;
void func()
{
   
    cout << "func()的调用" << endl;
}
void func(int a)
{
   
    cout << "func(int a)的调用!" << endl;
}
int main()
{
   
    func();
    func(10);
    system("pause");
    return 0;

执行结果:
在这里插入图片描述
分析:
在这里插入图片描述
函数重载注意事项:

1.引用作为重载条件
2.函数重载碰到函数默认参数

1.引用作为重载条件

#include<iostream>
using namespace std;
void func(int &a)
{
   
    cout << "func(int &a)的调用" << endl;
}
void func(const int& a)
{
   
    cout << "func(int a)的调用" << endl;
}

这样也是满足函数重载的
在后面主函数传参数,假如传一个变量

int a=10;
func(a)

这样会调用

void func(int &a)

如果传一个常量

func(10);

会调用

void func(const int& a)

2.函数重载碰到函数默认参数

#include<iostream>
using namespace std;
void func(int a,int b=10)
{
   
    cout << "func(int a, int b)的调用" << endl;
}
void func(int a)
{
   
    cout << "func(int a)的调用" << endl;
}
int main()
{
   
    int a = 10;
    func(a);   //此时编译器会报错,编译器会不知道调用哪一个函数
    system("pause");
    return 0;
}

所以,在我们使用函数重载时,要避免使用默认参数,避免歧义。

相关文章
|
6天前
|
并行计算 调度 C++
|
1天前
|
存储 C++
c/c++宏定义(函数)
c/c++宏定义(函数)
|
2天前
|
存储 安全 编译器
【C++入门】缺省参数、函数重载与引用(下)
【C++入门】缺省参数、函数重载与引用
|
2天前
|
编译器 C语言 C++
【C++入门】缺省参数、函数重载与引用(上)
【C++入门】缺省参数、函数重载与引用
|
3天前
|
编译器 C++
【C++进阶】引用 & 函数提高
【C++进阶】引用 & 函数提高
|
7天前
|
C++
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
|
7天前
|
存储 C++
C++从入门到精通:2.1.1函数和类
C++从入门到精通:2.1.1函数和类
|
8天前
|
存储 人工智能 编译器
【重学C++】【引用】一文看懂引用的本质与右值引用存在的意义
【重学C++】【引用】一文看懂引用的本质与右值引用存在的意义
19 0
|
15天前
|
机器学习/深度学习 定位技术 C++
c++中常用库函数
c++中常用库函数
38 0
|
1天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-3
c++初阶------类和对象(六大默认构造函数的揭破)