<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C ++</title>
	<atom:link href="http://www.bizzymicbizness.com/cplusplus/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bizzymicbizness.com/cplusplus</link>
	<description>All about C++</description>
	<lastBuildDate>Mon, 19 Oct 2009 12:38:17 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Functions Using Array Ranges</title>
		<link>http://www.bizzymicbizness.com/cplusplus/functions-using-array-ranges</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/functions-using-array-ranges#comments</comments>
		<pubDate>Mon, 19 Oct 2009 06:34:39 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Range]]></category>
		<category><![CDATA[STL]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=836</guid>
		<description><![CDATA[C++ functions that process arrays need to be informed about

The kind of data in the array
The location of the beginning of the array
And the number of elements in the array


The traditional C/C++ approach to functions that process arrays

Is to pass a pointer to the start of the array as one argument

And to pass the size [...]]]></description>
			<content:encoded><![CDATA[<p>C++ functions that process arrays need to be informed about</p>
<ul>
<li>The kind of data in the array</li>
<li>The location of the beginning of the array</li>
<li>And the number of elements in the array</li>
</ul>
<p><br class="blank" /><br />
The traditional C/C++ approach to functions that process arrays</p>
<ul>
<li>Is to pass a pointer to the start of the array as one argument
<ul>
<li>And to pass the size of the array as a second argument
<ul>
<li>(The pointer tells the function both where to find the array and the kind of data in it.)
<ul>
<li>That gives the function the information it needs to find all the data</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
There is another approach to giving a function the information it needs: to specify a <em>range</em> of elements</p>
<ul>
<li>This can be done by passing two pointers</li>
<ul>
<li>One identifying the start of the array and one identifying the end of the array</li>
</ul>
<li>The C++ Standard Template Library, for example, generalizes the range approach</li>
<ul>
<li>The STL approach uses the concept of “one past the end” to indicate an extent</li>
<ul>
<li>That is, in the case of an array, the argument identifying the end of the array would be a pointer to the location just after the last element</li>
</ul>
<li>For example, suppose you have this declaration (A-1):</li>
<ul>
<li>Then the two pointers <strong>elboud</strong> and <strong>elboud + 20</strong> define the range</li>
<ul>
<li>First, <strong>elboud</strong>, being the name of the array, points to the first element</li>
<ul>
<li>The expression <strong>elboud + 19</strong> points to the last element (that is, <strong>elboud[19]</strong>), so <strong>elboud + 20</strong> points to one past the end of the array</li>
</ul>
</ul>
</ul>
</ul>
<li>Passing a range to a function tells it</li>
<ul>
<li>Which elements to process</li>
</ul>
<li>(A-2) use two pointers to specify a range</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">double</span> elbuod<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun4.cpp -- functions with an array range</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> ArSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> begin, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> end<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> cookies<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">8</span>,<span style="color: #0000dd;">16</span>,<span style="color: #0000dd;">32</span>,<span style="color: #0000dd;">64</span>,<span style="color: #0000dd;">128</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require preceding int with static to</span>
<span style="color: #666666;">// enable array initialization</span>
&nbsp;
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total cookies eaten: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// first 3 elements</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;First three eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// last 4 elements</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Last four eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// return the sum of an integer array</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> begin, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> end<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> pt<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>pt <span style="color: #000080;">=</span> begin<span style="color: #008080;">;</span> pt <span style="color: #000040;">!</span><span style="color: #000080;">=</span> end<span style="color: #008080;">;</span> pt<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> <span style="color: #000040;">*</span>pt<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here’s the output of the program in (A-2):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-837" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output7.jpg" alt="Output" width="597" height="275" /></p>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
In (A-2), notice the <strong>for</strong> loop in the <strong>sum_array()</strong> function (A-3)</p>
<ul>
<li>It sets <strong>pt</strong> to point to the first element to be processed (the one pointed to by <strong>begin</strong>) and adds <strong>*pt</strong> (the value of the element) to <strong>total</strong>
<ul>
<li>Then the loop updates <strong>pt</strong> by incrementing it, causing it to point to the next element
<ul>
<li>The process continues as long as <strong>pt != end</strong>
<ul>
<li>When <strong>pt</strong> finally equals <strong>end</strong>, it’s pointing to the location following the last element of the range, so the loop halts</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>pt <span style="color: #000080;">=</span> begin<span style="color: #008080;">;</span> pt <span style="color: #000040;">!</span><span style="color: #000080;">=</span> end<span style="color: #008080;">;</span> pt<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> <span style="color: #000040;">*</span>pt<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Second, notice how the different function calls specify different ranges within the array (A-4)</p>
<ul>
<li>The pointer value <strong>cookies + ArSize</strong> points to the location following the last element</li>
<ul>
<li>(The array has <strong>ArSize</strong> elements, so <strong>cookies[ArSize - 1]</strong> is the last element, and its address is <strong>cookies + ArSize &#8211; 1</strong>.)</li>
<ul>
<li>So the range <strong>cookies</strong>, <strong>cookies + ArSize</strong> specifies the entire array</li>
<ul>
<li>Similarly, <strong>cookies</strong>, <strong>cookies + 3</strong> specifies the first three elements, and so on</li>
</ul>
</ul>
</ul>
<li>Note, by the way, that the rules for pointer subtraction imply that</li>
<ul>
<li>In <strong>sum_arr()</strong>, the expression <strong>end</strong> <strong>-</strong> <strong>begin</strong> is an integer value equal to the number of elements in the range</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
...
<span style="color: #007788;">sum</span> <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// first 3 elements</span>
...
<span style="color: #007788;">sum</span> <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// last 4 elements</span></pre></div></div>

<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/functions-using-array-ranges/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Array Function Examples</title>
		<link>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples#comments</comments>
		<pubDate>Sun, 18 Oct 2009 12:36:59 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Borland C++]]></category>
		<category><![CDATA[Bottom-Up Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[iostream]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Procedural Programming]]></category>
		<category><![CDATA[Top-Down Programming]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=801</guid>
		<description><![CDATA[When you choose to use an array to represent data, you are making a design decision

But design decisions should go beyond how data is stored; they should also involve how the data is used


Often, you’ll find it profitable to write specific functions to handle specific data operations

(The profits here include increased program reliability, ease of [...]]]></description>
			<content:encoded><![CDATA[<p>When you choose to use an array to represent data, you are making a design decision</p>
<ul>
<li>But design decisions should go beyond how data is stored; they should also involve how the data is used</li>
</ul>
<p><br class="blank" /><br />
Often, you’ll find it profitable to write specific functions to handle specific data operations</p>
<ul>
<li>(The profits here include increased program reliability, ease of modification, and ease of debugging.)
<ul>
<li>Also, when you begin integrating storage properties with operations when you think about a program, you are taking an important step toward the OOP mind-set; that, too, might prove profitable in the future</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Let’s examine a simple case</p>
<ul>
<li>Suppose you want to use an array to keep track of the dollar values of your real estate
<ul>
<li>(If necessary, suppose you have real estate.)
<ul>
<li>You have to decide what type to use
<ul>
<li>Certainly, <strong>double</strong> is less restrictive in its range than <strong>int</strong> or <strong>long</strong>, and it provides enough significant digits to represent the values precisely</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Next, you have to decide on the number of array elements</p>
<ul>
<li>(With dynamic arrays created with new, you can put off that decision, but let’s keep things simple.)
<ul>
<li>Let’s say that you have no more than five properties, so you can use an array of five <strong>doubles</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Now consider the possible operations you might want to execute with the real estate array</p>
<ul>
<li>Two very basic ones are:</li>
<ul>
<li>Reading values into the array</li>
<li>And displaying the array contents</li>
</ul>
<li>Let’s add one more operation to the list:</li>
<ul>
<li>Reassessing the value of the properties</li>
<ul>
<li>For simplicity, assume that all your properties increase or decrease in value at the same rate</li>
</ul>
</ul>
<li>(Remember, this is a topic in C++, not on real estate management.)</li>
</ul>
<p><br class="blank" /><br />
Next, fit a function to each operation and then write the code accordingly</p>
<ul>
<li>We’ll go through the steps of creating these pieces of a program next
<ul>
<li>Afterward, we’ll fit them into a complete example</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Filling the Array</h2>
<p><br class="blank" /><br />
Because a function with an array name argument accesses the original array, not a copy, you can use a function call to assign values to array elements</p>
<ul>
<li>One argument to the function will be the name of the array to be filled
<ul>
<li>In general, a program might manage more than one person’s investments, hence more than one array, so you don’t want to build the array size into the function</li>
<ul>
<li>Instead, you pass the array size as a second argument</li>
</ul>
</ul>
<li>Also, it’s possible that you might want to quit reading data before filling the array, so you want to build that feature in to the function</li>
<ul>
<li>Because you might enter fewer than the maximum number of elements, it makes sense to have the function return the actual number of values entered
<ul>
<li>These considerations suggest the following function prototype (A-1)</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
The function takes an array name argument and an argument specifying the maximum number of items to be read, and the function returns the actual number of items read</p>
<ul>
<li>For example, if you use this function with an array of five elements, you pass <strong>5</strong> as the second argument
<ul>
<li>If you then enter only three values, the function returns <strong>3</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You can use a loop to read successive values into the array, but how can you terminate the loop early?</p>
<ul>
<li>One way is to use a special value to indicate the end of input</li>
<ul>
<li>Because no property should have a negative value, you can use a negative number to indicate the end of input</li>
</ul>
<li>Also, the function should do something about bad input</li>
<ul>
<li>Such as terminating further input</li>
</ul>
<li>Given this, you can code the function as follows (A-2):</li>
<ul>
<li>Note that this code includes a prompt to the user</li>
<ul>
<li>If the user enters a non-negative value, the value is assigned to the array</li>
<ul>
<li>Otherwise, the loop terminates</li>
</ul>
<li>If the user enters only valid values</li>
<ul>
<li>The loop terminates after it reads limit values</li>
</ul>
</ul>
<li>The last thing the loop does is increment <strong>i</strong>, so after the loop terminates, <strong>i</strong> is one greater than the last array index, hence it’s equal to the number of filled elements</li>
<ul>
<li>The function then returns that value</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> temp<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> limit<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter value #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> temp<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span><span style="color: #0000dd;">cin</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// bad input</span>
		<span style="color: #008000;">&#123;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad input; input process terminated.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// signal to terminate</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Showing the Array and Protecting It with const</h2>
<p><br class="blank" /><br />
Building a function to display the array contents is simple</p>
<ul>
<li>You pass the name of the array and the number of filled elements to the function, which then uses a loop to display each element</li>
</ul>
<p><br class="blank" /><br />
But there is another consideration—guaranteeing that the display function doesn’t alter the original array</p>
<ul>
<li>Unless the purpose of a function is to alter data passed to it, you should safeguard it from doing so</li>
<ul>
<li>That protection comes automatically with ordinary arguments because C++ passes them by value, and the function works with a copy</li>
</ul>
<li>But functions that use an array work with the original</li>
<ul>
<li>After all, that’s why the <strong>fill_array()</strong> function is able to do its job</li>
</ul>
</ul>
<p><br class="blank" /><br />
To keep a function from accidentally altering the contents of an array argument</p>
<ul>
<li>You can use the keyword <strong>const</strong> when you declare the formal argument (A-3)</li>
<ul>
<li>The declaration states that the pointer <strong>ar</strong> points to constant data</li>
<ul>
<li>This means that you can’t use <strong>ar</strong> to change the data</li>
<ul>
<li>That is, you can use a value such as <strong>ar[0]</strong>, but you can’t change that value</li>
</ul>
</ul>
</ul>
<li>Note that this doesn’t mean that the original array needs be constant; it just means that you can’t use <strong>ar</strong> in the <strong>show_array()</strong> function to change the data</li>
<ul>
<li>Thus, <strong>show_array()</strong> treats the array as read-only data</li>
</ul>
<li>Suppose you accidentally violate this restriction by doing something like the following in the <strong>show_array()</strong> function (A-4)</li>
<ul>
<li>In this case, the compiler will put a stop to your wrongful ways</li>
<ul>
<li>Borland C++, for example, gives an error message like this (edited slightly) (A-5)</li>
<ul>
<li>Other compilers may choose to express their displeasure in different words</li>
</ul>
<li>The message reminds you that C++ interprets the declaration <strong>const double</strong> <strong>ar[]</strong> to mean <strong>const double *ar</strong></li>
<ul>
<li>Thus, the declaration really says that <strong>ar</strong> points to a constant value</li>
<ul>
<li>We’ll discuss this in detail when we finish with the current example</li>
</ul>
</ul>
</ul>
</ul>
<li>Meanwhile, here is the code for the <strong>show_array()</strong> function (A-6)</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ar<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-5)</p>
<div class="syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">
Cannot modify a const object in function show_array(const double *,int)
</pre>
</div>
</div>
<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Property #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: $&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Modifying the Array</h2>
<p><br class="blank" /><br />
The third operation for the array in this example is multiplying each element by the same revaluation factor</p>
<ul>
<li>You need to pass three arguments to the function:</li>
<ul>
<li>The factor</li>
<li>The array</li>
<li>And the number of elements</li>
</ul>
<li>No return value is needed, so the function can look like this (A-7)</li>
<ul>
<li>Because this function is supposed to alter the array values, you don’t use <strong>const</strong> when you declare <strong>ar</strong></li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-7)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> r<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Putting the Pieces Together</h2>
<p><br class="blank" /><br />
Now that you’ve defined a data type in terms of how it’s stored (an array) and how it’s used (three functions), you can put together a program that uses the design</p>
<ul>
<li>Because you’ve already built all the array-handling tools</li>
<ul>
<li>You’ve greatly simplified programming <strong>main()</strong></li>
</ul>
<li>Most of the remaining programming work consists of having <strong>main()</strong> call the functions you’ve just developed</li>
<li>(A-8) shows the result</li>
<ul>
<li>It places a <strong>using</strong> directive in just those functions that use the <strong>iostream</strong> facilities</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-8)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun3.cpp -- array functions and const</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> Max <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// function prototypes</span>
<span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// don’t change data</span>
<span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> properties<span style="color: #008000;">&#91;</span>Max<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> size <span style="color: #000080;">=</span> fill_array<span style="color: #008000;">&#40;</span>properties, Max<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	show_array<span style="color: #008000;">&#40;</span>properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter reassessment rate: &quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> factor<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> factor<span style="color: #008080;">;</span>
	revalue<span style="color: #008000;">&#40;</span>factor, properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	show_array<span style="color: #008000;">&#40;</span>properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Done.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> temp<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> limit<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter value #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> temp<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span><span style="color: #0000dd;">cin</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// bad input</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad input; input process terminated.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// signal to terminate</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// the following function can use, but not alter,</span>
<span style="color: #666666;">// the array whose address is ar</span>
<span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Property #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: $&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// multiplies each element of ar[] by r</span>
<span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> r<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here are two sample runs of the program in (A-8):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-802" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output6.jpg" alt="Output" width="598" height="275" /></p>
<p><br class="blank" /></p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-803" title="Output Second" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output-Second2.jpg" alt="Output Second" width="597" height="275" /></p>
<p><br class="blank" /><br />
Recall that <strong>fill_array()</strong> prescribes that input should quit when the user enters five properties or enters a negative number, whichever comes first</p>
<ul>
<li>The first output example illustrates reaching the five-property limit</li>
<ul>
<li>And the second output example illustrates that entering a negative value terminates the input phase</li>
</ul>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
We’ve already discussed the important programming details related to the real estate example, so let’s reflect on the process</p>
<ul>
<li>You began by thinking about the data type and designed appropriate functions to handle the data</li>
<ul>
<li>Then, you assembled these functions into a program</li>
<ul>
<li>This is sometimes called bottom-up programming because the design process moves from the component parts to the whole</li>
<ul>
<li>This approach is well suited to OOP, which concentrates on data representation and manipulation first</li>
</ul>
</ul>
</ul>
<li>Traditional procedural programming, on the other hand, leans toward top-down programming</li>
<ul>
<li>In which you develop a modular grand design first and then turn your attention to the details</li>
</ul>
<li>Both methods are useful, and both lead to modular programs</li>
</ul>
<p><br class="blank" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Implications of Using Arrays as Arguments</title>
		<link>http://www.bizzymicbizness.com/cplusplus/the-implications-of-using-arrays-as-arguments</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/the-implications-of-using-arrays-as-arguments#comments</comments>
		<pubDate>Sat, 17 Oct 2009 09:02:15 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Function Call]]></category>
		<category><![CDATA[Hexadecimal]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[sizeof]]></category>
		<category><![CDATA[std::]]></category>
		<category><![CDATA[Variable]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=789</guid>
		<description><![CDATA[Let’s look at the implications of (A-1)

The function call sum_arr(cookies, ArSize)

Passes the address of the first element of the cookies array and the number of elements of the array to the sum_arr() function

The sum_arr() function

Assigns the cookies address to the pointer variable arr and assigns ArSize to the int variable n

This means (A-1) doesn’t really [...]]]></description>
			<content:encoded><![CDATA[<p>Let’s look at the implications of (A-1)</p>
<ul>
<li>The function call <strong>sum_arr(cookies, ArSize)</strong></li>
<ul>
<li>Passes the address of the first element of the <strong>cookies</strong> array and the number of elements of the array to the <strong>sum_arr() </strong>function</li>
</ul>
<li>The <strong>sum_arr()</strong> function</li>
<ul>
<li>Assigns the <strong>cookies</strong> address to the pointer variable <strong>arr</strong> and assigns <strong>ArSize</strong> to the <strong>int</strong> variable <strong>n</strong></li>
</ul>
<li>This means (A-1) doesn’t really pass the array contents to the function</li>
<ul>
<li>Instead, it tells the function where the array is (the address), what kind of elements it has (the type), and how many elements it has (the <strong>n</strong> variable)</li>
<ul>
<li>See (Image-1)</li>
</ul>
</ul>
<li>Armed with this information, the function then uses the original array</li>
<ul>
<li>If you pass an ordinary variable, the function works with a copy</li>
<ul>
<li>But if you pass an array, the function works with the original</li>
</ul>
</ul>
<li>Actually, this difference doesn’t violate C++’s pass-by-value approach</li>
<ul>
<li>The <strong>sum_arr()</strong> function still passes a value that’s assigned to a new variable</li>
<ul>
<li>But that value is a single address, not the contents of an array</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun1.cpp -- functions with an array argument</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> ArSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// prototype</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> cookies<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">8</span>,<span style="color: #0000dd;">16</span>,<span style="color: #0000dd;">32</span>,<span style="color: #0000dd;">64</span>,<span style="color: #0000dd;">128</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require preceding int with static to</span>
<span style="color: #666666;">// enable array initialization</span>
&nbsp;
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total cookies eaten: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// return the sum of an integer array</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> arr<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
(Image-1) Telling a function about an array</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-791" title="Telling a function about an array" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Telling-a-function-about-an-array.jpg" alt="Telling a function about an array" width="498" height="306" /></p>
<p><br class="blank" /><br />
Is the correspondence between array names and pointers a good thing?</p>
<ul>
<li>Indeed, it is</li>
</ul>
<p><br class="blank" /><br />
The design decision to use array addresses as arguments saves the time and memory needed to copy an entire array</p>
<ul>
<li>The overhead for using copies can be prohibitive if you’re working with large arrays</li>
<ul>
<li>With copies, not only does a program need more computer memory, but it has to spend time copying large blocks of data</li>
</ul>
<li>On the other hand, working with the original data raises the possibility of inadvertent data corruption</li>
<ul>
<li>That’s a real problem in classic C, but ANSI C and C++’s <strong>const</strong> modifier provides a remedy</li>
<ul>
<li>You’ll soon see an example</li>
</ul>
</ul>
<li>But first, let’s alter (A-1) to illustrate some points about how array functions operate</li>
</ul>
<p><br class="blank" /><br />
(A-2) demonstrates that <strong>cookies</strong> and <strong>arr</strong> have the same value</p>
<ul>
<li>It also shows how the pointer concept makes the <strong>sum_arr</strong> function more versatile than it may have appeared at first
<ul>
<li>To provide a bit of variety and to show you what it looks like, the program uses the <strong>std::</strong> qualifier instead of the <strong>using</strong> directive to provide access to <strong>cout</strong> and <strong>endl</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun2.cpp -- functions with an array argument</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> ArSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// use std:: instead of using directive</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> cookies<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">2</span>,<span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">8</span>,<span style="color: #0000dd;">16</span>,<span style="color: #0000dd;">32</span>,<span style="color: #0000dd;">64</span>,<span style="color: #0000dd;">128</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require preceding int with static to</span>
<span style="color: #666666;">// enable array initialization</span>
&nbsp;
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> cookies <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = array address, &quot;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require a type cast: unsigned (cookies)</span>
&nbsp;
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">sizeof</span> cookies <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = sizeof cookies<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, ArSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total cookies eaten: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// a lie</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;First three eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// another lie</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Last four eaters ate &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; cookies.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// return the sum of an integer array</span>
<span style="color: #0000ff;">int</span> sum_arr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> arr <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = arr, &quot;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// some systems require a type cast: unsigned (arr)</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">sizeof</span> arr <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = sizeof arr<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		total <span style="color: #000080;">=</span> total <span style="color: #000040;">+</span> arr<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here’s the output of the program in (A-2): </p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-790" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output5.jpg" alt="Output" width="538" height="248" /></p>
<p><br class="blank" /><br />
Note that the address values and the array and integer sizes will vary from system to system</p>
<ul>
<li>Also, some implementations will display the addresses in base 10 notation instead of in hexadecimal</li>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
(A-2) illustrates some very interesting points about array functions</p>
<ul>
<li>First, note that <strong>cookies</strong> and <strong>arr</strong> both evaluate to the same address, exactly as claimed</li>
<li>But <strong>sizeof cookies</strong> is <strong>32</strong>, whereas <strong>sizeof arr</strong> is only <strong>4</strong></li>
<ul>
<li>That’s because <strong>sizeof cookies</strong> is the size of the whole array, whereas <strong>sizeof arr</strong> is the size of the pointer variable</li>
</ul>
<li>By the way, this is why you have to explicitly pass the size of the array rather than use <strong>sizeof arr</strong> in <strong>sum_arr()</strong></li>
<ul>
<li>Because the only way <strong>sum_arr()</strong> knows the number of elements in the array is through what you tell it with the second argument, you can lie to the function</li>
<ul>
<li>For example, the second time the program uses the function, it makes this call (A-3)</li>
<ul>
<li>By telling the function that <strong>cookies</strong> has just three elements, you get the function to calculate the sum of the first three elements</li>
</ul>
</ul>
</ul>
<ul>
<li>Why stop there? You can also lie about where the array starts (A-4)</li>
<ul>
<li>Because <strong>cookies</strong> acts as the address of the first element, <strong>cookies + 4</strong> acts as the address of the fifth element</li>
<ul>
<li>This statement sums the fifth, sixth, seventh, and eighth elements of the array</li>
</ul>
</ul>
</ul>
<li>Note in the output how the third call to the function assigns a different address to <strong>arr</strong> than the first two calls did</li>
<ul>
<li>And yes, you can use <strong>&amp;cookies[4]</strong> instead of <strong>cookies + 4</strong> as the argument; they both mean the same thing</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies, <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">sum <span style="color: #000080;">=</span> sum_arr<span style="color: #008000;">&#40;</span>cookies <span style="color: #000040;">+</span> <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Remember</p>
<ul>
<li>To indicate the kind of array and the number of elements to an array-processing function, you pass the information as two separate arguments (A-5)
<ul>
<li>Don’t try to pass the array size by using brackets notation (A-6)</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-5)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> fillArray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// prototype</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> fillArray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> arr<span style="color: #008000;">&#91;</span>size<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// NO -- bad prototype</span></pre></div></div>

<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/the-implications-of-using-arrays-as-arguments/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Reference Variable</title>
		<link>http://www.bizzymicbizness.com/cplusplus/creating-a-reference-variable</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/creating-a-reference-variable#comments</comments>
		<pubDate>Fri, 16 Oct 2009 08:27:50 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Reference Variable]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=771</guid>
		<description><![CDATA[C and C++ use the &#38; symbol to indicate the address of a variable

C++ assigns an additional meaning to the &#38; symbol

Presses it into service for declaring references

For example, to make rodents an alternative name for the variable rats, you could do the following (A-1):






Example of (A-1)

int rats;
int &#38; rodents = rats; // makes rodents [...]]]></description>
			<content:encoded><![CDATA[<p>C and C++ use the <strong>&amp;</strong> symbol to indicate the address of a variable</p>
<ul>
<li>C++ assigns an additional meaning to the <strong>&amp;</strong> symbol
<ul>
<li>Presses it into service for declaring references
<ul>
<li>For example, to make <strong>rodents</strong> an alternative name for the variable <strong>rats</strong>, you could do the following (A-1):</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rats<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// makes rodents an alias for rats</span></pre></div></div>

<p><br class="blank" /><br />
In this context, <strong>&amp;</strong> is not the address operator</p>
<ul>
<li>Instead, it serves as part of the type identifier
<ul>
<li>Just as <strong>char *</strong> in a declaration means pointer-to-<strong>char</strong>,<strong> int &amp;</strong> means reference-to-<strong>int</strong></li>
</ul>
</li>
</ul>
<p> <br />
<br class="blank" /><br />
The reference declaration allows you to use <strong>rats</strong> and <strong>rodents</strong> interchangeably; both refer to the same value and the same memory location</p>
<ul>
<li>(A-2) illustrates the truth of this claim</li>
</ul>
<p> </p>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// firstref.cpp -- defining and using a reference</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">255</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// rodents is a reference</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	rodents<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// some implementations require type casting the following</span>
	<span style="color: #666666;">// addresses to type unsigned</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Note that the <strong>&amp;</strong> operator in the statement (A-3)</p>
<ul>
<li>Is not the address operator
<ul>
<li>But declares that <strong>rodents</strong> is of type <strong>int &amp;</strong>—that is, it is a reference to an <strong>int</strong> variable</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
But the <strong>&amp;</strong> operator in the statement (A-4)</p>
<ul>
<li>Is the address operator
<ul>
<li>With <strong>&amp;rodents</strong> representing the address of the variable to which <strong>rodents</strong> refers</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Here is the output of the program in (A-2): </p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-772" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output4.jpg" alt="Output" width="598" height="275" /></p>
<p><br class="blank" /><br />
As you can see both, <strong>rats</strong> and <strong>rodents</strong> have the same value and the same address</p>
<ul>
<li>(The address values and display format vary from system to system.)</li>
</ul>
<p> <br />
<br class="blank" /><br />
Incrementing <strong>rodents</strong> by one, affects both variables</p>
<ul>
<li>More precisely, the <strong>rodents++</strong> operation increments a single variable for which there are two names</li>
</ul>
<p><br class="blank" /><br />
Again, keep in mind that although this example shows you how a reference works</p>
<ul>
<li>It doesn’t represent the typical use for a reference, which is as a function parameter, particularly for structure and object arguments
<ul>
<li>We’ll look into these uses pretty soon</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
References tend to be a bit confusing at first to C veterans coming to C++</p>
<ul>
<li>Because they are tantalizingly reminiscent of pointers, yet somehow different
<ul>
<li>For example, you can create both a reference and a pointer to refer to <strong>rats </strong>(A-3)</li>
</ul>
<li>Then, you could</li>
<ul>
<li>Use the expressions <strong>rodents</strong> and <strong>*prats</strong> interchangeably with <strong>rats</strong>
<ul>
<li>And use the expressions <strong>&amp;rodents</strong> and <strong>prats</strong> interchangeably with <strong>&amp;rats</strong></li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">255</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// rodents a reference</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> prats <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>  <span style="color: #666666;">// prats a pointer</span></pre></div></div>

<p><br class="blank" /><br />
From this standpoint</p>
<ul>
<li>A reference looks a lot like a pointer in disguised notation in which the <strong>*</strong> dereferencing operator is understood implicitly
<ul>
<li>And, in fact, that’s more or less what a reference is</li>
</ul>
<li>But there are differences besides those of notation</li>
<ul>
<li>For one, it is necessary to initialize the reference when you declare it; you can’t declare the reference and then assign it a value later the way you can with a pointer (A-4):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rat<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodent<span style="color: #008080;">;</span>
rodent <span style="color: #000080;">=</span> rat<span style="color: #008080;">;</span> <span style="color: #666666;">// No, you can’t do this</span></pre></div></div>

<p><br class="blank" /><br />
Remember</p>
<ul>
<li>You should initialize a reference variable when you declare it</li>
</ul>
<p><br class="blank" /><br />
A reference is rather like a <strong>const</strong> pointer; you have to initialize it when you create it</p>
<ul>
<li>And once a reference pledges its allegiance to a particular variable, it sticks to its pledge
<ul>
<li>That is, (A-5)</li>
</ul>
<li>(A-5) is, in essence, a disguised notation for something like this (A-6)
<ul>
<li>Here, the reference <strong>rodents</strong> plays the same role as the expression <strong>*pr</strong></li>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-5)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> pr <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
(A-7) shows what happens</p>
<ul>
<li>If you try to make a reference change allegiance from a <strong>rats</strong> variable to a <strong>bunnies</strong> variable</li>
</ul>
<p><br class="blank" /><br />
Example of (A-7)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// secondref.cpp -- defining and using a reference</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">256</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> rats<span style="color: #008080;">;</span> <span style="color: #666666;">// rodents is a reference</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;rats address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> bunnies <span style="color: #000080;">=</span> <span style="color: #0000dd;">128</span><span style="color: #008080;">;</span>
	rodents <span style="color: #000080;">=</span> bunnies<span style="color: #008080;">;</span> <span style="color: #666666;">// can we change the reference?</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;bunnies = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> bunnies<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rats = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rats<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;bunnies address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>bunnies<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, rodents address = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">&amp;</span>rodents <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here’s the output of the program in (A-7):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-773" title="Output Second" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output-Second1.jpg" alt="Output Second" width="599" height="276" /></p>
<p><br class="blank" /><br />
Initially, <strong>rodents</strong> refers to <strong>rats</strong></p>
<ul>
<li>But then the program apparently attempts to make <strong>rodents</strong> a reference to <strong>bunnies </strong>(A-8):</li>
<ul>
<li>For a moment, it looks as if this attempt has succeeded because the value of <strong>rodents</strong> changes from <strong>256</strong> to <strong>128</strong></li>
<ul>
<li>But closer inspection reveals that <strong>rats</strong> also has changed to <strong>128</strong> and that <strong>rats</strong> and <strong>rodents</strong> still share the same address</li>
<ul>
<li>Which differs from the <strong>bunnies</strong> address</li>
</ul>
</ul>
</ul>
<li>Because <strong>rodents</strong> is an alias for <strong>rats</strong></li>
<ul>
<li>The assignment statement really means the same as the following (A-9)</li>
<ul>
<li>That is, it means “Assign the value of the <strong>bunnies</strong> variable to the <strong>rat</strong> variable.”</li>
</ul>
</ul>
<li>In short, you can set a reference by an initializing declaration</li>
<ul>
<li>Not by assignment</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-8)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">rodents <span style="color: #000080;">=</span> bunnies<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-9)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">rats <span style="color: #000080;">=</span> bunnies<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Suppose you tried the following (A-10)</p>
<ul>
<li>Initializing <strong>rodents</strong> to <strong>*pt</strong> makes <strong>rodents</strong> refer to <strong>rats</strong>
<ul>
<li>Subsequently altering <strong>pt</strong> to point to <strong>bunnies</strong> does not alter the fact that <strong>rodents</strong> refers to <strong>rats</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-10)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rats <span style="color: #000080;">=</span> <span style="color: #0000dd;">256</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> pt <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>rats<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span> rodents <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>pt<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> bunnies <span style="color: #000080;">=</span> <span style="color: #0000dd;">128</span><span style="color: #008080;">;</span>
pt <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>bunnies<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/creating-a-reference-variable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Function Overloading</title>
		<link>http://www.bizzymicbizness.com/cplusplus/function-overloading</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/function-overloading#comments</comments>
		<pubDate>Thu, 15 Oct 2009 04:47:01 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=730</guid>
		<description><![CDATA[Function polymorphism is a neat C++ addition to C’s capabilities

Whereas default arguments

Let you call the same function by using varying numbers of arguments

function polymorphism, also called function overloading

Lets you use multiple functions sharing the same name





The word polymorphism

Means having many forms, so function polymorphism lets a function have many forms


Similarly, the expression function overloading

Means you [...]]]></description>
			<content:encoded><![CDATA[<p>Function polymorphism is a neat C++ addition to C’s capabilities</p>
<ul>
<li>Whereas default arguments
<ul>
<li>Let you call the same function by using varying numbers of arguments</li>
</ul>
<li><em>function polymorphism</em>, also called <em>function overloading</em>
<ul>
<li>Lets you use multiple functions sharing the same name</li>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The word <em>polymorphism</em></p>
<ul>
<li>Means having many forms, so <em>function polymorphism</em> lets a function have many forms</li>
</ul>
<p><br class="blank" /><br />
Similarly, the expression <em>function overloading</em></p>
<ul>
<li>Means you can attach more than one function to the same name, thus overloading the name</li>
</ul>
<p><br class="blank" /><br />
Both expressions boil down to the same thing,</p>
<ul>
<li>But we’ll usually use the expression <em>function overloading —</em>it sounds harder working</li>
</ul>
<p><br class="blank" /><br />
You can use function overloading</p>
<ul>
<li>To design a family of functions that do essentially the same thing, but using different argument lists</li>
</ul>
<p><br class="blank" /><br />
Overloaded functions are analogous to verbs having more than one meaning</p>
<ul>
<li>For example, Mr. Computerson can root at the ball park for the home team, and he can root in soil for truffles
<ul>
<li>The context (one hopes) tells you which meaning of root is intended in each case
<ul>
<li>Similarly, C++ uses the context to decide which version of an overloaded function is intended</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The key to function overloading</p>
<ul>
<li>Is a function’s argument list, also called the <em>function signature</em></li>
</ul>
<p><br class="blank" /><br />
If two functions use the same number and types of arguments in the same order</p>
<ul>
<li>They have the same signature; the variable names don’t matter</li>
</ul>
<p><br class="blank" /><br />
C++ enables you to define two functions by the same name</p>
<ul>
<li>Provided that the functions have different signatures</li>
</ul>
<p><br class="blank" /><br />
The signature can differ</p>
<ul>
<li>In the number of arguments or in the type of arguments, or both
<ul>
<li>For example, you can define a set of <strong>print()</strong> functions with the following prototypes (A-1):
<ul>
<li>When you then use a <strong>print()</strong> function, the compiler matches your use to the prototype that has the same signature (A-2):
<ul>
<li>For example, <strong>print(“Computers”, 15)</strong> uses a string and an integer as arguments, and it matches Prototype #1</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> print<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> str, <span style="color: #0000ff;">int</span> width<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// #1</span>
<span style="color: #0000ff;">void</span> print<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> d, <span style="color: #0000ff;">int</span> width<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>         <span style="color: #666666;">// #2</span>
<span style="color: #0000ff;">void</span> print<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> l, <span style="color: #0000ff;">int</span> width<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>           <span style="color: #666666;">// #3</span>
<span style="color: #0000ff;">void</span> print<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i, <span style="color: #0000ff;">int</span> width<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>            <span style="color: #666666;">// #4</span>
<span style="color: #0000ff;">void</span> print<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>str<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>             <span style="color: #666666;">// #5</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">print<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Computers&quot;</span>, <span style="color: #0000dd;">15</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// use #1</span>
print<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Keyboard&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	         <span style="color: #666666;">// use #5</span>
print<span style="color: #008000;">&#40;</span><span style="color:#800080;">2009.0</span>, <span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	         <span style="color: #666666;">// use #2</span>
print<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2009</span>, <span style="color: #0000dd;">12</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// use #4</span>
print<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2009L</span>, <span style="color: #0000dd;">15</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// use #3</span></pre></div></div>

<p><br class="blank" /><br />
When you use overloaded functions, you need to be sure you use the proper argument types in the function call</p>
<ul>
<li>For example, consider the following statements (A-3):
<ul>
<li>Which prototype does the <strong>print()</strong> call match in (A-1)? It doesn’t match any of them!</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> year <span style="color: #000080;">=</span> <span style="color: #0000dd;">3210</span><span style="color: #008080;">;</span>
print<span style="color: #008000;">&#40;</span>year, <span style="color: #0000dd;">6</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ambiguous call</span></pre></div></div>

<p><br class="blank" /><br />
A lack of a matching prototype doesn’t automatically rule out using one of the functions because C++ will try to use standard type conversions to force a match</p>
<ul>
<li>If, say, the <em>only</em> <strong>print()</strong> prototype were #2</li>
<ul>
<li>The function call <strong>print(year, 6)</strong> would convert the <strong>year</strong> value to type <strong>double</strong></li>
</ul>
<li>But in the earlier code (A-1) there are three prototypes that take a number as the first argument, providing three different choices for converting <strong>year</strong>
<ul>
<li>Faced with this ambiguous situation, C++ rejects the function call as an error</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Some signatures that appear to be different from each other nonetheless can’t coexist</p>
<ul>
<li>For example, consider these two prototypes (A-4):
<ul>
<li>You might think this is a place you could use function overloading because the function signatures appear to be different</li>
</ul>
<li>But consider things from the compiler’s standpoint
<ul>
<li>Suppose you have code like this (A-5):
<ul>
<li>The <strong>x</strong> argument matches both the <strong>double x</strong> prototype and the <strong>double &amp;x</strong> prototype
<ul>
<li>The compiler has no way of knowing which function to use</li>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<ul>
<li>Therefore, to avoid such confusion
<ul>
<li>When it checks function signatures, the compiler considers a reference to a type and the type itself to be the same signature</li>
</ul>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">double</span> cube<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> cube<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> <span style="color: #000040;">&amp;</span> x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-5)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> cube<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
The function-matching process does discriminate between <strong>const</strong> and non-<strong>const</strong> variables</p>
<ul>
<li>Consider the following prototypes (A-6):
<ul>
<li>Here’s what various function calls would match (A-7):</li>
</ul>
<li>The <strong>dribble()</strong> function has two prototypes—one for <strong>const</strong> pointers and one for regular pointers—and the compiler selects one or the other, depending on whether the actual argument is <strong>const</strong></li>
<li>The <strong>dabble()</strong> function only matches a call with a non-<strong>const</strong> argument, but the <strong>drivel()</strong> function matches calls with either <strong>const</strong> or non-<strong>const</strong> arguments
<ul>
<li>The reason for this difference in behavior between <strong>drivel()</strong> and <strong>dabble()</strong> is that it’s valid to assign a non-<strong>const</strong> value to a <strong>const</strong> variable, but not vice versa</li>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> dribble<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> bits<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// overloaded</span>
<span style="color: #0000ff;">void</span> dribble<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>cbits<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// overloaded</span>
<span style="color: #0000ff;">void</span> dabble<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> bits<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>		<span style="color: #666666;">// not overloaded</span>
<span style="color: #0000ff;">void</span> drivel<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> bits<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// not overloaded</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-7)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> p1<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;How's the weather?&quot;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">char</span> p2<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;How's business?&quot;</span><span style="color: #008080;">;</span>
dribble<span style="color: #008000;">&#40;</span>p1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// dribble(const char *);</span>
dribble<span style="color: #008000;">&#40;</span>p2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// dribble(char *);</span>
dabble<span style="color: #008000;">&#40;</span>p1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// no match</span>
dabble<span style="color: #008000;">&#40;</span>p2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// dabble(char *);</span>
drivel<span style="color: #008000;">&#40;</span>p1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// drivel(const char *);</span>
drivel<span style="color: #008000;">&#40;</span>p2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	<span style="color: #666666;">// drivel(const char *);</span></pre></div></div>

<p><br class="blank" /><br />
Keep in mind that the signature, not the function type, enables function overloading</p>
<ul>
<li>For example, the following two declarations are incompatible (A-8):</li>
<ul>
<li>Therefore, C++ doesn’t permit you to overload <strong>gronk()</strong> in this fashion</li>
</ul>
<li>You can have different return types, but only if the signatures are also different (A-9):</li>
</ul>
<p><br class="blank" /><br />
Example of (A-8)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">long</span> gronk<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n, <span style="color: #0000ff;">float</span> m<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	   <span style="color: #666666;">// same signatures,</span>
<span style="color: #0000ff;">double</span> gronk<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n, <span style="color: #0000ff;">float</span> m<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// hence not allowed</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-9)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">long</span> gronk<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n, <span style="color: #0000ff;">float</span> m<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	     <span style="color: #666666;">// different signatures,</span>
<span style="color: #0000ff;">double</span> gronk<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> n, <span style="color: #0000ff;">float</span> m<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// hence allowed</span></pre></div></div>

<p><br class="blank" /><br />
After we discuss templates later in other page</p>
<ul>
<li>We’ll further discuss function matching</li>
</ul>
<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/function-overloading/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Structures</title>
		<link>http://www.bizzymicbizness.com/cplusplus/structures</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/structures#comments</comments>
		<pubDate>Wed, 14 Oct 2009 07:52:07 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Dot Operator]]></category>
		<category><![CDATA[double]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[Member Names]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[Structure Definition]]></category>
		<category><![CDATA[Structure Tag]]></category>
		<category><![CDATA[Structure Types]]></category>
		<category><![CDATA[Structures]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=713</guid>
		<description><![CDATA[Sometimes it is useful to have a collection of values of different types and to treat the collection as a single item

For example, consider a bank certificate of deposit, which is often called a CD

A CD is a bank account that does not allow withdrawals for a specified number of months
A CD naturally has three [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is useful to have a collection of values of different types and to treat the collection as a single item</p>
<ul>
<li>For example, consider a bank certificate of deposit, which is often called a CD</li>
<ul>
<li>A CD is a bank account that does not allow withdrawals for a specified number of months</li>
<li>A CD naturally has three pieces of data associated with it:</li>
<ul>
<li>The account balance</li>
<li> The interest rate for the account</li>
<li> The term, which is the number of months until maturity</li>
</ul>
</ul>
<ul>
<li>The first two items can be represented</li>
<ul>
<li>As values of type <strong>double</strong></li>
</ul>
<li>The number of months can be represented</li>
<ul>
<li>As a value of type <strong>int</strong></li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
(A-1) shows the definition of a structure called</p>
<ul>
<li><strong>CDAccountV1</strong> that can be used for this kind of account
<ul>
<li>(The V1 stands for version 1. We will define an improved version later in this page.)</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// strctfirst.cpp -- program to demonstrate the CDAccountV1 structure type</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// structure for a bank certificate of deposit:</span>
<span style="color: #0000ff;">struct</span> CDAccountV1
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> balance<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> interestRate<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> term<span style="color: #008080;">;</span> <span style="color: #666666;">// months until maturity</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> getData<span style="color: #008000;">&#40;</span>CDAccountV1<span style="color: #000040;">&amp;</span> theAccount<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// postcondition: theAccount.balance, theAccount.interestRate, and </span>
<span style="color: #666666;">// theAccount.term have been given values that the user entered at the keyboard</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    CDAccountV1 account<span style="color: #008080;">;</span>
    getData<span style="color: #008000;">&#40;</span>account<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">double</span> rateFraction, interest<span style="color: #008080;">;</span>
    rateFraction <span style="color: #000080;">=</span> account.<span style="color: #007788;">interestRate</span><span style="color: #000040;">/</span><span style="color:#800080;">100.0</span><span style="color: #008080;">;</span>
    interest <span style="color: #000080;">=</span> account.<span style="color: #007788;">balance</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>rateFraction<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>account.<span style="color: #007788;">term</span><span style="color: #000040;">/</span><span style="color:#800080;">12.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    account.<span style="color: #007788;">balance</span> <span style="color: #000080;">=</span> account.<span style="color: #007788;">balance</span> <span style="color: #000040;">+</span> interest<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span>.<span style="color: #007788;">setf</span><span style="color: #008000;">&#40;</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">fixed</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span>.<span style="color: #007788;">setf</span><span style="color: #008000;">&#40;</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">showpoint</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span>.<span style="color: #007788;">precision</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;When your CD matures in &quot;</span> 
         <span style="color: #000080;">&lt;&lt;</span> account.<span style="color: #007788;">term</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; months,<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
         <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;it will have a balance of $&quot;</span> 
         <span style="color: #000080;">&lt;&lt;</span> account.<span style="color: #007788;">balance</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// uses iostream:</span>
<span style="color: #0000ff;">void</span> getData<span style="color: #008000;">&#40;</span>CDAccountV1<span style="color: #000040;">&amp;</span> theAccount<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter account balance: $&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theAccount.<span style="color: #007788;">balance</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter account interest rate: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theAccount.<span style="color: #007788;">interestRate</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter the number of months until maturity: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theAccount.<span style="color: #007788;">term</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here is the output from the program in (A-1):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-714" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output3.jpg" alt="Output" width="536" height="247" /></p>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Structure Types</h2>
<p><br class="blank" /><br />
The structure definition in (A-1)</p>
<ul>
<li>Is as follows (A-2):</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> CDAccountV1
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">double</span> balance<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> interestRate<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> term<span style="color: #008080;">;</span> <span style="color: #666666;">// months until maturity</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
The keyword <strong>struct</strong></p>
<ul>
<li>Announces that this is a structure type definition</li>
</ul>
<p><br class="blank" /><br />
The identifier <strong>CDAccountV1</strong></p>
<ul>
<li>Is the name of the structure type, which is known as the structure tag
<ul>
<li>The structure tag can be any legal identifier that is not a keyword
<ul>
<li>Although this is not required by the C++ language, structure tags are usually spelled starting with an uppercase letter</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The identifiers declared inside the braces, <strong>{}</strong></p>
<ul>
<li>Are called member names</li>
</ul>
<p><br class="blank" /><br />
As illustrated in (A-2), a structure type definition ends</p>
<ul>
<li>With a closing brace and a semicolon</li>
</ul>
<p><br class="blank" /><br />
A structure definition</p>
<ul>
<li>Is usually placed outside any function definition
<ul>
<li>(In the same way that globally defined constant declarations are placed outside all function definitions.)</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The structure type</p>
<ul>
<li>Is then a global definition that is available to all the code that follows the structure definition</li>
</ul>
<p><br class="blank" /><br />
Once a structure type definition has been given</p>
<ul>
<li>The structure type can be used just like the predefined types <strong>int</strong>, <strong>char</strong>, and so forth</li>
</ul>
<p><br class="blank" /><br />
Note that in (A-1) the structure type <strong>CDAccountV1</strong></p>
<ul>
<li>Is used to declare a variable in the function <strong>main</strong>
<ul>
<li>And then it is used as the name of the parameter type for the function <strong>getData</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
A structure variable</p>
<ul>
<li>Can hold values just like any other variable can</li>
</ul>
<p><br class="blank" /><br />
A structure value</p>
<ul>
<li>Is a collection of smaller values called member values</li>
</ul>
<p><br class="blank" /><br />
There is one member value for each member name declared in the structure definition</p>
<ul>
<li>For example, a value of the type <strong>CDAccountV1</strong>
<ul>
<li>Is a collection of three member values, two of type <strong>double</strong> and one of type <strong>int</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The member values that together make up the structure value</p>
<ul>
<li>Are stored in member variables, which we discuss next</li>
</ul>
<p><br class="blank" /><br />
Each structure type specifies a list of member names</p>
<ul>
<li>In (A-1) the structure <strong>CDAccountV1</strong> has three member names:</li>
<ul>
<li><strong>balance</strong></li>
<li><strong>interestRate</strong></li>
<li><strong>term</strong></li>
</ul>
<li>Each of these member names can be used to pick out one smaller variable that is a part of the larger structure variable</li>
<ul>
<li>These smaller variables are called member variables</li>
</ul>
</ul>
<p><br class="blank" /><br />
Member variables</p>
<ul>
<li>Are specified by giving the name of the structure variable followed by a dot and then the member name</li>
<ul>
<li>For example, if <strong>account</strong> is a structure variable of type <strong>CDAccountV1 </strong>(as declared in(A-1)), then the structure variable <strong>account</strong> has the following three member variables:</li>
<ul>
<li><strong>account.balance</strong></li>
<li><strong>account.interestRate</strong></li>
<li><strong>account.term</strong></li>
</ul>
<li>The first two member variables are of type <strong>double</strong>, and the last is of type <strong>int</strong></li>
<li>As illustrated in (A-1), these member variables can be used just like any other variables of those types</li>
<ul>
<li>For example, the following line from the program in (A-1) will add the value contained in the member variable <strong>account.balance</strong> and the value contained in the ordinary variable <strong>interest</strong> and will then place the result in the member variable <strong>account.balance </strong>(A-3):</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">account.<span style="color: #007788;">balance</span> <span style="color: #000080;">=</span> account.<span style="color: #007788;">balance</span> <span style="color: #000040;">+</span> interest<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Two or more structure types may use the same member names</p>
<ul>
<li>For example, it is perfectly legal to have the following two type definitions in the same program (A-4)
<ul>
<li>This coincidence of names will produce no problems</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> FertilizerStock
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">double</span> quantity<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> nitrogenContent<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> CropYield
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> quantity<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> size<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
For example, if you declare the following two structure variables (A-5):</p>
<ul>
<li>Then the quantity of Super Grow fertilizer is stored in the member variable <strong>superGrow.quantity</strong>
<ul>
<li>And the quantity of apples produced is stored in the member variable <strong>apples.quantity</strong>
<ul>
<li>The dot operator and the structure variable specify which <strong>quantity</strong> is meant in each instance</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-5)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">FertilizerStock superGrow<span style="color: #008080;">;</span>
CropYield apples<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
A structure value can be viewed as a collection of member values</p>
<ul>
<li>A structure value can also be viewed as a single (complex) value (that just happens to be made up of member values)</li>
</ul>
<p><br class="blank" /><br />
Since a structure value can be viewed as a single value</p>
<ul>
<li>Structure values and structure variables can be used in the same ways that you use simple values and simple variables of the predefined types such as <strong>int</strong>
<ul>
<li>In particular, you can assign structure values using the equal sign</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
For example, if <strong>apples</strong> and <strong>oranges</strong> are structure variables of the type <strong>CropYield </strong>defined earlier, then the following is perfectly legal (A-6):</p>
<ul>
<li>The previous assignment statement is equivalent to (A-7)</li>
</ul>
<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">apples <span style="color: #000080;">=</span> oranges<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-7)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">apples.<span style="color: #007788;">quantity</span> <span style="color: #000080;">=</span> oranges.<span style="color: #007788;">quantity</span><span style="color: #008080;">;</span>
apples.<span style="color: #007788;">size</span> <span style="color: #000080;">=</span> oranges.<span style="color: #007788;">size</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Forgetting a Semicolon in a Structure Definition</h2>
<p><br class="blank" /><br />
When you add the final brace,<strong>}</strong>, to a structure definition, it feels like the structure definition is finished, but it is not</p>
<ul>
<li>You must also place a semicolon after that final brace
<ul>
<li>There is a reason for this, even though the reason is a feature that we will have no occasion to use</li>
</ul>
<li>A structure definition is more than a definition
<ul>
<li>It can also be used to declare structure variables
<ul>
<li>You are allowed to list structure variable names between that final brace and that final semicolon
<ul>
<li>For example, the following defines a structure called <strong>WeatherData</strong> and declares two structure variables, <strong>dataPoint1</strong> and <strong>dataPoint2</strong>, both of type <strong>WeatherData</strong> (A-8):</li>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-8)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> WeatherData
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">double</span> temperature<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> windVelocity<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> dataPoint1, dataPoint2<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Structures as Function Arguments</h2>
<p><br class="blank" /><br />
A function can have</p>
<ul>
<li>Call-by-value parameters of a structure type or call-by-reference parameters of a structure type, or both
<ul>
<li>The program in (A-1), for example, includes a function named <strong>getData</strong> that has a call-by-reference parameter with the structure type <strong>CDAccountV1</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
A structure type can also be the type for the value returned by a function</p>
<ul>
<li>For example, the following defines a function that takes one argument of type <strong>CDAccountV1</strong> and returns a different structure of type <strong>CDAccountV1 </strong>(A-9)
<ul>
<li>The structure returned will have the same balance and term as the argument, but will pay double the interest rate that the argument pays</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-9)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">CDAccountV1 doubleInterest<span style="color: #008000;">&#40;</span>CDAccountV1 oldAccount<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	CDAccountV1 temp<span style="color: #008080;">;</span>
	temp <span style="color: #000080;">=</span> oldAccount<span style="color: #008080;">;</span>
	temp.<span style="color: #007788;">interestRate</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #000040;">*</span>oldAccount.<span style="color: #007788;">interestRate</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> temp<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
(A-9) notice the local variable <strong>temp</strong> of type <strong>CDAccountV1</strong>; <strong>temp</strong> is used to build up a complete structure value of the desired kind</p>
<ul>
<li>Which is then returned by the function</li>
</ul>
<p><br class="blank" /><br />
If <strong>myAccount</strong> is a variable of type <strong>CDAccountV1</strong> that has been given values for its member variables</p>
<ul>
<li>Then the following will give <strong>yourAccount</strong> values for an account with double the interest rate of <strong>myAccount </strong>(A-10):</li>
</ul>
<p><br class="blank" /><br />
Example of (A-10)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">CDAccountV1 yourAccount<span style="color: #008080;">;</span>
yourAccount <span style="color: #000080;">=</span> doubleInterest<span style="color: #008000;">&#40;</span>myAccount<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Use Hierarchical Structures</h2>
<p><br class="blank" /><br />
Sometimes it makes sense to have structures whose members are themselves smaller structures</p>
<ul>
<li>For example, a structure type called <strong>PersonInfo</strong>
<ul>
<li>That can be used to store a person’s height, weight, and birth date can be defined as follows (A-11):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-11)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> Date
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> month<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> day<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> year<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> PersonInfo
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">double</span> height<span style="color: #008080;">;</span> <span style="color: #666666;">// in inches</span>
	<span style="color: #0000ff;">int</span> weight<span style="color: #008080;">;</span> <span style="color: #666666;">// in pounds</span>
	Date birthday<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
A structure variable of type <strong>PersonInfo</strong> is declared in the usual way (A-12):<br />
<br class="blank" /><br />
Example of (A-12)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">PersonInfo person1<span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
If the structure variable <strong>person1</strong> has had its value set to record a person’s birth date</p>
<ul>
<li>Then the year the person was born can be output to the screen as follows (A-13):</li>
</ul>
<p><br class="blank" /><br />
Example of (A-13)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> person1.<span style="color: #007788;">birthday</span>.<span style="color: #007788;">year</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
The way to read such expressions is left to right, and very carefully</p>
<ul>
<li>Starting at the left end, <strong>person1</strong> is a structure variable of type <strong>PersonInfo</strong>
<ul>
<li>To obtain the member variable with the name <strong>birthday</strong>, you use the dot operator as follows (A-14):
<ul>
<li>This member variable is itself a structure variable of type <strong>Date</strong>
<ul>
<li>Thus, this member variable itself has member variables
<ul>
<li>A member variable of the structure variable <strong>person1.birthday</strong> is obtained by adding a dot and the member variable name, such as <strong>year</strong>, which produces the expression <strong>person1.birthday.year</strong> shown above (A-13)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-14)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">person1.<span style="color: #007788;">birthday</span></pre></div></div>

<p><br class="blank" /><br />
In (A-15) we have rewritten the class for a certificate of deposit from (A-1)</p>
<ul>
<li>This new version has a member variable of the structure type <strong>Date</strong> that holds the date of maturity
<ul>
<li>We have also replaced the single <strong>balance</strong> member variable with two new member variables giving the initial balance and the balance at maturity</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-15)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// strctsec.cpp -- program to demonstrate the CDAccount structure type</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> Date
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> month<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> day<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> year<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// improved structure for a bank certificate of deposit:</span>
<span style="color: #0000ff;">struct</span> CDAccount
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> initialBalance<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> interestRate<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> term<span style="color: #008080;">;</span> <span style="color: #666666;">// months until maturity</span>
    Date maturity<span style="color: #008080;">;</span> <span style="color: #666666;">// date when CD matures</span>
    <span style="color: #0000ff;">double</span> balanceAtMaturity<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> getCDData<span style="color: #008000;">&#40;</span>CDAccount<span style="color: #000040;">&amp;</span> theAccount<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// postcondition: theAccount.initialBalance, theAccount.interestRate, </span>
<span style="color: #666666;">// theAccount.term, and theAccount.maturity </span>
<span style="color: #666666;">// have been given values that the user entered at the keyboard</span>
&nbsp;
<span style="color: #0000ff;">void</span> getDate<span style="color: #008000;">&#40;</span>Date<span style="color: #000040;">&amp;</span> theDate<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// postcondition: theDate.month, theDate.day, and theDate.year </span>
<span style="color: #666666;">// have been given values that the user entered at the keyboard</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    CDAccount account<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter account data on the day account was opened:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
    getCDData<span style="color: #008000;">&#40;</span>account<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">double</span> rateFraction, interest<span style="color: #008080;">;</span>
    rateFraction <span style="color: #000080;">=</span> account.<span style="color: #007788;">interestRate</span><span style="color: #000040;">/</span><span style="color:#800080;">100.0</span><span style="color: #008080;">;</span>
    interest <span style="color: #000080;">=</span> account.<span style="color: #007788;">initialBalance</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>rateFraction<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>account.<span style="color: #007788;">term</span><span style="color: #000040;">/</span><span style="color:#800080;">12.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    account.<span style="color: #007788;">balanceAtMaturity</span> <span style="color: #000080;">=</span> account.<span style="color: #007788;">initialBalance</span> <span style="color: #000040;">+</span> interest<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span>.<span style="color: #007788;">setf</span><span style="color: #008000;">&#40;</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">fixed</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span>.<span style="color: #007788;">setf</span><span style="color: #008000;">&#40;</span>ios<span style="color: #008080;">::</span><span style="color: #007788;">showpoint</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span>.<span style="color: #007788;">precision</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;When the CD matured on &quot;</span> 
         <span style="color: #000080;">&lt;&lt;</span> account.<span style="color: #007788;">maturity</span>.<span style="color: #007788;">month</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-&quot;</span> <span style="color: #000080;">&lt;&lt;</span> account.<span style="color: #007788;">maturity</span>.<span style="color: #007788;">day</span>
         <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-&quot;</span> <span style="color: #000080;">&lt;&lt;</span> account.<span style="color: #007788;">maturity</span>.<span style="color: #007788;">year</span> <span style="color: #000080;">&lt;&lt;</span> endl
         <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;it had a balance of $&quot;</span> 
         <span style="color: #000080;">&lt;&lt;</span> account.<span style="color: #007788;">balanceAtMaturity</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// uses iostream:</span>
<span style="color: #0000ff;">void</span> getCDData<span style="color: #008000;">&#40;</span>CDAccount<span style="color: #000040;">&amp;</span> theAccount<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter account initial balance: $&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theAccount.<span style="color: #007788;">initialBalance</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter account interest rate: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theAccount.<span style="color: #007788;">interestRate</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter the number of months until maturity: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theAccount.<span style="color: #007788;">term</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter the maturity date:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
    getDate<span style="color: #008000;">&#40;</span>theAccount.<span style="color: #007788;">maturity</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// uses iostream:</span>
<span style="color: #0000ff;">void</span> getDate<span style="color: #008000;">&#40;</span>Date<span style="color: #000040;">&amp;</span> theDate<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter month: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theDate.<span style="color: #007788;">month</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter day: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theDate.<span style="color: #007788;">day</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter year: &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> theDate.<span style="color: #007788;">year</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here is the output from the program in (A-15): </p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-715" title="Output Second" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output-Second.jpg" alt="Output Second" width="530" height="245" /></p>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Initializing Structures</h2>
<p><br class="blank" /><br />
You can initialize a structure at the time that it is declared</p>
<ul>
<li>To give a structure variable a value, follow it by an equal sign and a list of the member values enclosed in braces</li>
</ul>
<p><br class="blank" /><br />
For example, the following definition of a structure type for a date was given in the previous subsection (A-16):</p>
<ul>
<li>Once the type <strong>Date</strong> is defined, you can declare and initialize a structure variable called <strong>dueDate</strong> as follows (A-17):
<ul>
<li>The initializing values must be given in the order that corresponds to the order of member variables in the structure type definition
<ul>
<li>In this example (A-17):
<ul>
<li><strong>dueDate.month</strong> receives the first initializing value of <strong>12</strong></li>
<li><strong>dueDate.day</strong> receives the second value of <strong>31</strong></li>
<li><strong>dueDate.year</strong> receives the third value of <strong>2003</strong></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
It is an error if there are more initializer values than <strong>struct</strong> members</p>
<ul>
<li>If there are fewer initializer values than <strong>struct</strong> members, the provided values are used to initialize data members, in order
<ul>
<li>Each data member without an initializer is initialized to a zero value of an appropriate type for the variable</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-16)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> Date
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> month<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> day<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> year<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-17)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Date dueDate <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">12</span>, <span style="color: #0000dd;">31</span>, <span style="color: #0000dd;">2003</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/structures/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Member Access Control: Public or Private?</title>
		<link>http://www.bizzymicbizness.com/cplusplus/member-access-control-public-or-private</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/member-access-control-public-or-private#comments</comments>
		<pubDate>Sat, 10 Oct 2009 01:38:23 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Private]]></category>
		<category><![CDATA[Private Member Functions]]></category>
		<category><![CDATA[Public]]></category>
		<category><![CDATA[Public Interface]]></category>
		<category><![CDATA[Structures]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=650</guid>
		<description><![CDATA[You can declare class members

Whether they are data items or member functions

Either in the public or the private section of a class



 

But because one of the main precepts of OOP is to hide the data

Data items normally go into the private section

 

The member functions that constitute the class interface

Go into the public section

Otherwise, you can’t [...]]]></description>
			<content:encoded><![CDATA[<p>You can declare class members</p>
<ul>
<li>Whether they are data items or member functions
<ul>
<li>Either in the public or the private section of a class</li>
</ul>
</li>
</ul>
<p> <br />
<br class="blank" /><br />
But because one of the main precepts of OOP is to hide the data</p>
<ul>
<li>Data items normally go into the private section</li>
</ul>
<p> <br />
<br class="blank" /><br />
The member functions that constitute the class interface</p>
<ul>
<li>Go into the public section
<ul>
<li>Otherwise, you can’t call those functions from a program</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You can also put member functions</p>
<ul>
<li>In the private section
<ul>
<li>You can’t call such functions directly from a program, but the public methods can use them
<ul>
<li>Typically, you use private member functions to handle implementation details that don’t form part of the public interface</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You don’t have to use the keyword <strong>private</strong> in class declarations</p>
<ul>
<li>Because that is the default access control for class objects (A-1):
<ul>
<li>However, I will explicitly use the <strong>private</strong> label in order to emphasize the concept of data hiding</li>
</ul>
</li>
</ul>
<p> <br />
<br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> World
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">float</span> mass<span style="color: #008080;">;</span> <span style="color: #666666;">// private by default</span>
	<span style="color: #0000ff;">char</span> name<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// private by default</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">void</span> tellall<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	...
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Classes and Structures</h2>
<p><br class="blank" /><br />
Class descriptions look much like structure declarations</p>
<ul>
<li>With the addition of member functions</li>
<ul>
<li>And the <strong>public</strong> and <strong>private</strong> visibility labels</li>
<ul>
<li>In fact, C++ extends to structures the same features classes have</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
The only difference is that</p>
<ul>
<li>The default access type for a structure is <strong>public</strong></li>
<ul>
<li>Whereas the default type for a class is <strong>private</strong></li>
</ul>
</ul>
<p><br class="blank" /><br />
C++ programmers commonly use</p>
<ul>
<li>Classes to implement class descriptions while restricting structures to representing pure data objects</li>
<ul>
<li>Or, occasionally, classes with no private components</li>
</ul>
</ul>
<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/member-access-control-public-or-private/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Inline Functions</title>
		<link>http://www.bizzymicbizness.com/cplusplus/c-inline-functions</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/c-inline-functions#comments</comments>
		<pubDate>Sat, 10 Oct 2009 00:47:41 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[#define]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Function Call]]></category>
		<category><![CDATA[Inline Functions]]></category>
		<category><![CDATA[Macros]]></category>
		<category><![CDATA[Memory Address]]></category>
		<category><![CDATA[Stack]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=640</guid>
		<description><![CDATA[Inline functions

Are a C++ enhancement designed to speed up programs


The primary distinction between normal functions and inline functions

Is not in how you code them

But in how the C++ compiler incorporates them into a program




To understand the distinction between inline functions and normal functions

You need to peer more deeply into a program’s innards than we have [...]]]></description>
			<content:encoded><![CDATA[<p><em>Inline functions</em></p>
<ul>
<li>Are a C++ enhancement designed to speed up programs</li>
</ul>
<p><br class="blank" /><br />
The primary distinction between normal functions and inline functions</p>
<ul>
<li>Is not in how you code them
<ul>
<li>But in how the C++ compiler incorporates them into a program</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
To understand the distinction between inline functions and normal functions</p>
<ul>
<li>You need to peer more deeply into a program’s innards than we have so far
<ul>
<li>Let’s do that now</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The final product of the compilation process</p>
<ul>
<li>Is an executable program, which consists of a set of machine language instructions
<ul>
<li>When you start a program, the operating system loads these instructions into the computer’s memory so that each instruction has a particular memory address
<ul>
<li>The computer then goes through these instructions step-by-step</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Sometimes, as when you have a loop or a branching statement</p>
<ul>
<li>Program execution skips over instructions, jumping backward or forward to a particular address</li>
</ul>
<p><br class="blank" /><br />
Normal function calls</p>
<ul>
<li>Also involve having a program jump to another address (the function’s address)
<ul>
<li>And then jump back when the function terminates</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Let’s look at a typical implementation of that process in a little more detail</p>
<ul>
<li>When a program reaches the function call instruction
<ul>
<li>The program stores the memory address of the instruction immediately following the function call
<ul>
<li>Copies function arguments to the stack (a block of memory reserved for that purpose)
<ul>
<li>Jumps to the memory location that marks the beginning of the function
<ul>
<li>Executes the function code (perhaps placing a return value in a register)
<ul>
<li>And then jumps back to the instruction whose address it saved</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Jumping back and forth and keeping track of where to jump</p>
<ul>
<li>Means that there is an overhead in elapsed time to using functions</li>
</ul>
<p><br class="blank" /><br />
C++ inline functions</p>
<ul>
<li>Provide an alternative</li>
</ul>
<p><br class="blank" /><br />
In an inline function</p>
<ul>
<li>The compiled code is “in line” with the other code in the program
<ul>
<li>That is, the compiler replaces the function call with the corresponding function code</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
With inline code</p>
<ul>
<li>The program doesn’t have to jump to another location to execute the code and then jump back
<ul>
<li>Inline functions thus run a little faster than regular functions
<ul>
<li>But they come with a memory penalty</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
If a program calls an inline function at 10 separate locations</p>
<ul>
<li>Then the program winds up with 10 copies of the function inserted into the code
<ul>
<li>See (Image-1)</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
(Image-1)  Inline functions versus regular functions</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-641" title="Inline functions versus regular functions" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Inline-functions-versus-regular-functions.jpg" alt="Inline functions versus regular functions" width="551" height="418" /></p>
<p><br class="blank" /><br />
You should be selective about using inline functions</p>
<ul>
<li>If the time needed to execute the function code is long compared to the time needed to handle the function call mechanism</li>
<ul>
<li>Then the time saved is a relatively small portion of the entire process</li>
</ul>
<li>If the code execution time is short</li>
<ul>
<li>Then an inline call can save a large portion of the time used by the non-inline call</li>
<ul>
<li>On the other hand, you are now saving a large portion of a relatively quick process, so the absolute time savings may not be that great unless the function is called frequently</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
To use this feature, you must take at least one of two actions:</p>
<ul>
<li>Preface the function declaration with the keyword <strong>inline</strong></li>
<li>Preface the function definition with the keyword <strong>inline</strong></li>
</ul>
<p><br class="blank" /><br />
A common practice is</p>
<ul>
<li>To omit the prototype
<ul>
<li>And to place the entire definition (meaning the function header and all the function code) where the prototype would normally go</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The compiler does not have to honor your request to make a function inline</p>
<ul>
<li>It might decide the function is too large
<ul>
<li>Or notice that it calls itself (recursion is not allowed or indeed possible for inline functions)
<ul>
<li>Or the feature might not be turned on or implemented for your particular compiler</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
(A-1) illustrates the inline technique with an inline <strong>square()</strong> function that squares its argument</p>
<ul>
<li>Note that placed the entire definition is on one line
<ul>
<li>That’s not required, but if the definition doesn’t fit on one line, the function is probably a poor candidate for an inline function</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// inline.cpp -- using an inline function</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #666666;">// an inline function definition</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">double</span> square<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> x <span style="color: #000040;">*</span> x<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> a, b<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> c <span style="color: #000080;">=</span> <span style="color:#800080;">13.0</span><span style="color: #008080;">;</span>
&nbsp;
	a <span style="color: #000080;">=</span> square<span style="color: #008000;">&#40;</span><span style="color:#800080;">5.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	b <span style="color: #000080;">=</span> square<span style="color: #008000;">&#40;</span><span style="color:#800080;">4.5</span> <span style="color: #000040;">+</span> <span style="color:#800080;">7.5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// can pass expressions</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;a = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> a <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, b = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> b <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;c = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> c<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, c squared = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> square<span style="color: #008000;">&#40;</span>c<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Now c = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here’s the output of the program in (A-1):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-642" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output2.jpg" alt="Output" width="536" height="247" /></p>
<p><br class="blank" /><br />
This output illustrates that inline functions pass arguments by value just like regular functions do</p>
<ul>
<li>If the argument is an expression, such as <strong>4.5 + 7.5</strong>, the function passes the value of the expression—<strong>12</strong> in this case
<ul>
<li>This makes C++’s inline facility far superior to C’s macro definitions
<ul>
<li>See the “Inline Versus Macros”</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Even though the program doesn’t provide a separate prototype, C++’s prototyping features are still in play</p>
<ul>
<li>That’s because the entire definition, which comes before the function’s first use, serves as a prototype
<ul>
<li>This means you can use <strong>square()</strong> with an <strong>int</strong> argument or a <strong>long</strong> argument
<ul>
<li>And the program automatically type casts the value to type <strong>double</strong> before passing it to the function</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Inline Versus Macros</h2>
<p><br class="blank" /><br />
The <strong>inline</strong> facility is an addition to C++</p>
<ul>
<li>C uses the preprocessor <strong>#define</strong> statement to provide <em>macros</em>
<ul>
<li>Which are crude implementations of inline code
<ul>
<li>For example, here’s a macro for squaring a number (A-2):</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define SQUARE(X) X*X</span></pre></div></div>

<p><br class="blank" /><br />
(A-2) this works not by passing arguments but through text substitution</p>
<ul>
<li>With the <strong>X</strong> acting as a symbolic label for the “argument” (A-3):</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">a <span style="color: #000080;">=</span> SQUARE<span style="color: #008000;">&#40;</span><span style="color:#800080;">5.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// is replaced by a = 5.0*5.0;</span>
b <span style="color: #000080;">=</span> SQUARE<span style="color: #008000;">&#40;</span><span style="color:#800080;">4.5</span> <span style="color: #000040;">+</span> <span style="color:#800080;">7.5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// is replaced by b = 4.5 + 7.5 * 4.5 + 7.5;</span>
d <span style="color: #000080;">=</span> SQUARE<span style="color: #008000;">&#40;</span>c<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// is replaced by d = c++*c++;</span></pre></div></div>

<p><br class="blank" /><br />
(A-3) only the first example here works properly</p>
<ul>
<li>You can improve matters with a liberal application of parentheses (A-4):</li>
</ul>
<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define SQUARE(X) ((X)*(X))</span></pre></div></div>

<p><br class="blank" /><br />
Still, the problem remains that macros don’t pass by value</p>
<ul>
<li>Even with this new definition, <strong>SQUARE(c++)</strong> increments <strong>c</strong> twice
<ul>
<li>But the inline <strong>square()</strong> function in (A-1) evaluates <strong>c</strong>, passes that value to be squared, and then increments <strong>c</strong> once</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The intent here is not to show you how to write C macros</p>
<ul>
<li>Rather, it is to suggest that if you have been using C macros to perform function-like services
<ul>
<li>You should consider converting them to C++ inline functions</li>
</ul>
</li>
</ul>
<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/c-inline-functions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passing and Returning Structures</title>
		<link>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures#comments</comments>
		<pubDate>Thu, 08 Oct 2009 04:25:44 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[Function Argument Types]]></category>
		<category><![CDATA[Function Return Types]]></category>
		<category><![CDATA[Passing and Returning Structures]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[Variables]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=622</guid>
		<description><![CDATA[Passing structures by value makes the most sense when the structure is relatively compact

So let’s look at a couple examples along those lines


The first example deals with travel time (not to be confused with time travel)

Some maps will tell you that

It is 2 hours, 56 minutes, from Zero One City to Binary Falls
And 1 hour, [...]]]></description>
			<content:encoded><![CDATA[<p>Passing structures by value makes the most sense when the structure is relatively compact</p>
<ul>
<li>So let’s look at a couple examples along those lines</li>
</ul>
<p><br class="blank" /><br />
The first example deals with travel time (not to be confused with time travel)</p>
<ul>
<li>Some maps will tell you that
<ul>
<li>It is 2 hours, 56 minutes, from Zero One City to Binary Falls</li>
<li>And 1 hour, 16 minutes, from Binary Falls to Hexadecimal Valley</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You can use a structure to represent such times</p>
<ul>
<li>Using one member for the hour value</li>
<li>And a second member for the minute value</li>
</ul>
<p><br class="blank" /><br />
Adding two times is a little tricky</p>
<ul>
<li>Because you might have to transfer some of the minutes to the hours part
<ul>
<li>For example, the two preceding times sum to 3 hours, 72 minutes, which should be converted to 4 hours, 12 minutes</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Let’s develop a structure to represent a time value</p>
<ul>
<li>And then a function that takes two such structures as arguments
<ul>
<li>And returns a structure that represents their sum</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Defining the structure is simple (A-1):<br />
<br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> travel_time
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> hours<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> mins<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Next, consider the prototype for a <strong>sum()</strong> function that returns the sum of two such structures</p>
<ul>
<li>The return value should be type <strong>travel_time</strong>, and so should the two arguments
<ul>
<li>Thus, the prototype should look like this (A-2):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
To add two times, you first add the minute members </p>
<ul>
<li>Integer division by 60 yields the number of hours to carry over</li>
<ul>
<li>And the modulus operation (<strong>%</strong>) yields the number of minutes left</li>
</ul>
</ul>
<p><br class="blank" /><br />
(A-3) incorporates the above approach into the <strong>sum()</strong> function</p>
<ul>
<li>And adds a <strong>show_time()</strong> function
<ul>
<li>To display the contents of a <strong>travel_time</strong> structure</li>
</ul>
</li>
</ul>
<p> <br />
<br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// travel.cpp -- using structures with functions</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">struct</span> travel_time
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> hours<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> mins<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> Mins_per_hr <span style="color: #000080;">=</span> <span style="color: #0000dd;">60</span><span style="color: #008080;">;</span>
&nbsp;
travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> show_time<span style="color: #008000;">&#40;</span>travel_time t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	travel_time day1 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">56</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 2 hrs, 56 min</span>
	travel_time day2 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">16</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 1 hrs, 16 min</span>
&nbsp;
	travel_time trip <span style="color: #000080;">=</span> sum<span style="color: #008000;">&#40;</span>day1, day2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Two-day total: &quot;</span><span style="color: #008080;">;</span>
	show_time<span style="color: #008000;">&#40;</span>trip<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	travel_time day3<span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">5</span>, <span style="color: #0000dd;">12</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Three-day total: &quot;</span><span style="color: #008080;">;</span>
	show_time<span style="color: #008000;">&#40;</span>sum<span style="color: #008000;">&#40;</span>trip, day3<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	travel_time total<span style="color: #008080;">;</span>
&nbsp;
	total.<span style="color: #007788;">mins</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>t1.<span style="color: #007788;">mins</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">mins</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> Mins_per_hr<span style="color: #008080;">;</span>
	total.<span style="color: #007788;">hours</span> <span style="color: #000080;">=</span> t1.<span style="color: #007788;">hours</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">hours</span> <span style="color: #000040;">+</span>
		    <span style="color: #008000;">&#40;</span>t1.<span style="color: #007788;">mins</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">mins</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> Mins_per_hr<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> show_time<span style="color: #008000;">&#40;</span>travel_time t<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> t.<span style="color: #007788;">hours</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; hours, &quot;</span>
	     <span style="color: #000080;">&lt;&lt;</span> t.<span style="color: #007788;">mins</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; minutes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here <strong>travel_time</strong> acts just like a standard type name; you can use it to</p>
<ul>
<li>Declare variables</li>
<li>Function return types</li>
<li>Function argument types</li>
</ul>
<p><br class="blank" /><br />
Because variables such as <strong>total</strong> and <strong>t1</strong> are <strong>travel_time</strong> structures</p>
<ul>
<li>You can apply the dot membership operator to them</li>
</ul>
<p><br class="blank" /><br />
Note that because the <strong>sum()</strong> function returns a <strong>travel_time</strong> structure</p>
<ul>
<li>You can use it as an argument for the <strong>show_time()</strong> function</li>
</ul>
<p><br class="blank" /><br />
Because C++ functions, by default, pass arguments by value</p>
<ul>
<li>The <strong>show_time(sum(trip, day3))</strong> function call</li>
<ul>
<li>First evaluates the <strong>sum(trip, day3)</strong> function call in order to find its return value</li>
</ul>
<li>The <strong>show_time()</strong> call</li>
<ul>
<li>Then passes <strong>sum()</strong>’s return value, not the function itself to <strong>show_time()</strong></li>
</ul>
</ul>
<p><br class="blank" /><br />
Here’s the output of the program in (A-3):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-623" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output1.jpg" alt="Output" width="597" height="274" /></p>
<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The switch Statement</title>
		<link>http://www.bizzymicbizness.com/cplusplus/the-switch-statement</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/the-switch-statement#comments</comments>
		<pubDate>Thu, 08 Oct 2009 01:30:53 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[break]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[Enumerators]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[integer-expression]]></category>
		<category><![CDATA[switch]]></category>
		<category><![CDATA[The While Loop]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=610</guid>
		<description><![CDATA[Suppose you create a screen menu that asks the user to select one of five choices—for example

Cheap
Moderate
Expensive
Extravagant
Excessive


You can extend an if else if else sequence to handle five alternatives

But the C++ switch statement more easily handles selecting a choice from an extended list

Here’s the general form for a switch statement (A-1):




Example of (A-1)



switch (integer-expression)
{
      case [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you create a screen menu that asks the user to select one of five choices—for example</p>
<ul>
<li>Cheap</li>
<li>Moderate</li>
<li>Expensive</li>
<li>Extravagant</li>
<li>Excessive</li>
</ul>
<p><br class="blank" /><br />
You can extend an <strong>if else if else</strong> sequence to handle five alternatives</p>
<ul>
<li>But the C++ <strong>switch</strong> statement more easily handles selecting a choice from an extended list
<ul>
<li>Here’s the general form for a <strong>switch</strong> statement (A-1):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>
<div class="syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">
switch (<em>integer-expression</em>)
{
      case <em>label1</em> : <em>statement(s)</em>
      case <em>label2</em> : <em>statement(s)</em>
      ...
      default : <em>statement(s)</em>
}
</pre>
</div>
</div>
<p><br class="blank" /><br />
A C++ <strong>switch</strong> statement acts as a routing device</p>
<ul>
<li>That tells the computer which line of code to execute next</li>
</ul>
<p><br class="blank" /><br />
On reaching a <strong>switch</strong> statement</p>
<ul>
<li>A program jumps to the line labeled with the value corresponding to the value of <strong><em>integer-expression</em></strong>
<ul>
<li>For example, if <strong><em>integer-expression</em></strong> has the value <strong>4</strong>, the program goes to the line that has a <strong>case 4:</strong> label</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
The value <strong><em>integer-expression</em></strong></p>
<ul>
<li>As the name suggests, must be an expression that reduces to an integer value</li>
</ul>
<p><br class="blank" /><br />
Also, each label</p>
<ul>
<li>Must be an integer constant expression</li>
</ul>
<p><br class="blank" /><br />
Most often, labels</p>
<ul>
<li>Are simple <strong>int</strong> or <strong>char</strong> constants, such as <strong>1</strong> or ‘<strong>q</strong>’, or enumerators</li>
</ul>
<p><br class="blank" /><br />
If <strong><em>integer-expression</em></strong> doesn’t match any of the labels</p>
<ul>
<li>The program jumps to the line labeled <strong>default</strong>
<ul>
<li>The <strong>default</strong> label is optional
<ul>
<li>If you omit it and there is no match, the program jumps to the next statement following the <strong>switch</strong>
<ul>
<li>See (Image-1)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
(Image-1) The structure of <strong>switch</strong> statements</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-612" title="The structure of switch statements" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/The-structure-of-switch-statements.jpg" alt="The structure of switch statements" width="581" height="225" /></p>
<p><br class="blank" /><br />
The <strong>switch</strong> statement</p>
<ul>
<li>Is different from similar statements in languages such as Pascal in a very important way</li>
</ul>
<p><br class="blank" /><br />
Each C++ case label functions</p>
<ul>
<li>Only as a line label, not as a boundary between choices
<ul>
<li>That is, after a program jumps to a particular line in a <strong>switch</strong>
<ul>
<li>It then sequentially executes all the statements following that line in the switch unless you explicitly direct it otherwise</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Execution does <em>not</em> automatically stop at the next case</p>
<ul>
<li>To make execution stop at the end of a particular group of statements
<ul>
<li>You must use the <strong>break</strong> statement
<ul>
<li>This causes execution to jump to the statement following the <strong>switch</strong></li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
(A-2) shows how to use <strong>switch</strong> and <strong>break</strong> together to implement a simple menu for executives</p>
<ul>
<li>The program uses a <strong>showmenu()</strong> function to display a set of choices
<ul>
<li>A <strong>switch</strong> statement then selects an action based on the user’s response</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Compatibility Note</p>
<ul>
<li>Some C++ implementations treat the <strong>\a</strong> escape sequence (used in <strong>case 1</strong> in (A-2)) as silent</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// switch.cpp -- using the switch statement</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// function prototypes</span>
<span style="color: #0000ff;">void</span> report<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> comfort<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> choice<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> choice<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>choice <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>choice<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\a</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">2</span> <span style="color: #008080;">:</span> report<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">3</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The boss was in all day.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">4</span> <span style="color: #008080;">:</span> comfort<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">default</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;That's not a choice.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> choice<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bye!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Please enter 1, 2, 3, 4, or 5:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;1) alarm 2) report<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;3) alibi 4) comfort<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;5) quit<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> report<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;It's been an excellent week for business.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;Sales are up 150%. Expenses are down 35%.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> comfort<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Your employees think you are the finest CEO<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;in the industry. The board of directors think<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;you are the finest CEO in the industry.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here is a sample run of the executive menu program in (A-2):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-611" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output.jpg" alt="Output" width="597" height="382" /></p>
<p><br class="blank" /><br />
The <strong>while</strong> loop terminates</p>
<ul>
<li>When the user enters <strong>5</strong></li>
</ul>
<p><br class="blank" /><br />
Entering <strong>1</strong> through <strong>4</strong></p>
<ul>
<li>Activates the corresponding choice from the <strong>switch</strong> list</li>
</ul>
<p><br class="blank" /><br />
Entering <strong>10</strong></p>
<ul>
<li>Triggers the default statements</li>
</ul>
<p><br class="blank" /><br />
As noted earlier, this program needs the <strong>break</strong> statements</p>
<ul>
<li>To confine execution to a particular portion of a <strong>switch</strong> statement
<ul>
<li>To see that this is so, you can remove the <strong>break</strong> statements from (A-2) and see how it works afterward
<ul>
<li>You’ll find, for example, that entering <strong>2</strong> causes the program to execute <em>all</em> the statements associated with case labels 2, 3, 4, and the default
<ul>
<li>C++ works this way because that sort of behavior can be useful</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
For one thing, it makes it simple to use multiple labels</p>
<ul>
<li>For example, suppose you rewrote (A-2) using characters instead of integers as menu choices and switch labels
<ul>
<li>In that case, you could use both an uppercase and a lowercase label for the same statements (A-3):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// switch.cpp -- using the switch statement</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// function prototypes</span>
<span style="color: #0000ff;">void</span> report<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> comfort<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> choice<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> choice<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>choice <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'Q'</span> <span style="color: #000040;">&amp;&amp;</span> choice <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'q'</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>choice<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'a'</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'A'</span><span style="color: #008080;">:</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\a</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'r'</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'R'</span><span style="color: #008080;">:</span> report<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'l'</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'L'</span><span style="color: #008080;">:</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The boss was in all day.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'c'</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">case</span> <span style="color: #FF0000;">'C'</span><span style="color: #008080;">:</span> comfort<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">default</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;That's not a choice.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> choice<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bye!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> showmenu<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Please enter a, r, l, c, or q:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;a) alarm r) report<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;l) alibi c) comfort<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;q) quit<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> report<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;It's been an excellent week for business.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;Sales are up 150%. Expenses are down 35%.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> comfort<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Your employees think you are the finest CEO<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;in the industry. The board of directors think<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #FF0000;">&quot;you are the finest CEO in the industry.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
(A-3) because there is no <strong>break</strong> immediately following <strong>case ‘a’</strong>, program execution passes on to the next line</p>
<ul>
<li>Which is the statement following <strong>case ‘A’</strong></li>
</ul>
<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/the-switch-statement/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

