当前位置: 首页 > news >正文

【c++】函数重载

函数重载

C++⽀持在同⼀作⽤域中出现同名函数,但是要求这些同名函数的形参不同(可以是参数个数不同或者类型不同),这样C++函数调⽤就表现出了多态⾏为,使⽤更灵活。C语⾔是不⽀持同⼀作⽤域中出现同名函数的。

#include<iostream> 
using namespace std; 
// 1、参数类型不同  
int Add(int left, int right) 
{ cout << "int Add(int left, int right)" << endl; return left + right; 
} 
double Add(double left, double right) 
{ cout << "double Add(double left, double right)" << endl; return left + right; 
} 
// 2、参数个数不同  
void f() 
{ cout << "f()" << endl; 
} 
void f(int a) 
{ cout << "f(int a)" << endl; 
} 
// 3、参数类型顺序不同  
void f(int a, char b) 
{ cout << "f(int a,char b)" << endl; 
} 
void f(char b, int a) 
{ cout << "f(char b, int a)" << endl; 
}
// 下⾯两个函数(无参函数和全缺省函数)构成重载  
// f1()调⽤时会报错,存在歧义,编译器不知道调⽤谁  
void f1() 
{ cout << "f()" << endl; 
} 
void f1(int a = 10) 
{ cout << "f(int a)" << endl; 
} 
int main() 
{ Add(10, 20); Add(10.1, 20.2);//会自动识别然后传到对应的函数里 f();//不传参调用时,存在调用歧义 f(10); f(10, 'a'); f('a', 10); return 0; 
} 

注意:

// 返回值不同不能作为重载条件,因为调⽤时也⽆法区分  
//void fxx() 
//{} 
// 
//int fxx() 
//{ 
// 	return 0; 
//} 

文章转载自:

http://NQW8GF9v.hcstr.cn
http://kOqag9jW.hcstr.cn
http://aXCGX0EN.hcstr.cn
http://xyBJIXdO.hcstr.cn
http://OZbx9Xrt.hcstr.cn
http://29c2Tl2C.hcstr.cn
http://yIjJ79tZ.hcstr.cn
http://LXhYRMcm.hcstr.cn
http://mVy6KoH3.hcstr.cn
http://ncM9HbYm.hcstr.cn
http://4eDCBNvg.hcstr.cn
http://u0hfHXXK.hcstr.cn
http://k8HHInCa.hcstr.cn
http://7RyP95ph.hcstr.cn
http://0YnbS2MG.hcstr.cn
http://AAQvuitp.hcstr.cn
http://LCm5fu7E.hcstr.cn
http://BRy6Ss6S.hcstr.cn
http://nrQlS5RQ.hcstr.cn
http://qzOQYgzc.hcstr.cn
http://I1OZ7R3F.hcstr.cn
http://ZDCCsSLG.hcstr.cn
http://7j4OmJHZ.hcstr.cn
http://IbHhLLZ0.hcstr.cn
http://3EJc87LQ.hcstr.cn
http://WSzOnHow.hcstr.cn
http://sA6ii9OK.hcstr.cn
http://nYwdGLmv.hcstr.cn
http://AG1BY4kX.hcstr.cn
http://QN1MMJ3g.hcstr.cn
http://www.dtcms.com/a/370286.html

相关文章:

  • 算法复杂度分析:从理论基础到工程实践的系统认知
  • Java-118 深入浅出 MySQL ShardingSphere 分片剖析:SQL 支持范围、限制与优化实践
  • 小智医疗:Java大模型应用项目全流程实战
  • DeepSeek辅助在64位Linux中编译运行32位的asm-xml-1.4程序
  • Claude 中国禁用后,阿里 1T 参数模型 Qwen3-Max 连夜发布,效果太强了
  • C++并发编程指南 std::promise 介绍与使用
  • 使用函数调用对整形数组进行排序
  • Linux bzip2 命令使用说明
  • python打包工具setuptools
  • 屏幕小管家——图像识别自动操作助手
  • hbuilderX的gite项目怎么看项目地址
  • 【MFC】对话框节点属性:Language(语言)
  • 联邦学习论文分享:Towards Building the Federated GPT:Federated Instruction Tuning
  • 【Neovim】Vi、Vim、Neovim 与 LazyVim:发展史
  • Eigen中Eigen::Affine3d和Eigen::Isometry3d详解
  • 得物前端二面面经总结
  • 如何离线安装 VirtualMachinePlatform
  • Redisson分布式事务锁
  • 浪潮CD1000-移动云电脑-RK3528芯片-2+32G-安卓9-2种开启ADB ROOT刷机教程方法
  • 详解文件操作
  • 网络通信 IO 模型学习总结基础强化
  • 分布式go项目-搭建监控和追踪方案
  • python炒股
  • SpringBoot01-配置文件
  • 深度学习——数据增强(Data Augmentation)
  • 【Python自动化】 21.1 Pandas 读取 Excel 文件的完整指南
  • 从Java全栈到前端框架:一次真实面试的深度复盘
  • 试用电子实验记录本ELN的经验之谈
  • [C++刷怪笼]:搜索二叉树--便利的查找工具
  • 分布式数据架构