2.编写重载函数max1,可分别求2个整数、3个整数、2个双精度数、3个双精度数的最大值。
时间: 2024-06-10 16:04:05 浏览: 171
重载函数Max1可以分别求取两个整数、三个整数、两个双精度数和三个双精度数的最大值。在C++代码中,重载函数Max1的实现如下:
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
int Max1(int a, int b) {
return max(a, b);
}
int Max1(int a, int b, int c) {
return max(max(a, b), c);
}
double Max1(double a, double b) {
return max(a, b);
}
double Max1(double a, double b, double c) {
return max(max(a, b), c);
}
int main() {
int x, y, z;
cout << "Please enter two integers:\n";
cin >> x >> y;
cout << Max1(x, y) << endl;
cout << "Please enter three integers:\n";
cin >> x >> y >> z;
cout << Max1(x, y, z) << endl;
double a, b, c;
cout << "Please enter two double precision numbers:\n";
cin >> a >> b;
cout << Max1(a, b) << endl;
cout << "Please enter three double precision numbers:\n";
cin >> a >> b >> c;
cout << Max1(a, b, c) << endl;
return 0;
}
```
阅读全文