#include <iostream>#include<iomanip>#include<windows.h>#include<stdio.h>#include<time.h>using namespacestd;voidWeathercast (stringWeather="PM2.5")//functions that contain default parameters{time_t T=time (0); Chartmp[ -]; Strftime (TMP,sizeof(TMP),"%y/%m/%d%x%A", LocalTime (&t)); cout<<tmp<<"Today is weather"<<weather<<Endl;}intMain () {cout<<"Hello Word"<<Endl; Weathercast ("Sunshine"); Weathercast (); return 0;}
Multiple parameters:
#include <iostream>#include<iomanip>#include<windows.h>#include<stdio.h>#include<time.h>using namespacestd;/**function:*description: The size of the cube *input:*output:*return:*others:c++ default parameters from right to left default, the number of arguments in the middle cannot jump + the number of default arguments must be greater than or equal to the number of formal parameters */intVolumeintLintw=3,intH=5){ returnl*w*h;}intMain () {cout<<volume (1) <<Endl; return 0;}
Special cases
#include <iostream>#include<iomanip>#include<windows.h>#include<stdio.h>#include<time.h>using namespacestd;//a function that cannot be used as both an overload and a default parameter. When you write less than one parameter, the system cannot confirm that it is overloaded or the default parametervoidPrintinta) {printf ("void print (int a)");}voidPrintintAintb=Ten) {printf ("void print (int a,int b=10)");}intMain () {print (Ten); return 0;}
The compiler will then error
C + + default parameters