1、string 字符串
遍历形式有两种:
(1) string s ="helloworld";
for(char ch : s){
cout<<ch<<;
}
cout<<endl;
(2)string s="welcometohqyj";
for(int i=0;i<s.size();i++){
cout<<s.at(i);
}
2、内联函数
定义:当函数调用的时候,函数体执行语句会取代调用的位置。减少系统调用时间,提高效率。
使用条件:
(1)函数代码少
(2)调用次数频繁
(3)函数运行时间短
3、默认值。
定义:函数允许有默认值,有默认值会提高灵活性。当调用函数不传参数时,会使用默认值,当传入参数时,会替代默认值
注意事项:
(1)当函数声明与实现分离的时候,只需在声明部分添加默认值。
(2)当函数声明部分有多个参数时,当中间一个参数添加默认值,其后的参数都要加入默认值。
4、函数重载
程序中有多个重名函数来处理相同或相似的问题,但是参数列表不能相同,包括参数个数和参数类型。
注意事项:
(1)不能通过返回值来判断函数重载
(2)载不能与函数默认值结合使用,易产生二义性。
#include <iostream>
using namespace std;
void getSalary(float base_salary);
void getSalary(float base_salary,float bonus);
int main()
{
getSalary(3500);
getSalary(3500,3000);
return 0;
}
void getSalary(float base_salary)
{
cout<<"普通文员工资:"<<base_salary<<endl;
}
void getSalary(float base_salary,float bonus)
{
cout<<"销售的工资:"<<base_salary+bonus<<endl;
}