编写重载函数myMax可分别求取两个整数,三个整数,两个双精度数的最大值。
时间: 2023-08-06 09:03:46 浏览: 48
以下是myMax函数的重载实现:
```c++
#include <iostream>
using namespace std;
int myMax(int a, int b) {
return (a>b) ? a : b;
}
int myMax(int a, int b, int c) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
double myMax(double a, double b) {
return (a>b) ? a : b;
}
int main() {
int a = 5, b = 10, c = 15;
double d = 2.3, e = 5.6;
cout << "Max of " << a << " and " << b << " is " << myMax(a, b) << endl;
cout << "Max of " << a << ", " << b << " and " << c << " is " << myMax(a, b, c) << endl;
cout << "Max of " << d << " and " << e << " is " << myMax(d, e) << endl;
return 0;
}
```
输出结果:
```
Max of 5 and 10 is 10
Max of 5, 10 and 15 is 15
Max of 2.3 and 5.6 is 5.6
```