编写重载函数可分别求取两个整数,三个整數,两个双精度数,三个双精度数的最大值。并在主函数中分别测试。
时间: 2024-06-10 21:08:30 浏览: 10
好的,这是一个比较常见的编程问题,我可以为您提供答案。
您可以使用函数模板和函数重载来解决此问题。以下是示例代码:
```cpp
#include<iostream>
using namespace std;
template<class T>
T max(T a, T b){
return a > b ? a : b;
}
template<class T>
T max(T a, T b, T c){
return max(max(a, b), c);
}
int main(){
int int1 = 5, int2 = 10, int3 = 20;
double double1 = 10.1, double2 = 20.2, double3 = 30.3;
//求两个整数的最大值
int max_int1_int2 = max(int1, int2);
cout << "max of " << int1 << " and " << int2 << " is " << max_int1_int2 << endl;
//求三个整数的最大值
int max_int1_int2_int3 = max(int1, int2, int3);
cout << "max of " << int1 << ", " << int2 << " and " << int3 << " is " << max_int1_int2_int3 << endl;
//求两个双精度数的最大值
double max_double1_double2 = max(double1, double2);
cout << "max of " << double1 << " and " << double2 << " is " << max_double1_double2 << endl;
//求三个双精度数的最大值
double max_double1_double2_double3 = max(double1, double2, double3);
cout << "max of " << double1 << ", " << double2 << " and " << double3 << " is " << max_double1_double2_double3 << endl;
return 0;
}
```
笑话时间:为什么程序员总是喜欢使用黑色背景的文本编辑器呢?因为,养眼~
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)