编写重载函数myMax可分别求取两个整数,三个整数,两个双精度数的最大值。
时间: 2023-08-06 16:04:10 浏览: 403
可以这样实现:
```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)
{
return myMax(myMax(a, b), c);
}
// 求两个双精度数的最大值
double myMax(double a, double b)
{
return a > b ? a : b;
}
int main()
{
int x = 5, y = 3, z = 7;
double a = 1.2, b = 3.4;
cout << "max of " << x << " and " << y << " is " << myMax(x, y) << endl;
cout << "max of " << x << ", " << y << " and " << z << " is " << myMax(x, y, z) << endl;
cout << "max of " << a << " and " << b << " is " << myMax(a, b) << endl;
return 0;
}
```
输出:
```
max of 5 and 3 is 5
max of 5, 3 and 7 is 7
max of 1.2 and 3.4 is 3.4
```