参数个数不同的函数载体(C++)
来源:互联网 发布:桐乡淘宝摄影基地 编辑:程序博客网 时间:2024/05/01 18:13
/*参数个数不同的函数重载是指,
具有不同参数个数的函数可以使用同一个函数名,
using namespace std;
int max(int a,int b);/*声明带有2个参数的函数max()*/
int max(int a,int b,int c);/*声明带有3个参数的函数max()*/
int max(int a,int b,int c,int d);/*声明带有4个参数的函数max()*/
int main()
{
int result;
cout<<"请输入2个整数:"<<endl;
int a,b;
cin>>a>>b;
result=max(a,b);
cout<<"2个整数的最大值:"<<result<<endl;
cout<<endl;
cout<<"请输入3个整数:"<<endl;
int i,j,k;
cin>>i>>j>>k;
result=max(i,j,k);
cout<<"3个整数的最大值:"<<result<<endl;
cout<<endl;
cout<<"请输入4个整数:"<<endl;
int v,x,y,z;
result=max(v,x,y,z);
cout<<"4个整数的最大值:"<<result<<endl;
cout<<endl;
return 0;
}
int max(int a,int b) /*2个*/
{
return a>b?a:b;
}
int max(int a,int b,int c) /*3个*/
{
int t=max(a,b);
return max(t,c);
}
int max(int a,int b,int c,int d) /*定义一个带有4个参数的函数max()*/
{
int t1=max(a,b);
int t2=max(c,d); /*调用函数*/
return max(t1,t2); /*返回调用函数*/
具有不同参数个数的函数可以使用同一个函数名,
编译器在调用该函数的根据实际参数的个数判断应该调用*/
using namespace std;
int max(int a,int b);/*声明带有2个参数的函数max()*/
int max(int a,int b,int c);/*声明带有3个参数的函数max()*/
int max(int a,int b,int c,int d);/*声明带有4个参数的函数max()*/
int main()
{
int result;
cout<<"请输入2个整数:"<<endl;
int a,b;
cin>>a>>b;
result=max(a,b);
cout<<"2个整数的最大值:"<<result<<endl;
cout<<endl;
cout<<"请输入3个整数:"<<endl;
int i,j,k;
cin>>i>>j>>k;
result=max(i,j,k);
cout<<"3个整数的最大值:"<<result<<endl;
cout<<endl;
cout<<"请输入4个整数:"<<endl;
int v,x,y,z;
result=max(v,x,y,z);
cout<<"4个整数的最大值:"<<result<<endl;
cout<<endl;
return 0;
}
int max(int a,int b) /*2个*/
{
return a>b?a:b;
}
int max(int a,int b,int c) /*3个*/
{
int t=max(a,b);
return max(t,c);
}
int max(int a,int b,int c,int d) /*定义一个带有4个参数的函数max()*/
{
int t1=max(a,b);
int t2=max(c,d); /*调用函数*/
return max(t1,t2); /*返回调用函数*/
}
0 0
- 参数个数不同的函数载体(C++)
- C语言实现参数个数可变的函数(转)
- 不定参数个数的C函数-
- 不定参数个数的C函数
- C中参数个数可变的函数
- C-可变参数个数函数的构建
- php 构造函数支持不同个数参数的方法
- php 构造函数支持不同个数参数的方法
- 我用C语言 写一个函数返回参数二进制中 1 的个数的三种不同方法
- 参数类型不同的函数重载(C++)
- 函数模板的载体-HPP
- C 不确定参数个数的函数的实现
- 使用c++11的function函数绑定参数个数不同的成员函数
- JSP(参数个数可变的函数)
- 用C语言实现参数个数可变的函数
- 用C语言实现参数个数可变的函数
- C语言中实现参数个数可变的函数
- 关于C/C++函数声明中参数个数的疑问
- 地铁换乘
- linux 驱动学习-1th
- apue和unp的学习之旅01——网络基本概念
- IOS中的随机数arc4random方法
- mysql删除,插入,查询等语句的总结
- 参数个数不同的函数载体(C++)
- java利用JXL包操作Excel表
- OpenCv中改变图像尺寸
- 拓扑排序c++实现
- Uboot 内存初始化(2440)
- android:layout_gravity和android:gravity的区别
- 数据结构 排序
- ObservableCollection和List的区别总结
- java Runtime类