.编写重载函数funmax可分别求两个int数、两个float数、两个double数的最大值。
时间: 2024-05-09 14:20:24 浏览: 65
C语言程序设计-编写自定义函数max,用来求三个数的最大值(要求:数据的输入、输出在主函数中实现)。
5星 · 资源好评率100%
下面是一个示例代码:
```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 = 5, b = 7;
float c = 3.5, d = 4.2;
double e = 2.6, f = 8.9;
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 5 and 7 is 7
max of 3.5 and 4.2 is 4.2
max of 2.6 and 8.9 is 8.9
```
阅读全文