编写重载函数myMax可分别求取两个整数,三个整数,两个双精度数的最大值。
函数接口定义:
无
裁判测试程序样例:
/* 请在这里填写答案 */
int main(){
cout<<myMax(3,4)<<endl;
cout<<myMax(3,4,5)<<endl;
cout<<myMax(4.3,3.4)<<endl;
}
输入样例:
无
输出样例:
在这里给出相应的输出。例如:
4
5
4.3
这里给出两种方法:
1.重载函数:
#include<iostream>
using namespace std;
int myMax(int a,int b){
if(a>b){
return a;
}else{
return b;
}
}
double myMax(double a,double b){
if(a>b){
return a;
}else{
return b;
}
}
int myMax(int a,int b,int c){
if(a>b){
if(a>c){
return a;
}else{
return c;
}
}else{
if(b>c){
return b;
}else{
return c;
}
}
}
2.函数模板:
#include<iostream>
using namespace std;
template <typename T>
T myMax(T a, T b){
if(a>b){
return a;
}else{
return b;
}
}
template <typename T>
T myMax(T a,T b,T c){
if(a>b){
if(a>c){
return a;
}else{
return c;
}
}else{
if(b>c){
return b;
}else{
return c;
}
}
}