江苏省建设工程考试网站,定制的网站源码,一级网站建设,前端线上培训哪个好创作不易#xff0c;本篇文章如果帮助到了你#xff0c;还请点赞 关注支持一下♡#x16966;)!! 主页专栏有更多知识#xff0c;如有疑问欢迎大家指正讨论#xff0c;共同进步#xff01; #x1f525;c系列专栏#xff1a;C/C零基础到精通 #x1f525; 给大… 创作不易本篇文章如果帮助到了你还请点赞 关注支持一下♡)!! 主页专栏有更多知识如有疑问欢迎大家指正讨论共同进步 c系列专栏C/C零基础到精通 给大家跳段街舞感谢支持ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ c语言内容
专栏c语言之路重点知识整合
【c语言】全部知识点总结 目录 一、概念二、用法有函数参数默认值的函数重载 不构成函数重载的例子总结 一、概念
函数重载是指 在同一个作用域下函数名相同参数列表不同类型、数量、顺序返回值类型无所谓 的函数
重载的函数在调用时编译器可以根据实参自动去匹配对应的函数 二、用法
根据函数重载的定义定义一组函数
他们函数名相同但是返回值和参数列表都不同
int add(int a, int b)
{return a b;
}
double add(double a, double b)
{return a b;
}这两个函数就构成了函数重载在主函数中可以直接调用add函数进行加法计算编译器会根据参数列表的不同自动匹配不同的函数根据int型参数匹配int add函数根据double类型参数匹配double add函数
int main()
{cout add(1, 2) endl;cout add(1.1, 1.2) endl;return 0;
}选中第一条add(1, 2)语句可以看到匹配了int add函数 选中第二条add(1.1, 1.2)语句就匹配了double add函数 输出结果 以下是一些函数重载的例子
void fun(int a)
{cout __FUNCSIG__ endl;
}
void fun(char a, int b)
{cout __FUNCSIG__ endl;
}
void fun(int a,char b) {cout __FUNCSIG__ endl;
}在学过【C/C】函数参数默认值 的知识后我们再来研究一下有函数参数默认值的函数重载
有函数参数默认值的函数重载
void fun(int a)
{cout __FUNCSIG__ endl;
}
void fun(int a,char b) {cout __FUNCSIG__ endl;
}//对上面的函数指定一个默认值
void fun(int a, char bb)
{cout __FUNCSIG__ endl;
}
int main()
{fun(7,x);return 0;
}如果给void fun函数中的参数b指定默认值char bb
此时的void fun(int a, char bb)函数与void fun(int a,char b)函数构成函数重载吗还是与void fun(int a)函数构成函数重载
通过运行可以查看到错误为 函数“void fun(int,char)”已有主体因此void fun(int a, char bb)函数与void fun(int a,char b)并不构成函数重载他们的参数列表和返回值都相同 如果是void fun(int a)函数与void fun(int a, char bb)函数呢构成重载吗
初步思考这两个函数参数列表好像不同只是这两个函数与调用时的参数列表匹配void fun(int a)
{cout __FUNCSIG__ endl;
}
void fun(int a, char bb)
{cout __FUNCSIG__ endl;
}
int main()
{fun(7);return 0;
}此时运行查看错误为C2668 “fun”: 对重载函数的调用不明确看来他们构成了函数重载只是调用不明确如何对某一个函数明确调用呢 类比局部变量声明函数也可以进行局部函数声明!
只需要在主函数中进行局部函数声明使用{ }指定在某段代码块中使用该函数
比如我现在要使用void fun(int a)函数
void fun(int a)
{cout __FUNCSIG__ endl;
}
void fun(int a, char bb)
{cout __FUNCSIG__ endl;
}
int main()
{{//函数局部声明void fun(int a);fun(7); //void __cdecl fun(int)}return 0;
}如果同时需要在主函数中使用void fun(int a)和void fun(int a, char bb)这两个函数
只需要在不同的位置都进行函数声明使用{ }分隔开
void fun(int a)
{cout __FUNCSIG__ endl;
}
void fun(int a, char bb)
{cout __FUNCSIG__ endl;
}
int main()
{//.....{//函数局部声明void fun(int a);fun(7); //void __cdecl fun(int)}//.....{//函数局部声明void fun(int a, char b b);fun(7); //void __cdecl fun(int,char)}return 0;
}这样就在同一个主函数中使用了 在指定函数参数默认值后导致参数列表相同的重载函数
不构成函数重载的例子
*p和p[]都是地址p参数列表相同不构成函数重载
void fun(int* p)
{//...
}
void fun(int p[])
{//...
}char 与 const char 相同类型和常量修饰符都相同认为是相同的函数签名不构成函数重载
void fun(char a)
{//...
}
void fun(const char a)
{//...
}(错误原因都是函数已有主体也就是函数重定义) 在学过函数参数默认值的函数重载后上面的代码可以改成如下就构成了函数重载
void fun(const char a, int b 0)
{//...
}总结
函数重载是指 在同一个作用域下函数名相同参数列表不同类型、数量、顺序返回类型可同可不同 的函数 重载的函数在调用时编译器可以根据实参自动去匹配对应的函数 对于指定函数参数默认值后导致参数列表相同的重载函数主函数调用时只需要对要调用的函数进行局部函数声明 函数重载可以提高代码的可读性使得代码更加清晰明了 大家的点赞、收藏、关注将是我更新的最大动力 欢迎留言或私信建议或问题。
大家的支持和反馈对我来说意义重大我会继续不断努力提供有价值的内容如果本文哪里有错误的地方还请大家多多指出(●◡●) 文章转载自: http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.fykrm.cn.gov.cn.fykrm.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.jmmzt.cn.gov.cn.jmmzt.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.msgnx.cn.gov.cn.msgnx.cn http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.rnmyw.cn.gov.cn.rnmyw.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.gsqw.cn.gov.cn.gsqw.cn