您的位置:首页 > 其它

面向对象程序设计上机练习一(函数重载)

2017-10-25 16:35 239 查看
Problem Description

利用数组和函数重载求5个数最大值(分别考虑整数、单精度、长整数的情况)。

Input

分别输入5个int型整数、5个float 型实数、5个long型正整数。

Output

分别输出5个int型整数的最大值、5个float 型实数的最大值、5个long型正整数的最大值。

Example Input

11 22 666 44 55

11.11 22.22 33.33 888.88 55.55

1234567 222222 333333 444444 555555

Example Output

666

888.88

1234567

函数重载:名字相同,但返回值、参数类型、数量不同

如果不懂看具体解释:http://blog.csdn.net/gx17864373822/article/details/78343282

#include <iostream>
using namespace std;
int Max(int a[])//定义函数如果不声明就写在main函数之前
{
int max=a[0];
for(int i=1; i<5; i++)
{
if(a[i]>max)
max=a[i];
}
return max;
}
float Max(float b[])
{
float max=b[0];
for(int i=1; i<5; i++)
{
if(b[i]>max)
max=b[i];
}
return max;
}
long Max(long c[])
{
long max=c[0];
for(int i=1; i<5; i++)
{
if(c[i]>max)
max=c[i];
}
return max;
}
int main()
{
int a[6];
float b[6];
long c[6];//long型
int m1;//分别输出的最大值
float m2;
long m3;

for(int i=0; i<5; i++)
cin>>a[i];
m1=Max(a);//调用函数,实参只写数组的名字代表数组首地址
cout<<m1<<endl;

for(int i=0; i<5; i++)
cin>>b[i];
m2=Max(b);
cout<<m2<<endl;

for(int i=0; i<5; i++)
cin>>c[i];
m3=Max(c);
cout<<m3<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: