📄 right5-1.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 5 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 5 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 1 节 </span>><span style="background-color: #CCFF99">
5.1 </span><span style="background-color: #CCFF99">函数重载</span></p>
<hr color="#008000" size="1">
<p style="line-height: 150%; text-indent: 0; margin: 0" class="右标题" align="left"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">函数重载允许一个程序内声明多个名称相同的函数,这些函数可以完成不同的功能,并可以带有不同类型、不同数目的参数及返问值。函数重载要求编译器能够唯一地确定调用一个函数时应执行哪个函数代码,即采用哪个函数的实现。确定函数实现时,要求从<font color="#FF6600">函数参数的个数和类型</font>上来区分。</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">1.
函数重载的意义</font></p>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">所谓函数重载是指同一个函数名可以对应着多个函数的实现。</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">2.
函数重载的实现</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">定义函数时只要函数名相同而参数不同,就可以实现函数的重载。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example5-1.htm" target="_blank">【例</a></font><a href="example5-1.htm" target="_blank">5-1<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">求两个操作数之和。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example5-2.htm" target="_blank">【例</a></font><a href="example5-2.htm" target="_blank">5-2<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">下面程序找出几个整型数据中的最小值。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font face="??ì?,SimSun" lang="ZH-CN" size="2" color="#FF6600">注意:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><span style="font-family:宋体">①<span lang="EN-US"><o:p>
</o:p>
</span></span>重载函数的返回类型,即函数类型可以相同,也可以不同。<font color="#FF0000">但如果仅仅是返回类型不同,而函数名相同、形参表也相同,则是非法的。在编译时会认为是语法错误。</font>例如:</font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int
fun(int a,int b)</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">float
fun(int a,int b)</font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">就是不合法的。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><span style="font-family:宋体">②<span lang="EN-US"><o:p>
</o:p>
还有一种情况,如:</span></span></font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><span style="font-family: 宋体" lang="EN-US">int
fun(int a,int b)</span></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><span style="font-family: 宋体" lang="EN-US">int
fun(int x,int y)</span></font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">虽然是合法的,但不是重载。它只是<font color="#FF6600">对同一函数原型的多次声明。</font><br>
<br>
</font><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">3. 函数重载的匹配</font></p>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">调用一个重载函数时,编译器是如何知道该调用那个函数呢?这就要根据实参和形参类型以及参数的个数来决定。</font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 27; top: 611"><a href="right4-3.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 488; top: 611"><a href="right5-2.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -