参数个数不同的函数载体(C++)
2014-03-14 21:24
246 查看
/*参数个数不同的函数重载是指,
具有不同参数个数的函数可以使用同一个函数名,
编译器在调用该函数的根据实际参数的个数判断应该调用*/
#include <iostream>
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); /*返回调用函数*/
}
具有不同参数个数的函数可以使用同一个函数名,
编译器在调用该函数的根据实际参数的个数判断应该调用*/
#include <iostream>
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); /*返回调用函数*/
}
相关文章推荐
- jQuery循环绑定不同参数的函数
- 函数参数二:不同种类型函数参数及其调用
- 类的成员函数指针作为参数传递给其他函数和普通函数指针的传递是不同的
- 交换函数的两种不同的实现方式------地址传递和引用参数
- 当用数组名作函数参数时,情况则不同。由于实际上形参和实参为同一数组,因此当形参数组发生变化时,实参数组也随之
- SQL Server 第三堂课,学习数据库函数。跟C#语言异曲同工,同样是由输入参数,输出参数,函数体,返回值四要素组成,不同的是语法和写法。掌握知识的关键在与学好C#语言的函数
- 从send函数和sendto函数参数的不同看TCP和UDP的差别
- 类的成员函数指针作为参数传递给其他函数和普通函数指针的传递是不同的
- 带参数的宏与函数的不同
- socket编程:从send函数和sendto函数参数的不同看TCP和UDP的差别。
- openCV matchTemplate函数不同参数对应的相似性度量
- 数组名作为函数参数和数组元素作为函数参数有什么不同?‘\0’和“\0”的区别?
- js实现调用方法(函数)时可动态传入不同参数个数、模拟实现方法(函数)重载
- 详谈C与C++的函数声明中省略参数的不同意义
- 一个类如何实现两个接口中同名同参数不同返回值的函数
- 1.写一个函数返回参数二进制中 1 的个数;2..获取一个数二进制序列中所有的偶数位和奇数位;3. 输出一个整数的每一位;4.两个int整数的二进制表达中,有多少个位不同
- // c 程序设计设计 教材例8.16 指向函数的指针。 实现同一函数,根据参数执行不同的子函数。
- 回调函数参数不同,导致程序崩溃.
- thinkPHP传数组到模板页面volist每一列加一个button,点击按钮携带参数传给JS函数,跳转到不同的方法
- 父类和子类的同名成员函数参数格式相同,返回值类型不同