找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 4462|回复: 0

【分享】给函数设置 默认参数【C++源码】

[复制链接]

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

发表于 2009-3-9 13:25:05 | 显示全部楼层 |阅读模式
<font face="Verdana">
<p><br/>// Sum.cpp <br/>//给函数设置 默认参数<br/>//阿杰编写于:2009年3月9日13:22:36<br/><br/>#include "iostream.h" <br/>int Sum(int a, int b = 2, int c = 3);<br/>void main()<br/>{<br/>&nbsp; int x(4), y(5), z(6);<br/>&nbsp; //x(4) 就是x=4;的意思 a,b,c是函数的形参,x,y,z是传入函数对应的值,分别对应于a,b,c 如果传入的参不足三个,系统就会使用默认的值<br/>&nbsp; int he;<br/>&nbsp; he = Sum(x, y, z);<br/>&nbsp; cout &lt;&lt; "和=" &lt;&lt; he &lt;&lt; endl;<br/>&nbsp; cout &lt;&lt; endl;<br/>&nbsp; he = Sum(x, y);<br/>&nbsp; cout &lt;&lt; "和=" &lt;&lt; he &lt;&lt; endl;<br/>&nbsp; cout &lt;&lt; endl;<br/>&nbsp; he = Sum(x);<br/>&nbsp; cout &lt;&lt; "和=" &lt;&lt; he &lt;&lt; endl;<br/>&nbsp; cout &lt;&lt; endl;<br/>}</p>
<p>int Sum(int a, int b, int c)<br/>{<br/>&nbsp; cout &lt;&lt; "a=" &lt;&lt; a &lt;&lt; "\tb=" &lt;&lt; b &lt;&lt; "\tc=" &lt;&lt; c &lt;&lt; endl;<br/>&nbsp; return (a + b + c);<br/>}</font></p>
【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表