C++内联函数与函数重载

本文介绍了C++中的内联函数,用于提高程序运行效率,强调了使用条件。接着讨论了函数默认值的概念和注意事项,如声明与定义分离时的处理。然后讲解了函数重载的基本概念,指出参数的顺序、个数、类型不同是重载的关键,返回值类型不能作为重载依据。文章通过示例代码加深了理解。
摘要由CSDN通过智能技术生成

目录

1.内联函数

1.1概念

1.2内联函数使用条件

2.函数默认值

2.1 默认值概念

2.2默认值注意事项

1.函数的声明和定义分离时,默认值只需加在声明处。

2.当函数有多个参数时,中间有一个参数有默认值,其后的参数都需要写默认值

练习1:编写一个message函数

运行结果:

 3.函数重载

3.1概念

练习

运行结果:


1.内联函数

1.1概念

内联函数只取代C语言中的宏定义define,编译时,将函数体内容展开到调用位置,节省函数调用的时间,提高程序的运行效率。

1.2内联函数使用条件

函数代码少(5行以下);

函数运行时间短;

调用次数频繁。

#include <iostream>
using namespace std;
inline void show(){
    cout<<"hello"<<endl;
}
int main()
{
    show(); //相当于直接在这里写 cout<<"hello"<<endl;
}

2.函数默认值

2.1 默认值概念

函数允许有默认值,当不传参数时,就会使用默认值;当传入参数时,默认值会被传入的参数所替代。

作用:默认值能够提高程序的灵活性

#include <iostream>
using namespace std;
void draw(string color="黑色"){//设置默认值为黑色
    cout<<"画笔颜色"<<color<<endl;
}
int main()
{
   draw();  //画笔颜色黑色 调用函数时不写参数会使用默认值
   draw("红色"); //画笔颜色红色  传入参数后默认值被参数替代
}

2.2默认值注意事项

1.函数的声明和定义分离时,默认值只需加在声明处。

#include <iostream>
using namespace std;
void draw(string color="黑色"); //只在声明处写默认值

int main()
{
   draw();  //默认值: 画笔颜色黑色
   draw("红色"); //替代默认值:画笔颜色红色
}

void draw(string color)  //定义
{
    cout<<"画笔颜色"<<color<<endl;
}

2.当函数有多个参数时,中间有一个参数有默认值,其后的参数都需要写默认值

#include <iostream>
using namespace std;
//下方错误 b有默认值 其后的参数也需要有默认值
void func(int a,int b=10,int c){
    cout<<a<<" "<<b<<" "<<c<<endl;
}

void func(int a,int b=10,int c=20){
    cout<<a<<" "<<b<<" "<<c<<endl;
}

int main()
{
    func(0);// 0 10 20
    func(1,2);// 1 2 20
}

练习1:编写一个message函数

参数有 年龄age ,姓名 name , 地址 address。 年龄默认是18 地址默认“山东"。函数输出以上参数信息

#include <iostream>
using namespace std;
void message(string name,int age=18,string address="山东"){
    cout<<name<<" "<<age<<" "<<address<<endl;
}
int main()
{
    message("小红");//小红 18 山东

}

比如调用 repeat(5,"hello") 会打印5行 hello

如果调用repeat(10);这时会打印10行的"OK"

#include <iostream>
using namespace std;
void repeat(int n,string str="OK")
{
    for(int i=1;i<=n;i++){
        cout<<str<<endl;
    }
}
int main()
{
    repeat(10);
    repeat(5,"hello");
}

运行结果:

 3.函数重载

3.1概念

函数重载:同一作用域(类),同名函数,参数的顺序、个数、类型不同都可以重载。函数的返回值类型不能作为重载条件。

#include <iostream>
using namespace std;
void show(int a){
    cout<<a<<endl;
}
void show(int a,int b){
    cout<<a<<" "<<b<<endl;
}
void show(string str){
    cout<<str<<endl;
}
//不能通过返回值不同区分函数重载
//string show(string str){
//    return str;
//}
int main()
{
    show(50);
    show(30,40);
    show("world");
}

练习

写一个计算工资的函数getSalary. 普通文员工资就是基本工资base_salary. 销售的工资包括基本工资base_salary和奖金bonus 函数重载形式给出这两个函数

#include <iostream>
    using namespace std;
    void getSalary(double base_salary)
    {
        cout<<"文员的工资"<<base_salary<<endl;
    }
    void getSalary(double base_salary,double bonus)
    {
        cout<<"销售的工资"<<base_salary+bonus<<endl;
    }
    int main()
    {
        getSalary(5000);
        getSalary(5000,1000);

    }

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值