º¯ÊýµÄĬÈϲÎÊý£¬¶ÔÓÚ¶¨ÒåµÄº¯ÊýÊÇ¿ÉÒÔÉèÖÃĬÈϲÎÊýµÄ£¬ÕâÑùµ±ÔÚµ÷Óõĺ¯ÊýµÄʱºòÊÇ¿ÉÒÔ¸ù¾Ýº¯ÊýËùÉèÖõÄĬÈϲÎÊýÀ´½øÐвÎÊý´«µÝ¡£µ±ÓÐЩÊý¾ÝÔÚʹÓõĹý³ÌÖÐÓëÉèÖúõIJÎÊýÏàͬ£¬ÄÇô¾Í¿ÉÒÔ²»ÓÃΪº¯Êý´«µÝʵ²Î£¬´Ëʱ½«Ê¹Óú¯ÊýĬÈϵIJÎÊý¡£ÀýÈ磺
void func(int num1,int num2 = 3,char ch = '*'){}
void func(int num1=2,int num2,char ch='+');//´íÎó£¬Èç¹ûÓÐÒ»¸ö²ÎÊýÉèÖÃĬÈÏÖµÄÇôÆäºóÃæµÄËùÓвÎÊý¶¼ÒªÉèÖÃĬÈÏÖµ£»ÀýÈ磺
void func(int num1,int num2=1,char ch='+')£»//´ËÐÎʽÊǺϷ¨µÄ£»
×¢Òâµã£º
1. Ò»µ©¸øÒ»¸ö²ÎÊý¸³ÁËĬÈÏÖµ£¬ÔòËüºóÃæµÄËùÓвÎÊýÒ²¶¼±ØÐëÓÐĬÈÏÖµ¡£
2. ĬÈÏÖµµÄÀàÐͱØÐëÕýÈ·¡£
3. ĬÈÏÖµ¿ÉÒÔÔÚÔÐÍ£¨º¯ÊýÉùÃ÷£©»òÕߺ¯Êý¶¨ÒåÖиø³ö£¬µ«²»ÄÜÔÚÁ½¸öλÖÃͬʱ¸ø³ö£¬½¨ÒéÔÚÔÐÍÉùÃ÷ÖÐÖ¸¶¨Ä¬ÈÏÖµ¡££¨Èç¹û×Óº¯Êý¶¨ÒåÔÚÖ÷º¯ÊýµÄÏÂÃ棬ÔÚº¯Êý¶¨ÒåÖÐÉèÖÃĬÈϲÎÊýÊDz»ÐеÄ,»á±¨´íÕâÊÇÒòΪº¯ÊýÔÚÉùÃ÷µÄʱºò²¢Ã»ÓиæËßϵͳº¯ÊýµÄĬÈÏÖµÊÇʲô£¬³ÌÐòÔÚÔËÐеĹý³ÌÖУ¬²¢²»ÈÏʶ£¬ËùÒԻᱨ´í£¬µ«ÊÇÈç¹û°Ñº¯Êý¶¨Òå·ÅÔÚÖ÷º¯Êý֮ǰÊÇ¿ÉÒԵģ©£»
Óŵã:
Èç¹ûҪʹÓõIJÎÊýÔÚº¯ÊýÖм¸ºõ×ÜÊDzÉÓÃÏàͬµÄÖµ£¬ÔòĬÈϲÎÊý·Ç³£·½±ã
ͨ¹ýÌí¼Ó²ÎÊýÀ´Ôö¼Óº¯ÊýµÄ¹¦ÄÜʱ£¬Ä¬ÈϲÎÊýÒ²·Ç³£ÓÐÓÃ
#include <iostream> using namespace std; void func(int num1=1,int num2=1,char str='+'); void function1(int num1,int num2=1,char str='+'); int main() { func(2,13,'9'); //¶¼²»²ÉÓÃĬÈÏÖµ func(1); //µÚ¶þ¸öºÍµÚÈý¸ö²ÎÊý²ÉÓÃĬÈÏÖµ func(2,25); //µÚÈý¸ö²ÎÊý²ÉÓÃĬÈÏÖµ func(); //ËùÓÐÕâÈý¸ö²ÎÊý¶¼²ÉÓÃĬÈÏÖµ func(2,'0'); //µÚÈý¸ö²ÎÊý²ÉÓÃĬÈÏÖµ // func(2,,'o'); //´íÎó£¡ function1(1); return 0; } void func(int num1,int num2,char str) { //printf("num1=%d num2=%d str=%c\n",num1,num2,str); cout << "num1= " << num1 <<" "<< "num2= " << num2<<" "<< "str= " << str <<endl; }
void function1(int num1,int num2 ,char str) { cout << "-------------------------------"<<endl; cout << "num1= " << num1 <<" "<< "num2= " << num2<<" "<< "str= " << str <<endl; } |