编写重载函数可分别求取两个整数,三个整數,两个双精度数,三个双精度数的最大值。并在主函数中分别测试。
时间: 2023-04-24 12:00:50 浏览: 73
可以编写重载函数来分别求取两个整数、三个整数、两个双精度数、三个双精度数的最大值。
以下是示例代码:
```c++
#include <iostream>
using namespace std;
int max(int a, int b) {
return a > b ? a : b;
}
int max(int a, int b, int c) {
return max(max(a, b), c);
}
double max(double a, double b) {
return a > b ? a : b;
}
double max(double a, double b, double c) {
return max(max(a, b), c);
}
int main() {
int a = 10, b = 20, c = 30;
double d = 1.23, e = 4.56, f = 7.89;
cout << "max of " << a << " and " << b << " is " << max(a, b) << endl;
cout << "max of " << a << ", " << b << " and " << c << " is " << max(a, b, c) << endl;
cout << "max of " << d << " and " << e << " is " << max(d, e) << endl;
cout << "max of " << d << ", " << e << " and " << f << " is " << max(d, e, f) << endl;
return ;
}
```
输出结果:
```
max of 10 and 20 is 20
max of 10, 20 and 30 is 30
max of 1.23 and 4.56 is 4.56
max of 1.23, 4.56 and 7.89 is 7.89
```
在主函数中,我们分别测试了两个整数、三个整数、两个双精度数、三个双精度数的最大值。