編寫程序,其中包含三個重載的display()函數。第一個函數輸出一個double值,前面用字符串“A double:”引導;第二個函數輸出一個int值,前面用字符串“A int:”引導;第三個函數輸出一個char字符,前面用字符串“A char:”引導。在主函數中,分別用double,float,int,char和short型變量去調用display()函數,并對結果做簡要說明
1 回答

MM們
TA貢獻1886條經驗 獲得超2個贊
#include <iostream.h>
void Display(int INT)
{
cout<<"A int:"<<INT<<endl;
}
void Display(char CHAR)
{
cout<<"A char:"<<CHAR<<endl;
}
void Display(double DOUBLE)
{
cout<<"A double:"<<DOUBLE<<endl;
}
void main()
{
int a=20;
short b=10;
char c='m';
double d=1;
float e=2;
Display(a);
Display(b);
Display(c);
Display(d);
Display(e);
}
重載主要是參數個數或者參數類型不同,就構成重載,對于不存在的匹配進行隱式類型轉換,short->int,float->double,因此short匹配int,float匹配到double
- 1 回答
- 0 關注
- 1325 瀏覽
添加回答
舉報
0/150
提交
取消