C + + default parameters

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.