C++學習day3之默認參數

#include<iostream>
#include<time.h>
using namespace std;
//這個世界上很多東西都是有默認狀態的。
//這裏可以給個默認參數 爲2.5;
//後面你調用的時候不傳參數就是默認參數。 
//



//對於一個參數來說
void Weather_Cast(string weather = "rainy")
{
    time_t t=time(0);//代表1970,0,0,0距今天你調用這個函數過了多少秒
    char tmp[64];
    strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A",localtime(&t));
    cout<<tmp<<" today is weather "<<weather<<endl;
}

//對於多個參數
//默認的規則是從右向左默認,並且中間不能跳躍
int volume(int l,int w=1,int h=3)
{
    return l*w*h;
}



//天氣有 rainy sunshine windy snowy flogy pm2.5
int main()
{
    Weather_Cast("pm2.5");
    Weather_Cast();
    cout<<volume(1)<<endl;
    return 0; 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章