哪位c++高手帮忙解答一道c++题目啊!

来源:百度知道 编辑:UC知道 时间:2024/05/27 01:41:02
题目:
编写程序,定义show()函数的两个重载函数。第一个输出一个int值,前面用字符串“int:“引导;第二个输出一个字符,前面用字符串“char:”引导。调用show()函数时,分别用int,float,char和short型变量进行测试,分析一下结果。

#include <iostream.h>
void show(int i)
{
cout<<"int:"<<i<<endl;
}
void show(char c)
{
cout<<"char:"<<c<<endl;
}
void main()
{
int i=5;
float f=4.0;
char c = 'a';
short s = 3;
show(i);
show(f);
show(c);
show(s);
}

分别一下分别输出什么,来看一下重载函数的入参会产生什么样的类型转换

#include <iostream>

using namespace std;
void show( int m )
{
cout<<"int:"<<m<<endl;
}
void show( char m )
{
cout<<"char:"<<m<<endl;
}
int main()
{
show( -11 );
show('a');
show( (short)(3) );

return 0;
}

如果你要对int,float,char,shot型都进行测试,那么重载函数要写四个,程序如下:
#include<iostream.h>
void show(int x)
{
cout<<"int:"<<x<<endl;
}
void show(f