<?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 ++ &#187; Dot Operator</title>
	<atom:link href="http://www.bizzymicbizness.com/cplusplus/tag/dot-operator/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>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>
	</channel>
</rss>

