Think:
1、重载函数要求:两个重载函数必须在下列一个或两个方面有所区别:
(1)函数有不同参数。
(2)函数有不同参数类型
2、C++运算符重载的相关规定:
(1)不能改变运算符的优先级;
(2)不能改变运算符的结合型;
(3)默认参数不能和重载的运算符一起使用;
(4)不能改变运算符的操作数的个数;
(5)不能创建新的运算符,只有已有运算符可以被重载;
(6)运算符作用于C++内部提供的数据类型时,原来含义保持不变。
参考自百度百科
SDUT:面向对象程序设计上机练习一(函数重载)
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
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
Hint
Author
zlh
以下为Accepted代码
#include <iostream>
using namespace std;
void Max_pri(int a[]);
void Max_pri(float b[]);
void Max_pri(long c[]);
int main(){
int i, a[14];
float b[14];
long c[14];
for(i = 0; i < 5; i++)
cin >> a[i];
for(i = 0; i < 5; i++)
cin >> b[i];
for(i = 0; i < 5; i++)
cin >> c[i];
Max_pri(a);
Max_pri(b);
Max_pri(c);
return 0;
}
void Max_pri(int a[]){
int t = a[0];
for(int i = 1; i < 5; i++){
if(t < a[i])
t = a[i];
}
cout <<t<< endl;
}
void Max_pri(float b[]){
float t = b[0];
for(int i = 1; i < 5; i++){
if(t < b[i])
t = b[i];
}
cout <<t<< endl;
}
void Max_pri(long c[]){
long t = c[0];
for(int i = 1; i < 5; i++){
if(t < c[i])
t = c[i];
}
cout <<t<< endl;
}
&spm=1001.2101.3001.5002&articleId=77970329&d=1&t=3&u=466dfde6895544509a44206f3da0c3f0)
815

被折叠的 条评论
为什么被折叠?



