函数名可以一样,就叫作函数重载
重载规则:1.函数名相同2.参数个数不同,参数类型不同,参数顺序不同,满足一个就可以重载。
返回值类型必须相同,不可以是void func(){} 和int func(){}
#include <iostream>
#include <string.h>
//函数重载
using namespace std;
int aabs(int a) //绝对值
{
return a>0?a:-a;
}
double aabs(double a)
{
return a>0?a:-a;
}//函数名可以一样,就叫作函数重载
//重载规则:1.函数名相同2.参数个数不同,参数类型不同,参数顺序不同,满足一个就可以重载。
//返回值类型必须相同,不可以是void func(){} 和int func(){}
int main()
{
cout << aabs(-5) << endl;
cout << aabs(5.6) << endl;
char buf[100];
char buf1[100];
strcpy(buf,"aaaaaa");//一般不考虑返回值
cout<<buf<<endl;
//int和float 4位
//double和long 8位
cout<<sizeof (10.2f)<<endl;//10.2f=(float) 强制变成float
cout<<sizeof (10.2)<<endl;//10.2是double
return 0;
}
默认参数,显示时间,见代码注释:
//默认参数
#include <iostream>
#include <time.h> //时间函数
using namespace std;
void WeaterTime(string w = "sunny") //"sunny"默认参数
{
cout << "weater is " << w << endl;
time_t t = time(0);
char tmp[64];
strftime(tmp,sizeof (tmp),"%Y/%m/%d/%X/%A",localtime(&t));
cout <<"time is " << tmp <<endl; // tmp时间
}
int main()
{
WeaterTime(); //不传参就用上面的默认参数
return 0;
}