5.编写重载函数funmax可分别求两个int数、两个float数、两个double数的最大值。
时间: 2024-05-02 10:21:30 浏览: 105
以下是重载函数funmax的代码:
```c++
#include <iostream>
using namespace std;
int funmax(int a, int b) {
return (a > b) ? a : b;
}
float funmax(float a, float b) {
return (a > b) ? a : b;
}
double funmax(double a, double b) {
return (a > b) ? a : b;
}
int main() {
int a = 10, b = 20;
float c = 1.23, d = 4.56;
double e = 2.345, f = 8.765;
cout << "max of " << a << " and " << b << " is " << funmax(a, b) << endl;
cout << "max of " << c << " and " << d << " is " << funmax(c, d) << endl;
cout << "max of " << e << " and " << f << " is " << funmax(e, f) << endl;
return 0;
}
```
输出结果:
```
max of 20 and 20 is 20
max of 4.56 and 4.56 is 4.56
max of 8.765 and 8.765 is 8.765
```
阅读全文