阿杰 发表于 2009-3-9 13:25:05

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

<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>
页: [1]
查看完整版本: 【分享】给函数设置 默认参数【C++源码】