C++语言基础 例程 函数重载
来源:互联网 发布:会议纪要盖章吗 知乎 编辑:程序博客网 时间:2024/06/17 17:24
贺老师的教学链接
重载函数:同名同体,但接口不同
#include <iostream>using namespace std;int max(int a,int b,int c); //函数声明double max(double a,double b,double c); long max(long a,long b,long c); int main( ){ int i1,i2,i3,i; cin>>i1>>i2>>i3; //输入3个整数 i=max(i1,i2,i3); //求3个整数中的最大者 cout<<"i_max="<<i<<endl; double d1,d2,d3,d; cin>>d1>>d2>>d3; //输入3个双精度数 d=max(d1,d2,d3); //求3个双精度数中的最大者 cout<<"d_max="<<d<<endl; long g1,g2,g3,g; cin>>g1>>g2>>g3; //输入3个长整数 g=max(g1,g2,g3); //求3个长整数中的最大者 cout<<"g_max="<<g<<endl;}int max(int a,int b,int c) { if(b>a) a=b; if(c>a) a=c; return a;}double max(double a,double b,double c) { if(b>a) a=b; if(c>a) a=c; return a;}long max(long a,long b,long c) { if(b>a) a=b; if(c>a) a=c; return a;}
重载函数:同名不同体,参数个数有区别
#include <iostream>using namespace std;int max(int a,int b,int c); //函数声明int max(int a,int b); int main( ){ int a=8,b=-12,c=27; //输出3个整数中的最大者 cout<<"max(a,b,c)="<<max(a,b,c)<<endl; //输出两个整数中的最大者 cout<<"max(a,b)="<<max(a,b)<<endl; }int max(int a,int b,int c) { if(b>a) a=b; if(c>a) a=c; return a;}int max(int a,int b) { if(a>b) return a; else return b;}
函数重载与参数的默认值不要冲突
#include <iostream>using namespace std;int max(int a,int b,int c=5); //函数声明int max(int a,int b); int main( ){ int a=8,b=-12,c=27; cout<<"max(a,b,c)="<<max(a,b,c)<<endl; cout<<"max(a,b)="<<max(a,b)<<endl; return 0;}int max(int a,int b,int c) { if(b>a) a=b; if(c>a) a=c; return a;}int max(int a,int b) { if(a>b) return a; else return b;}
4 1
- C++语言基础 例程 函数重载
- C语言基础及例程
- C++语言基础 例程 运算符重载的方法
- C++语言基础 例程 重载双目运算符
- C++语言基础 例程 重载单目运算符
- C++语言基础 例程 构造函数
- C++语言基础 例程 默认构造函数
- C++语言基础 例程 析构函数
- C++语言基础 例程 函数中的引用
- C++语言基础 例程 虚析构函数
- C++语言基础 例程 纯虚函数
- C++语言基础 例程 虚函数
- C++语言基础 例程 内置函数
- C++语言基础 例程 函数模板
- C语言函数参数不定的例程
- c语言模拟函数重载
- C语言实现函数重载
- C语言基础 函数
- 谷歌与百度首页图标搜集
- C++语言基础 例程 内置函数
- 剑指offer2--二维数组中的查找(数组使用)
- C++语言基础 例程 有默认参数的函数
- 二叉树的三种遍历方式:递归、栈、循环
- C++语言基础 例程 函数重载
- Osal提供定时服务的函数
- C++语言基础 例程 函数模板
- JAVA集合中不可重复性与是否有顺序
- C++语言基础 例程 字符串类
- 文本编码
- 漫谈互联网思维
- FPGA 处理 视频信号噪声
- leetcode--House Robber II