.编写重载函数funmax可分别求两个int数、两个float数、两个double数的最大值。
时间: 2024-05-09 08:20:24 浏览: 74
下面是一个示例代码:
```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
```
相关问题
5.编写重载函数funmax可分别求两个int数、两个float数、两个double数的最大值。
以下是重载函数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
```
编写重载函数funmax可分别求两个int数、两个float数、两个double数的最大值。
```
#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 = 3, b = 5;
float c = 3.5, d = 5.5;
double e = 3.56, f = 5.67;
cout << "int最大值:" << funmax(a, b) << endl;
cout << "float最大值:" << funmax(c, d) << endl;
cout << "double最大值:" << funmax(e, f) << endl;
return 0;
}
```
阅读全文