<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7009649579956966078</id><updated>2024-09-01T22:38:47.031-07:00</updated><title type='text'>C++ Tutorial</title><subtitle type='html'>First of all I would like to thank&#39;s you for selecting my blog to learn the C++ programing language. This tutorial is for those people who want to learn programming in C++ and do not necessarily have any previous &#xa;knowledge of other programming languages. They can learn it here. So read my blog and enjoy a lot.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default?start-index=26&amp;max-results=25'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>27</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-104541049072569539</id><published>2011-05-29T08:06:00.000-07:00</published><updated>2011-06-03T00:47:21.146-07:00</updated><title type='text'>Download Turbo C++ 3.0</title><content type='html'>&lt;script type=&quot;text/javascript&quot;&gt;
ch_client = &quot;amitwebhero&quot;;
ch_width = 550;
ch_height = 250;
ch_type = &quot;mpu&quot;;
ch_sid = &quot;Chitika Default&quot;;
ch_backfill = 1;
ch_color_site_link = &quot;0000cc&quot;;
ch_color_title = &quot;0000cc&quot;;
ch_color_border = &quot;ffffff&quot;;
ch_color_text = &quot;000000&quot;;
ch_color_bg = &quot;ffffff&quot;;
&lt;/script&gt;&lt;br /&gt;
&lt;script src=&quot;http://scripts.chitika.net/eminimalls/amm.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #4d4d4d; line-height: 15px; text-align: justify;&quot;&gt;Download Turbo C++ programmer Software Here This place provides you all information&#39;s about the Turbo C++ programmer software. This software is very much Improtant&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #4d4d4d; line-height: 15px; text-align: justify;&quot;&gt;If you want to Download Turbo C++ programmer Software&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #4d4d4d; line-height: 15px; text-align: justify;&quot;&gt; You can visit here -&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #4d4d4d; line-height: 15px; text-align: justify;&quot;&gt;&lt;a href=&quot;http://www.sammisoft.com/soft-128137.download&quot;&gt;Click To Download&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: small;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: small;&quot;&gt;&lt;b&gt;Some Information to the Software Turbo C++&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: small;&quot;&gt;&lt;b&gt;&lt;br /&gt;
File size:&lt;/b&gt; 3.30 MB&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Platform:&lt;/b&gt; Win95, Win98, WinME, Windows2000, Windows2003, WinXP, Windows Vista, Windows Vista x64, Windows 7, Windows 7 x64&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
Price:&lt;/b&gt; FREE&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Released:&lt;/b&gt; 22-10-2008&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #4d4d4d; line-height: 15px; text-align: justify;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/104541049072569539/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/05/download-turbo-c-30.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/104541049072569539'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/104541049072569539'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/05/download-turbo-c-30.html' title='Download Turbo C++ 3.0'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-8666420332718620306</id><published>2011-03-30T08:06:00.000-07:00</published><updated>2011-05-30T00:56:03.655-07:00</updated><title type='text'>Introduction To C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The  C++ is the best programming language in the Computer science subject.  It is thought that the first programing language appeared during the  40&#39;s. A lot has happened since then, and if in the beginning programing  was nearly exclusively reserved to a scientific or military environment,  nowadays its use is a lot more extended. It is one of the best  development environments for C++ in the 90&#39;s.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;During  the 90&#39;s a programing language created quite a few years before in the  Bell Laboratories became popular in universities and companies, we are  talking about the C programing language, that not long after was  substituted for C++ with which it was possible to manipulate objects.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;C++ is used to make programmes in the computer&#39;s through a software named &lt;a href=&quot;http://onli9.blogspot.com/2011/05/download-turbo-c-30.html&quot;&gt;TURBO C++&lt;/a&gt;. This software is very much Important to make a C++ programme. A C++ programme is consist of the codes that we have to write in the programmer page and then you have to give commands in the appeared menu after installing the &lt;a href=&quot;http://onli9.blogspot.com/2011/05/download-turbo-c-30.html&quot;&gt;TURBO C++.&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In this blog you will several commands that will help you to proceed your self to your next step in the C++ programming language. The best way to proceed your self for the C++ programming language is to start writing commands in your C++ programmer.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;All C++ compilers support the compilation of console programs. Check the user&#39;s manual of your compiler for more info on how to compile them.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Below is the C++ contents that will help you directly to get the Informations.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&lt;a href=&quot;http://onli9.blogspot.com/2011/05/download-turbo-c-30.html?utm_source=BP_recent&quot;&gt;Download Turbo C++&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;post-count&quot; dir=&quot;ltr&quot;&gt;&lt;/span&gt;&lt;ul class=&quot;hierarchy&quot;&gt;&lt;li class=&quot;archivedate expanded&quot;&gt;&lt;ul class=&quot;posts&quot; style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/04/instructions-for-use.html&quot;&gt;Instructions for use&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/04/constants.html&quot;&gt;Constants&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/basic-inputoutput.html&quot;&gt;Basic Input/Output&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/control-structures.html&quot;&gt;Control Structures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/functions-i.html&quot;&gt;Functions (I)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/functions-ii.html&quot;&gt;Functions (II)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/arrays.html&quot;&gt;Arrays&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/character-sequences.html&quot;&gt;Character Sequences&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/pointers.html&quot;&gt;Pointers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/05/data-structures.html&quot;&gt;Data structures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/other-data-types.html&quot;&gt;Other Data Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/05/dynamic-memory.html&quot;&gt;Dynamic Memory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/classes-i.html&quot;&gt;Classes (I)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/classes-ii.html&quot;&gt;Classes (II)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/05/friendship-and-inheritance.html&quot;&gt;Friendship and inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/polymorphism.html&quot;&gt;Polymorphism&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/operators_20.html&quot;&gt;Operators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/templates.html&quot;&gt;Templates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/05/structure-of-program.html&quot;&gt;Structure of a program&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/05/variables-data-types_25.html&quot;&gt;Variables. Data Types.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/exceptions.html&quot;&gt;Exceptions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/type-casting.html&quot;&gt;Type Casting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/preprocessor-directives.html&quot;&gt;Preprocessor directives&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/inputoutput-with-files.html&quot;&gt;Input/Output with files&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://onli9.blogspot.com/2011/03/namespaces.html&quot;&gt;Namespaces&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Now Select your interests from here and proceed your self. &lt;/span&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/8666420332718620306/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/05/introduction-to-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/8666420332718620306'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/8666420332718620306'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/05/introduction-to-c.html' title='Introduction To C++'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-5551798569434150918</id><published>2011-03-29T00:36:00.000-07:00</published><updated>2011-05-29T07:38:40.456-07:00</updated><title type='text'>Namespaces</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;a href=&quot;http://signup.clicksor.com/pub/index.php?ref=183086&quot; target=&quot;_blank&quot;&gt;&lt;br /&gt;
&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;a href=&quot;http://signup.clicksor.com/pub/index.php?ref=183086&quot; target=&quot;_blank&quot;&gt; &lt;img border=&quot;0&quot; src=&quot;http://signup.clicksor.com/publisher/images/pub/120x240_2.gif&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;a href=&quot;http://signup.clicksor.com/pub/index.php?ref=183086&quot; target=&quot;_blank&quot;&gt;&lt;br /&gt;
&lt;img border=&quot;0&quot; src=&quot;http://signup.clicksor.com/publisher/images/pub/110x32_2.gif&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;script type=&quot;text/javascript&quot;&gt;
//interstitial ad
clicksor_enable_inter = true; clicksor_maxad = 50;  
clicksor_hourcap = 2; clicksor_showcap = 2;
//Alert MsgAd
clicksor_enable_MsgAlert = true;
//connect widget
clicksor_adhere_opt = &#39;left:50%&#39;;
//default pop-under house ad url
clicksor_enable_pop = true; clicksor_frequencyCap = 0;
durl = &#39;&#39;;//default banner house ad url 
clicksor_default_url = &#39;&#39;;
clicksor_banner_border = &#39;#99CC33&#39;; clicksor_banner_ad_bg = &#39;#FFFFFF&#39;;
clicksor_banner_link_color = &#39;#000000&#39;; clicksor_banner_text_color = &#39;#666666&#39;;
clicksor_banner_image_banner = true; clicksor_banner_text_banner = true;
clicksor_layer_border_color = &#39;&#39;;
clicksor_layer_ad_bg = &#39;&#39;; clicksor_layer_ad_link_color = &#39;&#39;;
clicksor_layer_ad_text_color = &#39;&#39;; clicksor_text_link_bg = &#39;&#39;;
clicksor_text_link_color = &#39;&#39;; clicksor_enable_text_link = true;
&lt;/script&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;script src=&quot;http://ads.clicksor.com/showAd.php?nid=1&amp;amp;pid=183086&amp;amp;adtype=2&amp;amp;sid=286747&amp;amp;float=1&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;noscript&gt;&amp;amp;amp;amp;amp;amp;lt;a href=&quot;http://www.yesadvertising.com&quot;&amp;amp;amp;amp;amp;amp;gt;affiliate marketing&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt;&lt;/noscript&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can &lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;font-family: Verdana,sans-serif; text-align: left;&quot; trbidi=&quot;on&quot;&gt;be divided in &quot;sub-scopes&quot;, each one with its own name.&amp;nbsp; &lt;br /&gt;
The format of namespaces is:&amp;nbsp; &lt;br /&gt;
namespace identifier &lt;br /&gt;
{ &lt;br /&gt;
entities &lt;br /&gt;
} &lt;br /&gt;
Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included &lt;br /&gt;
within the namespace. For example:&amp;nbsp; &lt;br /&gt;
namespace myNamespace &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int a, b; &lt;br /&gt;
} &lt;br /&gt;
In this case, the variables a and b are normal variables declared within a namespace called myNamespace. In order &lt;br /&gt;
to access these variables from outside the myNamespace namespace we have to use the scope operator ::. For &lt;br /&gt;
example, to access the previous variables from outside myNamespace we can write:&amp;nbsp; &lt;br /&gt;
myNamespace::a &lt;br /&gt;
myNamespace::b &lt;br /&gt;
The functionality of namespaces is especially useful in the case that there is a possibility that a global object or &lt;br /&gt;
function uses the same identifier as another one, causing redefinition errors. For example:&amp;nbsp; &lt;br /&gt;
// namespaces &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
namespace first &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int var = 5; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
namespace second &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; double var = 3.1416; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; first::var &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; second::var &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
5 &lt;br /&gt;
3.1416 &lt;br /&gt;
In this case, there are two global variables with the same name: var. One is defined within the namespace first &lt;br /&gt;
and the other one in second. No redefinition errors happen thanks to namespaces.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;using &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
The keyword using is used to introduce a name from a namespace into the current declarative region. For &lt;br /&gt;
example:&amp;nbsp; &lt;br /&gt;
// using &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
namespace first &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x = 5; &lt;br /&gt;
&amp;nbsp; int y = 10; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
namespace second &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; double x = 3.1416; &lt;br /&gt;
&amp;nbsp; double y = 2.7183; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; using first::x; &lt;br /&gt;
&amp;nbsp; using second::y; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; y &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; first::y &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; second::x &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
5 &lt;br /&gt;
2.7183 &lt;br /&gt;
10 &lt;br /&gt;
3.1416 &lt;br /&gt;
Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly &lt;br /&gt;
as our using declarations have specified. We still have access to first::y and second::x using their fully qualified &lt;br /&gt;
names.&amp;nbsp; &lt;br /&gt;
The keyword using can also be used as a directive to introduce an entire namespace:&amp;nbsp; &lt;br /&gt;
// using &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
namespace first &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x = 5; &lt;br /&gt;
&amp;nbsp; int y = 10; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
namespace second &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; double x = 3.1416; &lt;br /&gt;
&amp;nbsp; double y = 2.7183; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; using namespace first; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; y &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; second::x &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; second::y &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
5 &lt;br /&gt;
10 &lt;br /&gt;
3.1416 &lt;br /&gt;
2.7183 &lt;br /&gt;
In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
using and using namespace have validity only in the same block in which they are stated or in the entire code if &lt;br /&gt;
they are used directly in the global scope. For example, if we had the intention to first use the objects of one &lt;br /&gt;
namespace and then those of another one, we could do something like:&amp;nbsp; &lt;br /&gt;
// using namespace example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
namespace first &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x = 5; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
namespace second &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; double x = 3.1416; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; using namespace first; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; using namespace second; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
5 &lt;br /&gt;
3.1416&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Namespace alias &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
We can declare alternate names for existing namespaces according to the following format:&amp;nbsp; &lt;br /&gt;
namespace new_name = current_name;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Namespace std &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream. &lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/5551798569434150918/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/namespaces.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/5551798569434150918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/5551798569434150918'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/namespaces.html' title='Namespaces'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-4238727136034807779</id><published>2011-03-28T00:49:00.000-07:00</published><updated>2011-05-29T01:06:23.841-07:00</updated><title type='text'>Input/Output with files</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;C++ Standard Library &lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;C++ provides the following classes to perform output and input of characters to/from files:&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ofstream: Stream class to write on files&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ifstream: Stream class to read from files&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fstream: Stream class to both read and write from/to files.&amp;nbsp; &lt;br /&gt;
These classes are derived directly or indirectly from the classes istream, and ostream. We have already used &lt;br /&gt;
objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream. &lt;br /&gt;
Therfore, we have already been using classes that are related to our file streams. And in fact, we can use our file &lt;br /&gt;
streams the same way we are already used to use cin and cout, with the only difference that we have to associate &lt;br /&gt;
these streams with physical files. Let&#39;s see an example:&amp;nbsp; &lt;br /&gt;
// basic file operations &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;fstream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; ofstream myfile; &lt;br /&gt;
&amp;nbsp; myfile.open (&quot;example.txt&quot;); &lt;br /&gt;
&amp;nbsp; myfile &amp;lt;&amp;lt; &quot;Writing this to a file.\n&quot;; &lt;br /&gt;
&amp;nbsp; myfile.close(); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
[file example.txt] &lt;br /&gt;
Writing this to a file &lt;br /&gt;
This code creates a file called example.txt and inserts a sentence into it in the same way we are used to do with &lt;br /&gt;
cout, but using the file stream myfile instead.&amp;nbsp; &lt;br /&gt;
But let&#39;s go step by step:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Open a file &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;The first operation generally performed on an object of one of these classes is to associate it to a real file. This &lt;br /&gt;
procedure is known as to open a file. An open file is represented within a program by a stream object (an &lt;br /&gt;
instantiation of one of these classes, in the previous example this was myfile) and any input or output operation &lt;br /&gt;
performed on this stream object will be applied to the physical file associated to it.&amp;nbsp; &lt;br /&gt;
In order to open a file with a stream object we use its member function open():&amp;nbsp; &lt;br /&gt;
open (filename, mode); &lt;br /&gt;
Where filename is a null-terminated character sequence of type const char * (the same type that string literals &lt;br /&gt;
have) representing the name of the file to be opened, and mode is an optional parameter with a combination of the following flags:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil5JC79ir9PHGQtxE1K_PporUgEP__LQWEtHFQe_9451YjxL58iuiJSRCXvtuEvkiFZ01oVUw31vDNofBC25qN81jDrM-iRFqF3gPTAvgsJrqUwBrKXaY38qz9Bt3OJz00OKjfF0rr3fX2/s1600/amitupdjfi.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file &lt;br /&gt;
example.bin in binary mode to add data we could do it by the following call to member function open():&amp;nbsp; &lt;br /&gt;
ofstream myfile; &lt;br /&gt;
myfile.open (&quot;example.bin&quot;, ios::out | ios::app | ios::binary); &lt;br /&gt;
Each one of the open() member functions of the classes ofstream, ifstream and fstream has a default mode that is used if the file is opened without a second argument:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjW_n9MiJnkEz8t8ThDdj6FsQvraa8pQJO0Rwzaiz2CSuMXxyztWFRRAwnoROT9q5ZhWY81GSKMdjxjDlgcYGN8H9ARNrUVWaqcI-CNZVz_TnIH_aGcOw0gNG4ymKNtUHKCGZIglIVql9WL/s1600/amitjjj.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively assumed, even if a &lt;br /&gt;
mode that does not include them is passed as second argument to the open() member function.&amp;nbsp; &lt;br /&gt;
The default value is only applied if the function is called without specifying any value for the mode parameter. If &lt;br /&gt;
the function is called with any value in that parameter the default mode is overridden, not combined.&amp;nbsp; &lt;br /&gt;
File streams opened in binary mode perform input and output operations independently of any format &lt;br /&gt;
considerations. Non-binary files are known as text files, and some translations may occur due to formatting of &lt;br /&gt;
some special characters (like newline and carriage return characters).&amp;nbsp; &lt;br /&gt;
Since the first task that is performed on a file stream object is generally to open a file, these three classes include &lt;br /&gt;
a constructor that automatically calls the open() member function and has the exact same parameters as this &lt;br /&gt;
member. Therefore, we could also have declared the previous myfile object and conducted the same opening &lt;br /&gt;
operation in our previous example by writing:&amp;nbsp; &lt;br /&gt;
ofstream myfile (&quot;example.bin&quot;, ios::out | ios::app | ios::binary); &lt;br /&gt;
Combining object construction and stream opening in a single statement. Both forms to open a file are valid and &lt;br /&gt;
equivalent.&amp;nbsp; &lt;br /&gt;
To check if a file stream was successful opening a file, you can do it by calling to member is_open() with no &lt;br /&gt;
arguments. This member function returns a bool value of true in the case that indeed the stream object is &lt;br /&gt;
associated with an open file, or false otherwise:&amp;nbsp; &lt;br /&gt;
if (myfile.is_open()) { /* ok, proceed with output */ } &lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Closing a file &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;When we are finished with our input and output operations on a file we shall close it so that its resources become &lt;br /&gt;
available again. In order to do that we have to call the stream&#39;s member function close(). This member function &lt;br /&gt;
takes no parameters, and what it does is to flush the associated buffers and close the file:&amp;nbsp; &lt;br /&gt;
myfile.close(); &lt;br /&gt;
Once this member function is called, the stream object can be used to open another file, and the file is available &lt;br /&gt;
again to be opened by other processes.&amp;nbsp; &lt;br /&gt;
In case that an object is destructed while still associated with an open file, the destructor automatically calls the member function close().&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Text files&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Text file streams are those where we do not include the ios::binary flag in their opening mode. These files are &lt;br /&gt;
designed to store text and thus all values that we input or output from/to them can suffer some formatting &lt;br /&gt;
transformations, which do not necessarily correspond to their literal binary value.&amp;nbsp; &lt;br /&gt;
Data output operations on text files are performed in the same way we operated with cout:&amp;nbsp; &lt;br /&gt;
// writing on a text file &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;fstream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; ofstream myfile (&quot;example.txt&quot;); &lt;br /&gt;
&amp;nbsp; if (myfile.is_open()) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; myfile &amp;lt;&amp;lt; &quot;This is a line.\n&quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; myfile &amp;lt;&amp;lt; &quot;This is another line.\n&quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; myfile.close(); &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; else cout &amp;lt;&amp;lt; &quot;Unable to open file&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
[file example.txt] &lt;br /&gt;
This is a line. &lt;br /&gt;
This is another line. &lt;br /&gt;
Data input from a file can also be performed in the same way that we did with cin:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;// reading a text file &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;fstream&amp;gt; &lt;br /&gt;
#include &amp;lt;string&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; string line; &lt;br /&gt;
&amp;nbsp; ifstream myfile (&quot;example.txt&quot;); &lt;br /&gt;
&amp;nbsp; if (myfile.is_open()) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; while (! myfile.eof() ) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; getline (myfile,line); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; line &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; myfile.close(); &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; else cout &amp;lt;&amp;lt; &quot;Unable to open file&quot;;&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
This is a line. &lt;br /&gt;
This is another line.&amp;nbsp;&amp;nbsp; &lt;br /&gt;
This last example reads a text file and prints out its content on the screen. Notice how we have used a new &lt;br /&gt;
member function, called eof() that returns true in the case that the end of the file has been reached. We have &lt;br /&gt;
created a while loop that finishes when indeed myfile.eof() becomes true (i.e., the end of the file has been &lt;br /&gt;
reached).&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Checking state flags &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;In addition to eof(), which checks if the end of file has been reached, other member functions exist to check the &lt;br /&gt;
state of a stream (all of them return a bool value):&amp;nbsp; &lt;br /&gt;
bad()&amp;nbsp; &lt;br /&gt;
Returns true if a reading or writing operation fails. For example in the case that we try to write to a file &lt;br /&gt;
that is not open for writing or if the device where we try to write has no space left.&amp;nbsp; &lt;br /&gt;
fail()&amp;nbsp; &lt;br /&gt;
Returns true in the same cases as bad(), but also in the case that a format error happens, like when an &lt;br /&gt;
alphabetical character is extracted when we are trying to read an integer number.&amp;nbsp; &lt;br /&gt;
eof()&amp;nbsp; &lt;br /&gt;
Returns true if a file open for reading has reached the end.&amp;nbsp; &lt;br /&gt;
good()&amp;nbsp; &lt;br /&gt;
It is the most generic state flag: it returns false in the same cases in which calling any of the previous &lt;br /&gt;
functions would return true.&amp;nbsp; &lt;br /&gt;
In order to reset the state flags checked by any of these member functions we have just seen we can use the &lt;br /&gt;
member function clear(), which takes no parameters.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;get and put stream pointers &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;All i/o streams objects have, at least, one internal stream pointer:&amp;nbsp; &lt;br /&gt;
ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next &lt;br /&gt;
input operation.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element &lt;br /&gt;
has to be written.&amp;nbsp; &lt;br /&gt;
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both &lt;br /&gt;
istream and ostream).&amp;nbsp; &lt;br /&gt;
These internal stream pointers that point to the reading or writing locations within a stream can be manipulated &lt;br /&gt;
using the following member functions:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;tellg() and tellp() &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;These two member functions have no parameters and return a value of the member type pos_type, which is an integer data type representing the current position of the get stream pointer (in the case of tellg) or the put &lt;br /&gt;
stream pointer (in the case of tellp).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;seekg() and seekp() &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;These functions allow us to change the position of the get and put stream pointers. Both functions are overloaded &lt;br /&gt;
with two different prototypes. The first prototype is:&amp;nbsp; &lt;br /&gt;
seekg ( position ); &lt;br /&gt;
seekp ( position ); &lt;br /&gt;
Using this prototype the stream pointer is changed to the absolute position position (counting from the beginning &lt;br /&gt;
of the file). The type for this parameter is the same as the one returned by functions tellg and tellp: the &lt;br /&gt;
member type pos_type, which is an integer value.&amp;nbsp; &lt;br /&gt;
The other prototype for these functions is:&amp;nbsp; &lt;br /&gt;
seekg ( offset, direction ); &lt;br /&gt;
seekp ( offset, direction ); &lt;br /&gt;
Using this prototype, the position of the get or put pointer is set to an offset value relative to some specific point &lt;br /&gt;
determined by the parameter direction. offset is of the member type off_type, which is also an integer type. &lt;br /&gt;
And direction is of type seekdir, which is an enumerated type (enum) that determines the point from where &lt;br /&gt;
offset is counted from, and that can take any of the following values:&amp;nbsp; &lt;br /&gt;
ios::beg offset counted from the beginning of the stream &lt;br /&gt;
ios::cur offset counted from the current position of the stream pointer &lt;br /&gt;
ios::end offset counted from the end of the stream &lt;br /&gt;
The following example uses the member functions we have just seen to obtain the size of a file:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;// obtaining file size &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;fstream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; long begin,end; &lt;br /&gt;
&amp;nbsp; ifstream myfile (&quot;example.txt&quot;); &lt;br /&gt;
&amp;nbsp; begin = myfile.tellg(); &lt;br /&gt;
&amp;nbsp; myfile.seekg (0, ios::end); &lt;br /&gt;
&amp;nbsp; end = myfile.tellg(); &lt;br /&gt;
&amp;nbsp; myfile.close(); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;size is: &quot; &amp;lt;&amp;lt; (end-begin) &amp;lt;&amp;lt; &quot; bytes.\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
size is: 40 bytes.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Binary files &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;In binary files, to input and output data with the extraction and insertion operators (&amp;lt;&amp;lt; and &amp;gt;&amp;gt;) and functions like &lt;br /&gt;
getline is not efficient, since we do not need to format any data, and data may not use the separation codes used &lt;br /&gt;
by text files to separate elements (like space, newline, etc...).&amp;nbsp; &lt;br /&gt;
File streams include two member functions specifically designed to input and output binary data sequentially: &lt;br /&gt;
write and read. The first one (write) is a member function of ostream inherited by ofstream. And read is a &lt;br /&gt;
member function of istream that is inherited by ifstream. Objects of class fstream have both members. Their &lt;br /&gt;
prototypes are:&amp;nbsp; &lt;br /&gt;
write ( memory_block, size ); &lt;br /&gt;
read ( memory_block, size ); &lt;br /&gt;
Where memory_block is of type &quot;pointer to char&quot; (char*), and represents the address of an array of bytes where &lt;br /&gt;
the read data elements are stored or from where the data elements to be written are taken. The size parameter is &lt;br /&gt;
an integer value that specifies the number of characters to be read or written from/to the memory block.&amp;nbsp; &lt;br /&gt;
// reading a complete binary file &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;fstream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
ifstream::pos_type size; &lt;br /&gt;
char * memblock; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; ifstream file (&quot;example.bin&quot;, &lt;br /&gt;
ios::in|ios::binary|ios::ate); &lt;br /&gt;
&amp;nbsp; if (file.is_open()) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; size = file.tellg(); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; memblock = new char [size]; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; file.seekg (0, ios::beg); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; file.read (memblock, size); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; file.close(); &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;the complete file content is in memory&quot;; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; delete[] memblock; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; else cout &amp;lt;&amp;lt; &quot;Unable to open file&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
the complete file content is in memory &lt;br /&gt;
In this example the entire file is read and stored in a memory block. Let&#39;s examine how this is done:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;First, the file is open with the ios::ate flag, which means that the get pointer will be positioned at the end of the &lt;br /&gt;
file. This way, when we call to member tellg(), we will directly obtain the size of the file. Notice the type we have &lt;br /&gt;
used to declare variable size:&amp;nbsp; &lt;br /&gt;
ifstream::pos_type size; &lt;br /&gt;
ifstream::pos_type is a specific type used for buffer and file positioning and is the type returned by &lt;br /&gt;
file.tellg(). This type is defined as an integer type, therefore we can conduct on it the same operations we &lt;br /&gt;
conduct on any other integer value, and can safely be converted to another integer type large enough to contain &lt;br /&gt;
the size of the file. For a file with a size under 2GB we could use int:&amp;nbsp; &lt;br /&gt;
int size; &lt;br /&gt;
size = (int) file.tellg(); &lt;br /&gt;
Once we have obtained the size of the file, we request the allocation of a memory block large enough to hold the &lt;br /&gt;
entire file:&amp;nbsp; &lt;br /&gt;
memblock = new char[size]; &lt;br /&gt;
Right after that, we proceed to set the get pointer at the beginning of the file (remember that we opened the file &lt;br /&gt;
with this pointer at the end), then read the entire file, and finally close it:&amp;nbsp; &lt;br /&gt;
file.seekg (0, ios::beg); &lt;br /&gt;
file.read (memblock, size); &lt;br /&gt;
file.close(); &lt;br /&gt;
At this point we could operate with the data obtained from the file. Our program simply announces that the content of the file is in memory and then terminates.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Buffers and Synchronization &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;When we operate with file streams, these are associated to an internal buffer of type streambuf. This buffer is a &lt;br /&gt;
memory block that acts as an intermediary between the stream and the physical file. For example, with an &lt;br /&gt;
ofstream, each time the member function put (which writes a single character) is called, the character is not &lt;br /&gt;
written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in &lt;br /&gt;
that stream&#39;s intermediate buffer.&amp;nbsp; &lt;br /&gt;
When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream) &lt;br /&gt;
or simply freed (if it is an input stream). This process is called synchronization and takes place under any of the &lt;br /&gt;
following circumstances:&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; When the file is closed: before closing a file all buffers that have not yet been flushed are synchronized &lt;br /&gt;
and all pending data is written or read to the physical medium.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically &lt;br /&gt;
synchronized.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Explicitly, with manipulators: When certain manipulators are used on streams, an explicit &lt;br /&gt;
synchronization takes place. These manipulators are: flush and endl.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Explicitly, with member function sync(): Calling stream&#39;s member function sync(), which takes no &lt;br /&gt;
parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the &lt;br /&gt;
stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully &lt;br /&gt;
synchronized) it returns 0.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/4238727136034807779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/inputoutput-with-files.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4238727136034807779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4238727136034807779'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/inputoutput-with-files.html' title='Input/Output with files'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil5JC79ir9PHGQtxE1K_PporUgEP__LQWEtHFQe_9451YjxL58iuiJSRCXvtuEvkiFZ01oVUw31vDNofBC25qN81jDrM-iRFqF3gPTAvgsJrqUwBrKXaY38qz9Bt3OJz00OKjfF0rr3fX2/s72-c/amitupdjfi.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-8148039828702659315</id><published>2011-03-27T00:47:00.000-07:00</published><updated>2011-05-29T00:57:33.800-07:00</updated><title type='text'>Preprocessor directives</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Preprocessor directives are lines included in the code of our programs that are not program statements but &lt;br /&gt;
directives for the preprocessor. These lines are always preceded by a hash sign (#). The preprocessor is executed &lt;br /&gt;
before the actual compilation of code begins, therefore the preprocessor digests all these directives before any &lt;br /&gt;
code is generated by the statements.&amp;nbsp; &lt;br /&gt;
These preprocessor directives extend only across a single line of code. As soon as a newline character is found, the preprocessor directive is considered to end. No semicolon (;) is expected at the end of a preprocessor directive. The only way a preprocessor directive can extend through more than one line is by preceding the newline character at the end of the line by a backslash (\).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;macro definitions (#define, #undef) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;To define preprocessor macros we can use #define. Its format is:&amp;nbsp; &lt;br /&gt;
#define identifier replacement &lt;br /&gt;
When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code &lt;br /&gt;
by replacement. This replacement can be an expression, a statement, a block or simply anything. The &lt;br /&gt;
preprocessor does not understand C++, it simply replaces any occurrence of identifier by replacement.&amp;nbsp; &lt;br /&gt;
#define TABLE_SIZE 100 &lt;br /&gt;
int table1[TABLE_SIZE]; &lt;br /&gt;
int table2[TABLE_SIZE]; &lt;br /&gt;
After the preprocessor has replaced TABLE_SIZE, the code becomes equivalent to:&amp;nbsp; &lt;br /&gt;
int table1[100]; &lt;br /&gt;
int table2[100]; &lt;br /&gt;
This use of #define as constant definer is already known by us from previous tutorials, but #define can work also &lt;br /&gt;
with parameters to define function macros:&amp;nbsp; &lt;br /&gt;
#define getmax(a,b) a&amp;gt;b?a:b &lt;br /&gt;
This would replace any occurrence of getmax followed by two arguments by the replacement expression, but also &lt;br /&gt;
replacing each argument by its identifier, exactly as you would expect if it was a function:&amp;nbsp; &lt;br /&gt;
// function macro &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
#define getmax(a,b) ((a)&amp;gt;(b)?(a):(b)) &lt;br /&gt;
&lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x=5, y; &lt;br /&gt;
&amp;nbsp; y= getmax(x,2); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; y &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; getmax(7,x) &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
5 &lt;br /&gt;
7 &lt;br /&gt;
Defined macros are not affected by block structure. A macro lasts until it is undefined with the #undef preprocessor directive:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#define TABLE_SIZE 100 &lt;br /&gt;
int table1[TABLE_SIZE]; &lt;br /&gt;
#undef TABLE_SIZE &lt;br /&gt;
#define TABLE_SIZE 200 &lt;br /&gt;
int table2[TABLE_SIZE]; &lt;br /&gt;
This would generate the same code as:&amp;nbsp; &lt;br /&gt;
int table1[100]; &lt;br /&gt;
int table2[200]; &lt;br /&gt;
Function macro definitions accept two special operators (# and ##) in the replacement sequence: &lt;br /&gt;
If the operator # is used before a parameter is used in the replacement sequence, that parameter is replaced by a &lt;br /&gt;
string literal (as if it were enclosed between double quotes)&amp;nbsp; &lt;br /&gt;
#define str(x) #x &lt;br /&gt;
cout &amp;lt;&amp;lt; str(test); &lt;br /&gt;
This would be translated into:&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;test&quot;; &lt;br /&gt;
The operator ## concatenates two arguments leaving no blank spaces between them:&amp;nbsp; &lt;br /&gt;
#define glue(a,b) a ## b &lt;br /&gt;
glue(c,out) &amp;lt;&amp;lt; &quot;test&quot;; &lt;br /&gt;
This would also be translated into:&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;test&quot;; &lt;br /&gt;
Because preprocessor replacements happen before any C++ syntax check, macro definitions can be a tricky &lt;br /&gt;
feature, but be careful: code that relies heavily on complicated macros may result obscure to other programmers, since the syntax they expect is on many occasions different from the regular expressions programmers expect in C++.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Conditional inclusions (#ifdef, #ifndef, #if, #endif, #else and &lt;br /&gt;
#elif) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;These directives allow to include or discard part of the code of a program if a certain condition is met.&amp;nbsp; &lt;br /&gt;
#ifdef allows a section of a program to be compiled only if the macro that is specified as the parameter has been &lt;br /&gt;
defined, no matter which its value is. For example:&amp;nbsp; &lt;br /&gt;
#ifdef TABLE_SIZE &lt;br /&gt;
int table[TABLE_SIZE]; &lt;br /&gt;
#endif&amp;nbsp;&amp;nbsp; &lt;br /&gt;
In this case, the line of code int table[TABLE_SIZE]; is only compiled if TABLE_SIZE was previously defined with &lt;br /&gt;
#define, independently of its value. If it was not defined, that line will not be included in the program compilation.&amp;nbsp; #ifndef serves for the exact opposite: the code between #ifndef and #endif directives is only compiled if the specified identifier has not been previously defined. For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#ifndef TABLE_SIZE &lt;br /&gt;
#define TABLE_SIZE 100 &lt;br /&gt;
#endif &lt;br /&gt;
int table[TABLE_SIZE]; &lt;br /&gt;
In this case, if when arriving at this piece of code, the TABLE_SIZE macro has not been defined yet, it would be &lt;br /&gt;
defined to a value of 100. If it already existed it would keep its previous value since the #define directive would &lt;br /&gt;
not be executed.&amp;nbsp; &lt;br /&gt;
The #if, #else and #elif (i.e., &quot;else if&quot;) directives serve to specify some condition to be met in order for the &lt;br /&gt;
portion of code they surround to be compiled. The condition that follows #if or #elif can only evaluate constant &lt;br /&gt;
expressions, including macro expressions. For example:&amp;nbsp; &lt;br /&gt;
#if TABLE_SIZE&amp;gt;200 &lt;br /&gt;
#undef TABLE_SIZE &lt;br /&gt;
#define TABLE_SIZE 200 &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
#elif TABLE_SIZE&amp;lt;50 &lt;br /&gt;
#undef TABLE_SIZE &lt;br /&gt;
#define TABLE_SIZE 50 &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
#else &lt;br /&gt;
#undef TABLE_SIZE &lt;br /&gt;
#define TABLE_SIZE 100 &lt;br /&gt;
#endif &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
int table[TABLE_SIZE]; &lt;br /&gt;
Notice how the whole structure of #if, #elif and #else chained directives ends with #endif.&amp;nbsp; &lt;br /&gt;
The behavior of #ifdef and #ifndef can also be achieved by using the special operators defined and !defined &lt;br /&gt;
respectively in any #if or #elif directive:&amp;nbsp; &lt;br /&gt;
#if !defined TABLE_SIZE &lt;br /&gt;
#define TABLE_SIZE 100 &lt;br /&gt;
#elif defined ARRAY_SIZE &lt;br /&gt;
#define TABLE_SIZE ARRAY_SIZE &lt;br /&gt;
int table[TABLE_SIZE];&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Line control (#line) &lt;/b&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;When we compile a program and some error happen during the compiling process, the compiler shows an error &lt;br /&gt;
message with references to the name of the file where the error happened and a line number, so it is easier to find &lt;br /&gt;
the code generating the error.&amp;nbsp; &lt;br /&gt;
The #line directive allows us to control both things, the line numbers within the code files as well as the file name that we want that appears when an error takes place. Its format is:&amp;nbsp; &lt;br /&gt;
#line number &quot;filename&quot; &lt;br /&gt;
Where number is the new line number that will be assigned to the next code line. The line numbers of successive lines will be increased one by one from this point on. &lt;br /&gt;
&quot;filename&quot; is an optional parameter that allows to redefine the file name that will be shown. For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#line 20 &quot;assigning variable&quot; &lt;br /&gt;
int a?;&amp;nbsp; &lt;br /&gt;
This code will generate an error that will be shown as error in file &quot;assigning variable&quot;, line 20.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Error directive (#error) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This directive aborts the compilation process when it is found, generating a compilation the error that can be &lt;br /&gt;
specified as its parameter:&amp;nbsp; &lt;br /&gt;
#ifndef __cplusplus &lt;br /&gt;
#error A C++ compiler is required! &lt;br /&gt;
#endif&amp;nbsp; &lt;br /&gt;
This example aborts the compilation process if the macro name __cplusplus is not defined (this macro name is &lt;br /&gt;
defined by default in all C++ compilers).&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Source file inclusion (#include) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This directive has also been used assiduously in other sections of this tutorial. When the preprocessor finds an &lt;br /&gt;
#include directive it replaces it by the entire content of the specified file. There are two ways to specify a file to be &lt;br /&gt;
included:&amp;nbsp; &lt;br /&gt;
#include &quot;file&quot; &lt;br /&gt;
#include &amp;lt;file&amp;gt; &lt;br /&gt;
The only difference between both expressions is the places (directories) where the compiler is going to look for the file. In the first case where the file name is specified between double-quotes, the file is searched first in the same directory that includes the file containing the directive. In case that it is not there, the compiler searches the file in the default directories where it is configured to look for the standard header files. &lt;br /&gt;
If the file name is enclosed between angle-brackets &amp;lt;&amp;gt; the file is searched directly where the compiler is configured to look for the standard header files. Therefore, standard header files are usually included in angle-brackets, while other specific header files are included using quotes.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Pragma directive (#pragma) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This directive is used to specify diverse options to the compiler. These options are specific for the platform and the compiler you use. Consult the manual or the reference of your compiler for more information on the possible parameters that you can define with #pragma.&amp;nbsp; &lt;br /&gt;
If the compiler does not support a specific argument for #pragma, it is ignored - no error is generated.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Predefined macro names &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbavzp2HNSLuQknzRk8dAMpt6ZBvfetdv44ioP2S_mfXowG4PvMTKUNHuflbj_FYezq0lssnC4TDn6LlWCQU9cqzjl8kfhW4cvCW0Pq_LL9sIea0ukn2cy1okws4PNuPCi-GuxvX7cZwkk/s1600/amituufi.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; font-size: small; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px; font-weight: bold; white-space: nowrap;&quot;&gt;For example:&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: black; font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; font-size: small; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 11px; white-space: nowrap;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;border-collapse: separate; color: black; font-size: small; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black; font-size: 11px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;// standard macro names &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;This is the line number &quot; &amp;lt;&amp;lt; __LINE__; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot; of file &quot; &amp;lt;&amp;lt; __FILE__ &amp;lt;&amp;lt; &quot;.\n&quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Its compilation began &quot; &amp;lt;&amp;lt; __DATE__; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot; at &quot; &amp;lt;&amp;lt; __TIME__ &amp;lt;&amp;lt; &quot;.\n&quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;The compiler gives a __cplusplus value of &quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; __cplusplus; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
This is the line number 7 of file &lt;br /&gt;
/home/jay/stdmacronames.cpp. &lt;br /&gt;
Its compilation began Nov&amp;nbsp; 1 2005 at &lt;br /&gt;
10:12:29. &lt;br /&gt;
The compiler gives a __cplusplus value &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;of 1 &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #336666; font-size: 11px; font-weight: bold; white-space: nowrap;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/8148039828702659315/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/preprocessor-directives.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/8148039828702659315'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/8148039828702659315'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/preprocessor-directives.html' title='Preprocessor directives'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbavzp2HNSLuQknzRk8dAMpt6ZBvfetdv44ioP2S_mfXowG4PvMTKUNHuflbj_FYezq0lssnC4TDn6LlWCQU9cqzjl8kfhW4cvCW0Pq_LL9sIea0ukn2cy1okws4PNuPCi-GuxvX7cZwkk/s72-c/amituufi.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-3816312305555915905</id><published>2011-03-26T00:41:00.000-07:00</published><updated>2011-05-29T00:46:58.405-07:00</updated><title type='text'>Type Casting</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Converting an expression of a given type into another type is known as type-casting. We have already seen some ways to type cast:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Implicit conversion &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Implicit conversions do not require any operator. They are automatically performed when a value is copied to a &lt;br /&gt;
compatible type. For example:&amp;nbsp; &lt;br /&gt;
short a=2000; &lt;br /&gt;
int b; &lt;br /&gt;
b=a; &lt;br /&gt;
Here, the value of a has been promoted from short to int and we have not had to specify any type-casting &lt;br /&gt;
operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow &lt;br /&gt;
conversions such as the conversions between numerical types (short to int, int to float, double to int...), to or &lt;br /&gt;
from bool, and some pointer conversions. Some of these conversions may imply a loss of precision, which the &lt;br /&gt;
compiler can signal with a warning. This can be avoided with an explicit conversion.&amp;nbsp; &lt;br /&gt;
Implicit conversions also include constructor or operator conversions, which affect classes that include specific &lt;br /&gt;
constructors or operator functions to perform conversions. For example:&amp;nbsp; &lt;br /&gt;
class A {}; &lt;br /&gt;
class B { public: B (A a) {} }; &lt;br /&gt;
&lt;br /&gt;
A a; &lt;br /&gt;
B b=a; &lt;br /&gt;
Here, a implicit conversion happened between objects of class A and class B, because B has a constructor that takes an object of class A as parameter. Therefore implicit conversions from A to B are allowed.&amp;nbsp; &lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Explicit conversion &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the &lt;br /&gt;
value, require an explicit conversion. We have already seen two notations for explicit type conversion: functional &lt;br /&gt;
and c-like casting:&amp;nbsp; &lt;br /&gt;
short a=2000; &lt;br /&gt;
int b; &lt;br /&gt;
b = (int) a;&amp;nbsp;&amp;nbsp;&amp;nbsp; // c-like cast notation &lt;br /&gt;
b = int (a);&amp;nbsp;&amp;nbsp;&amp;nbsp; // functional notation&amp;nbsp; &lt;br /&gt;
The functionality of these explicit conversion operators is enough for most needs with fundamental data types. &lt;br /&gt;
However, these operators can be applied indiscriminately on classes and pointers to classes, which can lead to code that while being syntactically correct can cause runtime errors. For example, the following code is syntactically correct:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
// class type-casting &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CDummy { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; float i,j; &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
class CAddition { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int x,y; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CAddition (int a, int b) { x=a; y=b; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int result() { return x+y;} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CDummy d; &lt;br /&gt;
&amp;nbsp; CAddition * padd; &lt;br /&gt;
&amp;nbsp; padd = (CAddition*) &amp;amp;d; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; padd-&amp;gt;result(); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
The program declares a pointer to CAddition, but then it assigns to it a reference to an object of another &lt;br /&gt;
incompatible type using explicit type-casting:&amp;nbsp; &lt;br /&gt;
padd = (CAddition*) &amp;amp;d; &lt;br /&gt;
Traditional explicit type-casting allows to convert any pointer into any other pointer type, independently of the &lt;br /&gt;
types they point to. The subsequent call to member result will produce either a run-time error or a unexpected &lt;br /&gt;
result.&amp;nbsp; &lt;br /&gt;
In order to control these types of conversions between classes, we have four specific casting operators: &lt;br /&gt;
dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed &lt;br /&gt;
between angle-brackets (&amp;lt;&amp;gt;) and immediately after, the expression to be converted between parentheses.&amp;nbsp; &lt;br /&gt;
dynamic_cast &amp;lt;new_type&amp;gt; (expression) &lt;br /&gt;
reinterpret_cast &amp;lt;new_type&amp;gt; (expression) &lt;br /&gt;
static_cast &amp;lt;new_type&amp;gt; (expression) &lt;br /&gt;
const_cast &amp;lt;new_type&amp;gt; (expression) &lt;br /&gt;
The traditional type-casting equivalents to these expressions would be: &lt;br /&gt;
&lt;br /&gt;
(new_type) expression &lt;br /&gt;
new_type (expression) &lt;br /&gt;
but each one with its own special characteristics:&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;dynamic_cast &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.&amp;nbsp; &lt;br /&gt;
Therefore, dynamic_cast is always successful when we cast a class to one of its base classes:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
class CBase { }; &lt;br /&gt;
class CDerived: public CBase { }; &lt;br /&gt;
&lt;br /&gt;
CBase b; CBase* pb; &lt;br /&gt;
CDerived d; CDerived* pd; &lt;br /&gt;
&lt;br /&gt;
pb = dynamic_cast&amp;lt;CBase*&amp;gt;(&amp;amp;d);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ok: derived-to-base &lt;br /&gt;
pd = dynamic_cast&amp;lt;CDerived*&amp;gt;(&amp;amp;b);&amp;nbsp; // wrong: base-to-derived &lt;br /&gt;
The second conversion in this piece of code would produce a compilation error since base-to-derived conversions &lt;br /&gt;
are not allowed with dynamic_cast unless the base class is polymorphic.&amp;nbsp; &lt;br /&gt;
When a class is polymorphic, dynamic_cast performs a special checking during runtime to ensure that the &lt;br /&gt;
expression yields a valid complete object of the requested class:&amp;nbsp; &lt;br /&gt;
// dynamic_cast &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;exception&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CBase { virtual void dummy() {} }; &lt;br /&gt;
class CDerived: public CBase { int a; }; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; try { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CBase * pba = new CDerived; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CBase * pbb = new CBase; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CDerived * pd; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; pd = dynamic_cast&amp;lt;CDerived*&amp;gt;(pba); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (pd==0) cout &amp;lt;&amp;lt; &quot;Null pointer on first type-cast&quot; &amp;lt;&amp;lt; endl; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; pd = dynamic_cast&amp;lt;CDerived*&amp;gt;(pbb); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (pd==0) cout &amp;lt;&amp;lt; &quot;Null pointer on second type-cast&quot; &amp;lt;&amp;lt; endl; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; } catch (exception&amp;amp; e) {cout &amp;lt;&amp;lt; &quot;Exception: &quot; &amp;lt;&amp;lt; e.what();} &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Null pointer on second type-&lt;br /&gt;
cast&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Compatibility note:&lt;/b&gt; dynamic_cast requires the Run-Time Type Information (RTTI) to keep track of dynamic types. Some compilers support this feature as an option which is disabled by default. This must be enabled for runtime type checking using dynamic_cast to work properly. &lt;br /&gt;
&lt;br /&gt;
The code tries to perform two dynamic casts from pointer objects of type CBase* (pba and pbb) to a pointer object &lt;br /&gt;
of type CDerived*, but only the first one is successful. Notice their respective initializations:&amp;nbsp; &lt;br /&gt;
CBase * pba = new CDerived; &lt;br /&gt;
CBase * pbb = new CBase; &lt;br /&gt;
Even though both are pointers of type CBase*, pba points to an object of type CDerived, while pbb points to an &lt;br /&gt;
object of type CBase. Thus, when their respective type-castings are performed using dynamic_cast, pba is pointing &lt;br /&gt;
to a full object of class CDerived, whereas pbb is pointing to an object of class CBase, which is an incomplete &lt;br /&gt;
object of class CDerived. &lt;br /&gt;
&lt;br /&gt;
When dynamic_cast cannot cast a pointer because it is not a complete object of the required class -as in the &lt;br /&gt;
second conversion in the previous example- it returns a null pointer to indicate the failure. If dynamic_cast is used to convert to a reference type and the conversion is not possible, an exception of type bad_cast is thrown instead.&amp;nbsp; dynamic_cast can also cast null pointers even between pointers to unrelated classes, and can also cast pointers of any type to void pointers (void*).&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;static_cast &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
static_cast can perform conversions between pointers to related classes, not only from the derived class to its &lt;br /&gt;
base, but also from a base class to its derived. This ensures that at least the classes are compatible if the proper &lt;br /&gt;
object is converted, but no safety check is performed during runtime to check if the object being converted is in &lt;br /&gt;
fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is &lt;br /&gt;
safe. On the other side, the overhead of the type-safety checks of dynamic_cast is avoided.&amp;nbsp; &lt;br /&gt;
class CBase {}; &lt;br /&gt;
class CDerived: public CBase {}; &lt;br /&gt;
CBase * a = new CBase; &lt;br /&gt;
CDerived * b = static_cast&amp;lt;CDerived*&amp;gt;(a); &lt;br /&gt;
This would be valid, although b would point to an incomplete object of the class and could lead to runtime errors if &lt;br /&gt;
dereferenced.&amp;nbsp; &lt;br /&gt;
static_cast can also be used to perform any other non-pointer conversion that could also be performed implicitly, &lt;br /&gt;
like for example standard conversion between fundamental types:&amp;nbsp; &lt;br /&gt;
double d=3.14159265; &lt;br /&gt;
int i = static_cast&amp;lt;int&amp;gt;(d); &lt;br /&gt;
Or any conversion between classes with explicit constructors or operator functions as described in &quot;implicit &lt;br /&gt;
conversions&quot; above.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;reinterpret_cast &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation &lt;br /&gt;
result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: &lt;br /&gt;
neither the content pointed nor the pointer type itself is checked.&amp;nbsp; &lt;br /&gt;
It can also cast pointers to or from integer types. The format in which this integer value represents a pointer is &lt;br /&gt;
platform-specific. The only guarantee is that a pointer cast to an integer type large enough to fully contain it, is &lt;br /&gt;
granted to be able to be cast back to a valid pointer.&amp;nbsp; &lt;br /&gt;
The conversions that can be performed by reinterpret_cast but not by static_cast have no specific uses in &lt;br /&gt;
C++ are low-level operations, whose interpretation results in code which is generally system-specific, and thus &lt;br /&gt;
non-portable. For example:&amp;nbsp; &lt;br /&gt;
class A {}; &lt;br /&gt;
class B {}; &lt;br /&gt;
A * a = new A; &lt;br /&gt;
B * b = reinterpret_cast&amp;lt;B*&amp;gt;(a); &lt;br /&gt;
This is valid C++ code, although it does not make much sense, since now we have a pointer that points to an &lt;br /&gt;
object of an incompatible class, and thus dereferencing it is unsafe.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;const_cast &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This type of casting manipulates the constness of an object, either to be set or to be removed. For example, in &lt;br /&gt;
order to pass a const argument to a function that expects a non-constant parameter:&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
131 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// const_cast &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void print (char * str) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; const char * c = &quot;sample text&quot;; &lt;br /&gt;
&amp;nbsp; print ( const_cast&amp;lt;char *&amp;gt; (c) ); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
sample text&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;typeid &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
typeid allows to check the type of an expression:&amp;nbsp; &lt;br /&gt;
typeid (expression) &lt;br /&gt;
This operator returns a reference to a constant object of type type_info that is defined in the standard header file &lt;br /&gt;
&amp;lt;typeinfo&amp;gt;. This returned value can be compared with another one using operators == and != or can serve to &lt;br /&gt;
obtain a null-terminated character sequence representing the data type or class name by using its name() member.&amp;nbsp; &lt;br /&gt;
// typeid &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; int * a,b; &lt;br /&gt;
&amp;nbsp; a=0; b=0; &lt;br /&gt;
&amp;nbsp; if (typeid(a) != typeid(b)) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;a and b are of different types:\n&quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;a is: &quot; &amp;lt;&amp;lt; typeid(a).name() &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;b is: &quot; &amp;lt;&amp;lt; typeid(b).name() &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
a and b are of different types: &lt;br /&gt;
a is: int * &lt;br /&gt;
b is: int&amp;nbsp;&amp;nbsp; &lt;br /&gt;
When typeid is applied to classes typeid uses the RTTI to keep track of the type of dynamic objects. When typeid is applied to an expression whose type is a polymorphic class, the result is the type of the most derived complete object:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
// typeid, polymorphic class &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;typeinfo&amp;gt; &lt;br /&gt;
#include &amp;lt;exception&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CBase { virtual void f(){} }; &lt;br /&gt;
class CDerived : public CBase {}; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; try { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CBase* a = new CBase; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CBase* b = new CDerived; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;a is: &quot; &amp;lt;&amp;lt; typeid(a).name() &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;b is: &quot; &amp;lt;&amp;lt; typeid(b).name() &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;*a is: &quot; &amp;lt;&amp;lt; typeid(*a).name() &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;*b is: &quot; &amp;lt;&amp;lt; typeid(*b).name() &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; } catch (exception&amp;amp; e) { cout &amp;lt;&amp;lt; &quot;Exception: &quot; &amp;lt;&amp;lt; e.what() &amp;lt;&amp;lt; endl; &lt;br /&gt;
} &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
a is: class CBase * &lt;br /&gt;
b is: class CBase * &lt;br /&gt;
*a is: class CBase &lt;br /&gt;
*b is: class CDerived &lt;br /&gt;
Notice how the type that typeid considers for pointers is the pointer type itself (both a and b are of type class &lt;br /&gt;
CBase *). However, when typeid is applied to objects (like *a and *b) typeid yields their dynamic type (i.e. the type of their most derived complete object).&amp;nbsp; &lt;br /&gt;
If the type typeid evaluates is a pointer preceded by the dereference operator (*), and this pointer has a null &lt;br /&gt;
value, typeid throws a bad_typeid exception. &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/3816312305555915905/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/type-casting.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3816312305555915905'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3816312305555915905'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/type-casting.html' title='Type Casting'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-6639140840797725395</id><published>2011-03-25T00:37:00.000-07:00</published><updated>2011-05-29T00:41:17.489-07:00</updated><title type='text'>Exceptions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring &lt;br /&gt;
control to special functions called handlers. &lt;br /&gt;
To catch exceptions we must place a portion of code under exception inspection. This is done by enclosing that &lt;br /&gt;
portion of code in a try block. When an exceptional circumstance arises within that block, an exception is thrown &lt;br /&gt;
that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all &lt;br /&gt;
handlers are ignored. &lt;br /&gt;
A exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with &lt;br /&gt;
the keyword catch, which must be placed immediately after the try block: &lt;br /&gt;
// exceptions &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; try &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; throw 20; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; catch (int e) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;An exception occurred. &quot; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Exception Nr. &quot; &amp;lt;&amp;lt; e &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
An exception occurred. Exception Nr. 20 &lt;br /&gt;
The code under exception handling is enclosed in a try block. In this example this code simply throws an &lt;br /&gt;
exception: &lt;br /&gt;
throw 20; &lt;br /&gt;
A throw expression accepts one parameter (in this case the integer value 20), which is passed as an argument to &lt;br /&gt;
the exception handler. &lt;br /&gt;
The exception handler is declared with the catch keyword. As you can see, it follows immediately the closing brace &lt;br /&gt;
of the try block. The catch format is similar to a regular function that always has at least one parameter. The type &lt;br /&gt;
of this parameter is very important, since the type of the argument passed by the throw expression is checked &lt;br /&gt;
against it, and only in the case they match, the exception is caught. &lt;br /&gt;
We can chain multiple handlers (catch expressions), each one with a different parameter type. Only the handler that matches its type with the argument specified in the throw statement is executed. &lt;br /&gt;
If we use an ellipsis (...) as the parameter of catch, that handler will catch any exception no matter what the &lt;br /&gt;
type of the throw exception is. This can be used as a default handler that catches all exceptions not caught by &lt;br /&gt;
other handlers if it is specified at last:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
try { &lt;br /&gt;
&amp;nbsp; // code here &lt;br /&gt;
} &lt;br /&gt;
catch (int param) { cout &amp;lt;&amp;lt; &quot;int exception&quot;; } &lt;br /&gt;
catch (char param) { cout &amp;lt;&amp;lt; &quot;char exception&quot;; } &lt;br /&gt;
catch (...) { cout &amp;lt;&amp;lt; &quot;default exception&quot;; } &lt;br /&gt;
In this case the last handler would catch any exception thrown with any parameter that is neither an int nor a &lt;br /&gt;
char.&amp;nbsp; &lt;br /&gt;
After an exception has been handled the program execution resumes after the try-catch block, not after the &lt;br /&gt;
throw statement!.&amp;nbsp; &lt;br /&gt;
It is also possible to nest try-catch blocks within more external try blocks. In these cases, we have the possibility &lt;br /&gt;
that an internal catch block forwards the exception to its external level. This is done with the expression throw; &lt;br /&gt;
with no arguments. For example:&amp;nbsp; &lt;br /&gt;
try { &lt;br /&gt;
&amp;nbsp; try { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // code here &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; catch (int n) { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
} &lt;br /&gt;
catch (...) { &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Exception occurred&quot;; &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Exception specifications &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
When declaring a function we can limit the exception type it might directly or indirectly throw by appending a &lt;br /&gt;
throw suffix to the function declaration:&amp;nbsp; &lt;br /&gt;
float myfunction (char param) throw (int); &lt;br /&gt;
This declares a function called myfunction which takes one agument of type char and returns an element of type &lt;br /&gt;
float. The only exception that this function might throw is an exception of type int. If it throws an exception with &lt;br /&gt;
a different type, either directly or indirectly, it cannot be caught by a regular int-type handler.&amp;nbsp; &lt;br /&gt;
If this throw specifier is left empty with no type, this means the function is not allowed to throw exceptions. &lt;br /&gt;
Functions with no throw specifier (regular functions) are allowed to throw exceptions with any type:&amp;nbsp; &lt;br /&gt;
int myfunction (int param) throw(); // no exceptions allowed &lt;br /&gt;
int myfunction (int param);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // all exceptions allowed &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Standard exceptions &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called exception and is defined in the &amp;lt;exception&amp;gt; header file under the namespace std. This class has the usual default and copy constructors, operators and destructors, plus an additional virtual member function called what that returns a null-terminated character sequence (char *) and that can be overwritten in derived classes to contain some sort of description of the exception.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
// standard exceptions &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;exception&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class myexception: public exception &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; virtual const char* what() const throw() &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return &quot;My exception happened&quot;; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
} myex; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; try &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; throw myex; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; catch (exception&amp;amp; e) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; e.what() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
My exception happened. &lt;br /&gt;
We have placed a handler that catches exception objects by reference (notice the ampersand &amp;amp; after the type), therefore this catches also classes derived from exception, like our myex object of class myexception.&amp;nbsp; &lt;br /&gt;
All exceptions thrown by components of the C++ Standard library throw exceptions derived from this &lt;br /&gt;
std::exception class. These are:&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-WIa1ivhqIJr8mXMzL2_vSvVIePvArQs7CFBLEmcrAfVP2ybtoYAMrHR9QsxxS8L5B26GQPREy2ue-77jVIQj4IqVQZZhCT6XOg43yRwz4Gz9KL9Iqct-HmeTzhMQHgm5b8esxhSljRzY/s1600/amitkkkk.png&quot; /&gt;&lt;/div&gt;&lt;br /&gt;
For example, if we use the operator new and the memory cannot be allocated, an exception of type bad_alloc is &lt;br /&gt;
thrown:&amp;nbsp; &lt;br /&gt;
try &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int * myarray= new int[1000]; &lt;br /&gt;
} &lt;br /&gt;
catch (bad_alloc&amp;amp;) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Error allocating memory.&quot; &amp;lt;&amp;lt; endl; &lt;br /&gt;
} &lt;br /&gt;
It is recommended to include all dynamic memory allocations within a try block that catches this type of exception to perform a clean action instead of an abnormal program termination, which is what happens when this type of exception is thrown and not caught. If you want to force a bad_alloc exception to see it in action, you can try to allocate a huge array; On my system, trying to allocate 1 billion ints threw a bad_alloc exception.&amp;nbsp; Because bad_alloc is derived from the standard base class exception, we can handle that same exception by catching references to the exception class:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
// bad_alloc standard exception &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;exception&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; try &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int* myarray= new int[1000]; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; catch (exception&amp;amp; e) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Standard exception: &quot; &amp;lt;&amp;lt; e.what() &lt;br /&gt;
&amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/6639140840797725395/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/exceptions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/6639140840797725395'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/6639140840797725395'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/exceptions.html' title='Exceptions'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-WIa1ivhqIJr8mXMzL2_vSvVIePvArQs7CFBLEmcrAfVP2ybtoYAMrHR9QsxxS8L5B26GQPREy2ue-77jVIQj4IqVQZZhCT6XOg43yRwz4Gz9KL9Iqct-HmeTzhMQHgm5b8esxhSljRzY/s72-c/amitkkkk.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-528969545693397908</id><published>2011-03-22T06:45:00.000-07:00</published><updated>2011-05-25T09:12:52.920-07:00</updated><title type='text'>Variables. Data Types.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The usefulness of the &quot;Hello World&quot; programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting program just to obtain a simple sentence &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;written on the screen as result. It certainly would have been much faster to type the output sentence by ourselves. However, programming is not limited only to printing simple texts on the screen. In order to go a little further on and to become able to write programs that perform useful tasks that really save us work we need to introduce the concept of variable.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Let us think that I ask you to retain the number 5 in your mental memory, and then I ask you to memorize also &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;the number 2 at the same time. You have just stored two different values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. Values that we could now for example subtract and obtain 4 as result.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables. The same process can be expressed in C++ with the following instruction set:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;a = 5; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;b = 2; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;a = a + 1; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;result = a - b; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Obviously, this is a very simple example since we have only used two small integer values, but consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Therefore, we can define a variable as a portion of memory to store a determined value.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Each variable needs an identifier that distinguishes it from the others, for example, in the previous code the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;variable identifiers were a, b and result, but we could have called the variables any names we wanted to invent, as long as they were valid identifiers.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Identifiers&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ), but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case they can begin with a digit. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler&#39;s specific ones, which are reserved keywords. The standard reserved keywords are:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;volatile, wchar_t, while&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Additionally, alternative representations for some operators cannot be used as identifiers since they are reserved words under some circumstances:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Your compiler may also include some additional specific reserved keywords. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;Very important:&lt;/b&gt;&lt;/u&gt; The C++ language is a &quot;case sensitive&quot; language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. Thus, for example, the RESULT variable is not the same as the result variable or the Result variable. These are three different variable identifiers.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Fundamental data types&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;When programming, we store the variables in our computer&#39;s memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.&amp;nbsp; The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Next you have a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdcB_Mz5R9bVLIibFlU2P1_XcL0ZuK4gqpDbE2hxpfepLSEYYt6Tx3oLne8wshxUyEF99O1mXQBmMkSRx_mnZonaup9SWKeCbvsoQkW1XZcGWGswrYk_zVwG374mUp2Rz4G16a_59EiYwu/s1600/c%252B%252B.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;* The values of the columns Size and Range depend on the system the program is compiled for. The values &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;shown above are those found on most 32-bit systems. But for other systems, the general specification is that int has the natural size suggested by the system architecture (one &quot;word&quot;) and the four integer types char,short, &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int and long must each one be at least as large as the one preceding it, with char being always 1 byte in size. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The same applies to the floating point types float, double and long double, where each one must provide at &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;least as much precision as the preceding one.&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Declaration of variables &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;int a; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;float mynumber;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in the program. &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;If you are going to declare more than one variable of the same type, you can declare all of them in a single &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;statement by separating their identifiers with commas. For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int a, b, c;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;This declares three variables (a, b and c), all of them of type int, and has exactly the same meaning as:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int a; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int b; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int c;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;The integer data types char, short, long and int can be either signed or unsigned depending on the range of &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;numbers needed to be represented. Signed types can represent both positive and negative values, whereas &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;unsigned types can only represent positive values (and zero). This can be specified by using either the specifier &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;signed or the specifier unsigned before the type name. For example:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;unsigned short int NumberOfSisters; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;signed int MyAccountBalance; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;By default, if we do not specify either signed or unsigned most compiler settings will assume the type to be &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;signed, therefore instead of the second declaration above we could have written:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int MyAccountBalance; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;with exactly the same meaning (with or without the keyword signed)&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;An exception to this general rule is the char type, which exists by itself and is considered a different fundamental data type from signed char and unsigned char, thought to store characters. You should use either signed or unsigned if you intend to store numerical values in a char-sized variable.&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;short and long can be used alone as type specifiers. In this case, they refer to their respective integer &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;fundamental types: short is equivalent to short int and long is equivalent to long int. The following two &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;variable declarations are equivalent:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;short Year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;short int Year; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Finally, signed and unsigned may also be used as standalone type specifiers, meaning the same as signed int &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;and unsigned int respectively. The following two declarations are equivalent:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;unsigned NextYear; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;unsigned int NextYear; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;To see what variable declarations look like in action within a program, we are going to see the C++ code of the xample about your mental memory proposed at the beginning of this section:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;// operating with variables &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; // declaring variables: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int a, b; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int result; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; // process: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; a = 5; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; b = 2; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; a = a + 1; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; result = a - b; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; // print out the result: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; result; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; // terminate the program: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;4&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Do not worry if something else than the variable declarations themselves looks a bit strange to you. You will see the rest in detail in coming sections. &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Scope of variables &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous code at the beginning of the body of the function main when we declared that a, b, and result were of type int.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimIMbnFq6rUQad2EiV_zk8_Lq6FgA6EsnmJu92BN1cTqSto6943uLVKpbfF2IpXsooEwu1w4lEaB5XbLo9lWXy7UHeCiX0Je1IyJYCZzbehyphenhyphenznVPdPYN6D7GQelG6efgj9Q5ShvHWPjKjF/s1600/amitc%252B%252B.jpg&quot; /&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration. The scope of local variables is limited to the block enclosed in braces ({}) where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main) their scope is between its declaration point and the end of that function. In the example above, this means that if another function existed in &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;addition to main, the local variables declared in main could not be accessed from the other function and vice versa. &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Initialization of variables &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;When declaring a regular local variable, its value is by default undetermined. But you may want a variable to store &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable. There &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;are two ways to do this in C++:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The first one, known as c-like, is done by appending an equal sign followed by the value to which the variable will &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;be initialized:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;type identifier = initial_value ;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;For example, if we want to declare an int variable called a initialized with a value of 0 at the moment in which it is &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;declared, we could write:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int a = 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The other way to initialize variables, known as constructor initialization, is done by enclosing the initial value &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;between parentheses (()):&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;type identifier (initial_value) ;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int a (0); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Both ways of initializing variables are valid and equivalent in C++.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// initialization of variables &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int a=5;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // initial value = 5 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int b(2);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // initial value = 2 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int result;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // initial value &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;undetermined &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; a = a + 3; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; result = a - b; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; result; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;6&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Introduction to strings &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Variables that can store non-numerical values that are longer than one single character are known as strings.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The C++ language library provides support for strings through the standard string class. This is not a &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;fundamental type, but it behaves in a similar way as fundamental types do in its most basic usage.&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A first difference with fundamental data types is that in order to declare and use objects (variables) of this type we &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;need to include an additional header file in our source code: &amp;lt;string&amp;gt; and have access to the std namespace &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;(which we already had in all our previous programs thanks to the using namespace statement).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// my first string &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;string&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string mystring = &quot;This is a string&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; mystring; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This is a string &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;As you may see in the previous example, strings can be initialized with any valid string literal just like numerical &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;type variables can be initialized to any valid numerical literal. Both initialization formats are valid with strings:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;string mystring = &quot;This is a string&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;string mystring (&quot;This is a string&quot;); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Strings can also perform all the other basic operations that fundamental data types can, like being declared without &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;an initial value and being assigned values during execution:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// my first string &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;string&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string mystring; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; mystring = &quot;This is the initial string content&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; mystring &amp;lt;&amp;lt; endl; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; mystring = &quot;This is a different string content&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; mystring &amp;lt;&amp;lt; endl; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This is the initial string content &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This is a different string content &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;For more details on C++ strings, you can have a look at the string class reference.&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/528969545693397908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/05/variables-data-types_25.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/528969545693397908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/528969545693397908'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/05/variables-data-types_25.html' title='Variables. Data Types.'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdcB_Mz5R9bVLIibFlU2P1_XcL0ZuK4gqpDbE2hxpfepLSEYYt6Tx3oLne8wshxUyEF99O1mXQBmMkSRx_mnZonaup9SWKeCbvsoQkW1XZcGWGswrYk_zVwG374mUp2Rz4G16a_59EiYwu/s72-c/c%252B%252B.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-5268651607285927711</id><published>2011-03-21T01:32:00.000-07:00</published><updated>2011-05-25T02:29:39.515-07:00</updated><title type='text'>Structure of a program</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Probably the best way to start learning a programming language is by writing a program. Therefore, here is our &lt;br /&gt;
first program:&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;__________________________________________________________________&amp;nbsp; &lt;br /&gt;
// my first program in C++ &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Hello World!&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
}&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;__________________________________________________________________ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;__________________________________________________________________ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Hello World!&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;__________________________________________________________________&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The first panel shows the source code for our first program. The second one shows the result of the program once &lt;br /&gt;
compiled and executed. The way to edit and compile a program depends on the compiler you are using. Depending &lt;br /&gt;
on whether it has a Development Interface or not and on its version. Consult the compilers section and the manual &lt;br /&gt;
or help included with your compiler if you have doubts on how to compile a C++ console program.&amp;nbsp; &lt;br /&gt;
The previous program is the typical program that programmer apprentices write for the first time, and its result is &lt;br /&gt;
the printing on screen of the &quot;Hello World!&quot; sentence. It is one of the simplest programs that can be written in &lt;br /&gt;
C++, but it already contains the fundamental components that every C++ program has. We are going to look line &lt;br /&gt;
by line at the code we have just written:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// my first program in C++&amp;nbsp; &lt;br /&gt;
This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not &lt;br /&gt;
have any effect on the behavior of the program. The programmer can use them to include short &lt;br /&gt;
explanations or observations within the source code itself. In this case, the line is a brief description of &lt;br /&gt;
what our program is.&amp;nbsp; &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&amp;nbsp; &lt;br /&gt;
Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines &lt;br /&gt;
with expressions but indications for the compiler&#39;s preprocessor. In this case the directive #include &lt;br /&gt;
&amp;lt;iostream&amp;gt; tells the preprocessor to include the iostream standard file. This specific file (iostream) &lt;br /&gt;
includes the declarations of the basic standard input-output library in C++, and it is included because its &lt;br /&gt;
functionality is going to be used later in the program.&amp;nbsp; &lt;br /&gt;
using namespace std;&amp;nbsp; &lt;br /&gt;
All the elements of the standard C++ library are declared within what is called a namespace, the &lt;br /&gt;
namespace with the name std. So in order to access its functionality we declare with this expression that &lt;br /&gt;
we will be using these entities. This line is very frequent in C++ programs that use the standard library, &lt;br /&gt;
and in fact it will be included in most of the source codes included in these tutorials. &lt;br /&gt;
int main ()&amp;nbsp; &lt;br /&gt;
This line corresponds to the beginning of the definition of the main function. The main function is the point &lt;br /&gt;
by where all C++ programs start their execution, independently of its location within the source code. It &lt;br /&gt;
does not matter whether there are other functions with other names defined before or after it - the &lt;br /&gt;
instructions contained within this function&#39;s definition will always be the first ones to be executed in any &lt;br /&gt;
C++ program. For that same reason, it is essential that all C++ programs have a main function.&amp;nbsp; &lt;br /&gt;
The word main is followed in the code by a pair of parentheses (()). That is because it is a function &lt;br /&gt;
declaration: In C++, what differentiates a function declaration from other types of expressions are these &lt;br /&gt;
parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within &lt;br /&gt;
them.&amp;nbsp; &lt;br /&gt;
Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is &lt;br /&gt;
contained within these braces is what the function does when it is executed.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;cout &amp;lt;&amp;lt; &quot;Hello World!&quot;;&amp;nbsp; &lt;br /&gt;
This line is a C++ statement. A statement is a simple or compound expression that can actually produce &lt;br /&gt;
some effect. In fact, this statement performs the only action that generates a visible effect in our first &lt;br /&gt;
program.&amp;nbsp; &lt;br /&gt;
cout represents the standard output stream in C++, and the meaning of the entire statement is to insert &lt;br /&gt;
a sequence of characters (in this case the Hello World sequence of characters) into the standard output &lt;br /&gt;
stream (which usually is the screen).&amp;nbsp; &lt;br /&gt;
cout is declared in the iostream standard file within the std namespace, so that&#39;s why we needed to &lt;br /&gt;
include that specific file and to declare that we were going to use this specific namespace earlier in our &lt;br /&gt;
code.&amp;nbsp; &lt;br /&gt;
Notice that the statement ends with a semicolon character (;). This character is used to mark the end of &lt;br /&gt;
the statement and in fact it must be included at the end of all expression statements in all C++ programs &lt;br /&gt;
(one of the most common syntax errors is indeed to forget to include some semicolon after a statement). &lt;br /&gt;
return 0;&amp;nbsp; &lt;br /&gt;
The return statement causes the main function to finish. return may be followed by a return code (in our &lt;br /&gt;
example is followed by the return code 0). A return code of 0 for the main function is generally interpreted &lt;br /&gt;
as the program worked as expected without any errors during its execution. This is the most usual way to &lt;br /&gt;
end a C++ console program. &lt;br /&gt;
You may have noticed that not all the lines of this program perform actions when the code is executed. There were &lt;br /&gt;
lines containing only comments (those beginning by //). There were lines with directives for the compiler&#39;s &lt;br /&gt;
preprocessor (those beginning by #). Then there were lines that began the declaration of a function (in this case, &lt;br /&gt;
the main function) and, finally lines with statements (like the insertion into cout), which were all included within &lt;br /&gt;
the block delimited by the braces ({}) of the main function.&amp;nbsp; &lt;br /&gt;
The program has been structured in different lines in order to be more readable, but in C++, we do not have strict &lt;br /&gt;
rules on how to separate instructions in different lines. For example, instead of&amp;nbsp; &lt;br /&gt;
int main ()&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;_________________________________________________________________&lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot; Hello World!&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;_________________________________________________________________&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;_________________________________________________________________&lt;br /&gt;
We could have written:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;__________________________________________________________________&lt;br /&gt;
int main () { cout &amp;lt;&amp;lt; &quot;Hello World!&quot;; return 0; }&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;_______________________________________________________________________________&lt;br /&gt;
All in just one line and this would have had exactly the same meaning as the previous code.&amp;nbsp; &lt;br /&gt;
In C++, the separation between statements is specified with an ending semicolon (;) at the end of each one, so &lt;br /&gt;
the separation in different code lines does not matter at all for this purpose. We can write many statements per &lt;br /&gt;
line or write a single statement that takes many code lines. The division of code in different lines serves only to &lt;br /&gt;
make it more legible and schematic for the humans that may read it.&amp;nbsp; &lt;br /&gt;
Let us add an additional instruction to our first program:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
9 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// my second program in C++ &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Hello World! &quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;I&#39;m a C++ program&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Hello World! I&#39;m a C++ program &lt;br /&gt;
In this case, we performed two insertions into cout in two different statements. Once again, the separation in &lt;br /&gt;
different lines of code has been done just to give greater readability to the program, since main could have been &lt;br /&gt;
perfectly valid defined this way:&amp;nbsp; &lt;br /&gt;
int main () { cout &amp;lt;&amp;lt; &quot; Hello World! &quot;; cout &amp;lt;&amp;lt; &quot; I&#39;m a C++ program &quot;; return 0; } &lt;br /&gt;
We were also free to divide the code into more lines if we considered it more convenient:&amp;nbsp; &lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;Hello World!&quot;; &lt;br /&gt;
&amp;nbsp; cout &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt; &quot;I&#39;m a C++ program&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
And the result would again have been exactly the same as in the previous examples.&amp;nbsp; &lt;br /&gt;
Preprocessor directives (those that begin by #) are out of this general rule since they are not statements. They are &lt;br /&gt;
lines read and processed by the preprocessor and do not produce any code by themselves. Preprocessor directives &lt;br /&gt;
must be specified in their own line and do not have to end with a semicolon (;).&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Comments &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only &lt;br /&gt;
to allow the programmer to insert notes or descriptions embedded within the source code. &lt;br /&gt;
C++ supports two ways to insert comments: &lt;br /&gt;
// line comment &lt;br /&gt;
/* block comment */ &lt;br /&gt;
The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up &lt;br /&gt;
to the end of that same line. The second one, known as block comment, discards everything between the /* &lt;br /&gt;
characters and the first appearance of the */ characters, with the possibility of including more than one line. &lt;br /&gt;
We are going to add comments to our second program:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;/* my second program in C++ &lt;br /&gt;
&amp;nbsp;&amp;nbsp; with more comments */ &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Hello World! &quot;;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints Hello &lt;br /&gt;
World! &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;I&#39;m a C++ program&quot;; // prints I&#39;m a &lt;br /&gt;
C++ program &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Hello World! I&#39;m a C++ program &lt;br /&gt;
If you include comments within the source code of your programs without using the comment characters &lt;br /&gt;
combinations //, /* or */, the compiler will take them as if they were C++ expressions, most likely causing one or &lt;br /&gt;
several error messages when you compile it.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/5268651607285927711/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/05/structure-of-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/5268651607285927711'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/5268651607285927711'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/05/structure-of-program.html' title='Structure of a program'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-529143931015638186</id><published>2011-03-21T00:32:00.000-07:00</published><updated>2011-05-29T00:36:05.728-07:00</updated><title type='text'>Templates</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Function templates &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Function templates are special functions that can operate with generic types. This allows us to create a function &lt;br /&gt;
template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.&amp;nbsp; &lt;br /&gt;
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.&amp;nbsp; &lt;br /&gt;
The format for declaring function templates with type parameters is:&amp;nbsp; &lt;br /&gt;
template &amp;lt;class identifier&amp;gt; function_declaration; &lt;br /&gt;
template &amp;lt;typename identifier&amp;gt; function_declaration; &lt;br /&gt;
The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its &lt;br /&gt;
use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.&amp;nbsp; &lt;br /&gt;
For example, to create a template function that returns the greater one of two objects we could use:&amp;nbsp; &lt;br /&gt;
template &amp;lt;class myType&amp;gt; &lt;br /&gt;
myType GetMax (myType a, myType b) { &lt;br /&gt;
&amp;nbsp;return (a&amp;gt;b?a:b); &lt;br /&gt;
} &lt;br /&gt;
Here we have created a template function with myType as its template parameter. This template parameter &lt;br /&gt;
represents a type that has not yet been specified, but that can be used in the template function as if it were a &lt;br /&gt;
regular type. As you can see, the function template GetMax returns the greater of two parameters of this still-&lt;br /&gt;
undefined type.&amp;nbsp; &lt;br /&gt;
To use this function template we use the following format for the function call:&amp;nbsp; &lt;br /&gt;
function_name &amp;lt;type&amp;gt; (parameters); &lt;br /&gt;
For example, to call GetMax to compare two integer values of type int we can write:&amp;nbsp; &lt;br /&gt;
int x,y; &lt;br /&gt;
GetMax &amp;lt;int&amp;gt; (x,y); &lt;br /&gt;
When the compiler encounters this call to a template function, it uses the template to automatically generate a &lt;br /&gt;
function replacing each appearance of myType by the type passed as the actual template parameter (int in this &lt;br /&gt;
case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.&amp;nbsp; &lt;br /&gt;
Here is the entire example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// function template &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
template &amp;lt;class T&amp;gt; &lt;br /&gt;
T GetMax (T a, T b) { &lt;br /&gt;
&amp;nbsp; T result; &lt;br /&gt;
&amp;nbsp; result = (a&amp;gt;b)? a : b; &lt;br /&gt;
&amp;nbsp; return (result); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; int i=5, j=6, k; &lt;br /&gt;
&amp;nbsp; long l=10, m=5, n; &lt;br /&gt;
&amp;nbsp; k=GetMax&amp;lt;int&amp;gt;(i,j); &lt;br /&gt;
&amp;nbsp; n=GetMax&amp;lt;long&amp;gt;(l,m); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; k &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
6 &lt;br /&gt;
10 &lt;br /&gt;
In this case, we have used T as the template parameter name instead of myType because it is shorter and in fact is &lt;br /&gt;
a very common template parameter name. But you can use any identifier you like.&amp;nbsp; &lt;br /&gt;
In the example above we used the function template GetMax() twice. The first time with arguments of type int &lt;br /&gt;
and the second one with arguments of type long. The compiler has instantiated and then called each time the &lt;br /&gt;
appropriate version of the function.&amp;nbsp; &lt;br /&gt;
As you can see, the type T is used within the GetMax() template function even to declare new objects of that type:&amp;nbsp; &lt;br /&gt;
T result; &lt;br /&gt;
Therefore, result will be an object of the same type as the parameters a and b when the function template is &lt;br /&gt;
instantiated with a specific type.&amp;nbsp; &lt;br /&gt;
In this specific case where the generic type T is used as a parameter for GetMax the compiler can find out &lt;br /&gt;
automatically which data type has to instantiate without having to explicitly specify it within angle brackets (like we &lt;br /&gt;
have done before specifying &amp;lt;int&amp;gt; and &amp;lt;long&amp;gt;). So we could have written instead:&amp;nbsp; &lt;br /&gt;
int i,j; &lt;br /&gt;
GetMax (i,j); &lt;br /&gt;
Since both i and j are of type int, and the compiler can automatically find out that the template parameter can &lt;br /&gt;
only be int. This implicit method produces exactly the same result:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
115 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// function template II &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
template &amp;lt;class T&amp;gt; &lt;br /&gt;
T GetMax (T a, T b) { &lt;br /&gt;
&amp;nbsp; return (a&amp;gt;b?a:b); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; int i=5, j=6, k; &lt;br /&gt;
&amp;nbsp; long l=10, m=5, n; &lt;br /&gt;
&amp;nbsp; k=GetMax(i,j); &lt;br /&gt;
&amp;nbsp; n=GetMax(l,m); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; k &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
6 &lt;br /&gt;
10 &lt;br /&gt;
Notice how in this case, we called our function template GetMax() without explicitly specifying the type between &lt;br /&gt;
angle-brackets &amp;lt;&amp;gt;. The compiler automatically determines what type is needed on each call.&amp;nbsp; &lt;br /&gt;
Because our template function includes only one template parameter (class T) and the function template itself &lt;br /&gt;
accepts two parameters, both of this T type, we cannot call our function template with two objects of different &lt;br /&gt;
types as arguments:&amp;nbsp; &lt;br /&gt;
int i; &lt;br /&gt;
long l; &lt;br /&gt;
k = GetMax (i,l); &lt;br /&gt;
This would not be correct, since our GetMax function template expects two arguments of the same type, and in this &lt;br /&gt;
call to it we use objects of two different types.&amp;nbsp; &lt;br /&gt;
We can also define function templates that accept more than one type parameter, simply by specifying more &lt;br /&gt;
template parameters between the angle brackets. For example:&amp;nbsp; &lt;br /&gt;
template &amp;lt;class T, class U&amp;gt; &lt;br /&gt;
T GetMin (T a, U b) { &lt;br /&gt;
&amp;nbsp; return (a&amp;lt;b?a:b); &lt;br /&gt;
} &lt;br /&gt;
In this case, our function template GetMin() accepts two parameters of different types and returns an object of the &lt;br /&gt;
same type as the first parameter (T) that is passed. For example, after that declaration we could call GetMin() &lt;br /&gt;
with:&amp;nbsp; &lt;br /&gt;
int i,j; &lt;br /&gt;
long l; &lt;br /&gt;
i = GetMin&amp;lt;int,long&amp;gt; (j,l); &lt;br /&gt;
or simply:&amp;nbsp; &lt;br /&gt;
i = GetMin (j,l); &lt;br /&gt;
even though j and l have different types, since the compiler can determine the appropriate instantiation anyway.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Class templates &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We also have the possibility to write class templates, so that a class can have members that use template &lt;br /&gt;
parameters as types. For example:&amp;nbsp; &lt;br /&gt;
template &amp;lt;class T&amp;gt; &lt;br /&gt;
class mypair { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T values [2]; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; mypair (T first, T second) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; values[0]=first; values[1]=second; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;
}; &lt;br /&gt;
The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to &lt;br /&gt;
declare an object of this class to store two integer values of type int with the values 115 and 36 we would write:&amp;nbsp; &lt;br /&gt;
mypair&amp;lt;int&amp;gt; myobject (115, 36); &lt;br /&gt;
this same class would also be used to create an object to store any other type:&amp;nbsp; &lt;br /&gt;
mypair&amp;lt;double&amp;gt; myfloats (3.0, 2.18); &lt;br /&gt;
The only member function in the previous class template has been defined inline within the class declaration itself. &lt;br /&gt;
In case that we define a function member outside the declaration of the class template, we must always precede &lt;br /&gt;
that definition with the template &amp;lt;...&amp;gt; prefix:&amp;nbsp; &lt;br /&gt;
// class templates &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
template &amp;lt;class T&amp;gt; &lt;br /&gt;
class mypair { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T a, b; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; mypair (T first, T second) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {a=first; b=second;} &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T getmax (); &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
template &amp;lt;class T&amp;gt; &lt;br /&gt;
T mypair&amp;lt;T&amp;gt;::getmax () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; T retval; &lt;br /&gt;
&amp;nbsp; retval = a&amp;gt;b? a : b; &lt;br /&gt;
&amp;nbsp; return retval; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; mypair &amp;lt;int&amp;gt; myobject (100, 75); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; myobject.getmax(); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
100 &lt;br /&gt;
Notice the syntax of the definition of member function getmax:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;template &amp;lt;class T&amp;gt; &lt;br /&gt;
T mypair&amp;lt;T&amp;gt;::getmax () &lt;br /&gt;
Confused by so many T&#39;s? There are three T&#39;s in this declaration: The first one is the template parameter. The &lt;br /&gt;
second T refers to the type returned by the function. And the third T (the one between angle brackets) is also a requirement: It specifies that this function&#39;s template parameter is also the class template parameter.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Template specialization &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;If we want to define a different implementation for a template when a specific type is passed as template &lt;br /&gt;
parameter, we can declare a specialization of that template.&amp;nbsp; &lt;br /&gt;
For example, let&#39;s suppose that we have a very simple class called mycontainer that can store one element of any &lt;br /&gt;
type and that it has just one member function called increase, which increases its value. But we find that when it &lt;br /&gt;
stores an element of type char it would be more convenient to have a completely different implementation with a &lt;br /&gt;
function member uppercase, so we decide to declare a class template specialization for that type:&amp;nbsp; &lt;br /&gt;
// template specialization &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
// class template: &lt;br /&gt;
template &amp;lt;class T&amp;gt; &lt;br /&gt;
class mycontainer { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T element; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; mycontainer (T arg) {element=arg;} &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T increase () {return ++element;} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
// class template specialization: &lt;br /&gt;
template &amp;lt;&amp;gt; &lt;br /&gt;
class mycontainer &amp;lt;char&amp;gt; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; char element; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; mycontainer (char arg) {element=arg;} &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; char uppercase () &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((element&amp;gt;=&#39;a&#39;)&amp;amp;&amp;amp;(element&amp;lt;=&#39;z&#39;)) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; element+=&#39;A&#39;-&#39;a&#39;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return element; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; mycontainer&amp;lt;int&amp;gt; myint (7); &lt;br /&gt;
&amp;nbsp; mycontainer&amp;lt;char&amp;gt; mychar (&#39;j&#39;); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; myint.increase() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; mychar.uppercase() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
8 &lt;br /&gt;
J &lt;br /&gt;
This is the syntax used in the class template specialization:&amp;nbsp; &lt;br /&gt;
template &amp;lt;&amp;gt; class mycontainer &amp;lt;char&amp;gt; { ... }; &lt;br /&gt;
First of all, notice that we precede the class template name with an emptytemplate&amp;lt;&amp;gt; parameter list. This is to &lt;br /&gt;
explicitly declare it as a template specialization.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;But more important than this prefix, is the &amp;lt;char&amp;gt; specialization parameter after the class template name. This &lt;br /&gt;
specialization parameter itself identifies the type for which we are going to declare a template class specialization &lt;br /&gt;
(char). Notice the differences between the generic class template and the specialization:&amp;nbsp; &lt;br /&gt;
template &amp;lt;class T&amp;gt; class mycontainer { ... }; &lt;br /&gt;
template &amp;lt;&amp;gt; class mycontainer &amp;lt;char&amp;gt; { ... }; &lt;br /&gt;
The first line is the generic template, and the second one is the specialization.&amp;nbsp; &lt;br /&gt;
When we declare specializations for a template class, we must also define all its members, even those exactly &lt;br /&gt;
equal to the generic template class, because there is no &quot;inheritance&quot; of members from the generic template to the specialization.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Non-type parameters for templates &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Besides the template arguments that are preceded by the class or typename keywords , which represent types, &lt;br /&gt;
templates can also have regular typed parameters, similar to those found in functions. As an example, have a look &lt;br /&gt;
at this class template that is used to contain sequences of elements:&amp;nbsp; &lt;br /&gt;
// sequence template &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
template &amp;lt;class T, int N&amp;gt; &lt;br /&gt;
class mysequence { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T memblock [N]; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void setmember (int x, T value); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; T getmember (int x); &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
template &amp;lt;class T, int N&amp;gt; &lt;br /&gt;
void mysequence&amp;lt;T,N&amp;gt;::setmember (int x, T value) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; memblock[x]=value; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
template &amp;lt;class T, int N&amp;gt; &lt;br /&gt;
T mysequence&amp;lt;T,N&amp;gt;::getmember (int x) { &lt;br /&gt;
&amp;nbsp; return memblock[x]; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; mysequence &amp;lt;int,5&amp;gt; myints; &lt;br /&gt;
&amp;nbsp; mysequence &amp;lt;double,5&amp;gt; myfloats; &lt;br /&gt;
&amp;nbsp; myints.setmember (0,100); &lt;br /&gt;
&amp;nbsp; myfloats.setmember (3,3.1416); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; myints.getmember(0) &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; myfloats.getmember(3) &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
100 &lt;br /&gt;
3.1416&amp;nbsp; &lt;br /&gt;
It is also possible to set default values or types for class template parameters. For example, if the previous class &lt;br /&gt;
template definition had been:&amp;nbsp; &lt;br /&gt;
template &amp;lt;class T=char, int N=10&amp;gt; class mysequence {..}; &lt;br /&gt;
We could create objects using the default template parameters by declaring:&amp;nbsp; &lt;br /&gt;
mysequence&amp;lt;&amp;gt; myseq; &lt;br /&gt;
Which would be equivalent to:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Templates and multiple-file projects&amp;nbsp;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;From the point of view of the compiler, templates are not normal functions or classes. They are compiled on &lt;br /&gt;
demand, meaning that the code of a template function is not compiled until an instantiation with specific template &lt;br /&gt;
arguments is required. At that moment, when an instantiation is required, the compiler generates a function &lt;br /&gt;
specifically for those arguments from the template.&amp;nbsp; &lt;br /&gt;
When projects grow it is usual to split the code of a program in different source code files. In these cases, the &lt;br /&gt;
interface and implementation are generally separated. Taking a library of functions as example, the interface &lt;br /&gt;
generally consists of declarations of the prototypes of all the functions that can be called. These are generally &lt;br /&gt;
declared in a &quot;header file&quot; with a .h extension, and the implementation (the definition of these functions) is in an &lt;br /&gt;
independent file with c++ code.&amp;nbsp; &lt;br /&gt;
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation &lt;br /&gt;
(definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.&amp;nbsp; Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors. &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/529143931015638186/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/templates.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/529143931015638186'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/529143931015638186'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/templates.html' title='Templates'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-1334600648100760772</id><published>2011-03-20T00:03:00.000-07:00</published><updated>2011-05-29T00:31:09.880-07:00</updated><title type='text'>Operators</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Once we know of the existence of variables and constants, we can begin to operate with them. For that purpose, C++ integrates operators. Unlike other languages whose operators are mainly keywords, operators in C++ are mostly made of signs that are not part of the alphabet but are available in all keyboards. This makes C++ code shorter and more international, since it relies less on English words, but requires a little of learning effort in the beginning.  You do not have to memorize all the content of this page. Most details are only provided to serve as a later reference in case you need it.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Assignment (=)&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The assignment operator assigns a value to a variable.&amp;nbsp; &lt;br /&gt;
a = 5; &lt;br /&gt;
This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is &lt;br /&gt;
known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable &lt;br /&gt;
whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these. &lt;br /&gt;
The most important rule when assigning is the right-to-left rule: The assignment operation always takes place from &lt;br /&gt;
right to left, and never the other way:&amp;nbsp; &lt;br /&gt;
a = b; &lt;br /&gt;
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue). The value that was &lt;br /&gt;
stored until this moment in a is not considered at all in this operation, and in fact that value is lost.&amp;nbsp; &lt;br /&gt;
Consider also that we are only assigning the value of b to a at the moment of the assignment operation. Therefore &lt;br /&gt;
a later change of b will not affect the new value of a.&amp;nbsp; &lt;br /&gt;
For example, let us have a look at the following code - I have included the evolution of the content stored in the &lt;br /&gt;
variables as comments:&amp;nbsp; &lt;br /&gt;
// assignment operator &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int a, b;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a:?,&amp;nbsp; b:? &lt;br /&gt;
&amp;nbsp; a = 10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a:10, b:? &lt;br /&gt;
&amp;nbsp; b = 4;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a:10, b:4 &lt;br /&gt;
&amp;nbsp; a = b;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a:4,&amp;nbsp; b:4 &lt;br /&gt;
&amp;nbsp; b = 7;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a:4,&amp;nbsp; b:7 &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;a:&quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; a; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot; b:&quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; b; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
a:4 b:7 &lt;br /&gt;
This code will give us as result that the value contained in a is 4 and the one contained in b is 7. Notice how a was not affected by the final modification of b, even though we declared a = b earlier (that is because of the right-to-left rule).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A property that C++ has over other programming languages is that the assignment operation can be used as the &lt;br /&gt;
rvalue (or part of an rvalue) for another assignment operation. For example:&amp;nbsp; &lt;br /&gt;
a = 2 + (b = 5); &lt;br /&gt;
is equivalent to:&amp;nbsp; &lt;br /&gt;
b = 5; &lt;br /&gt;
a = 2 + b; &lt;br /&gt;
that means: first assign 5 to variable b and then assign to a the value 2 plus the result of the previous assignment &lt;br /&gt;
of b (i.e. 5), leaving a with a final value of 7.&amp;nbsp; &lt;br /&gt;
The following expression is also valid in C++:&amp;nbsp; &lt;br /&gt;
a = b = c = 5; &lt;br /&gt;
It assigns 5 to the all the three variables: a, b and c.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Arithmetic operators ( +, -, *, /, % ) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The five arithmetical operations supported by the C++ language are:&amp;nbsp; &lt;br /&gt;
+ addition &lt;br /&gt;
-&amp;nbsp; subtraction &lt;br /&gt;
*&amp;nbsp; multiplication &lt;br /&gt;
/&amp;nbsp; division &lt;br /&gt;
% modulo &lt;br /&gt;
Operations of addition, subtraction, multiplication and division literally correspond with their respective &lt;br /&gt;
mathematical operators. The only one that you might not be so used to see is modulo; whose operator is the &lt;br /&gt;
percentage sign (%). Modulo is the operation that gives the remainder of a division of two values. For example, if &lt;br /&gt;
we write:&amp;nbsp; &lt;br /&gt;
a = 11 % 3; &lt;br /&gt;
the variable a will contain the value 2, since 2 is the remainder from dividing 11 between 3.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Compound assignment (+=, -=, *=, /=, %=, &amp;gt;&amp;gt;=, &amp;lt;&amp;lt;=, &amp;amp;=, &lt;br /&gt;
^=, |=) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA0fyroyrBizwfnNAq65b0o3BVu524plV_RrlUMNGjVIOrMReaY1yrLHhIe9sG8j43OqfXLflduzSS4uOwoW2rOyVBarsq3xl3MJGNwwQ_ZM4RrSSlaBOPl6sai-Csg0xNKHAKsacGu37f/s1600/ye+amit.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;and the same for all other operators. For example:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// compound assignment operators &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int a, b=3; &lt;br /&gt;
&amp;nbsp; a = b; &lt;br /&gt;
&amp;nbsp; a+=2;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // equivalent to a=a+2 &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; a; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
5 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Increase and decrease (++, --) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or &lt;br /&gt;
reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:&amp;nbsp; &lt;br /&gt;
c++; &lt;br /&gt;
c+=1; &lt;br /&gt;
c=c+1; &lt;br /&gt;
are all equivalent in its functionality: the three of them increase by one the value of c.&amp;nbsp; &lt;br /&gt;
In the early C compilers, the three previous expressions probably produced different executable code depending on &lt;br /&gt;
which one was used. Nowadays, this type of code optimization is generally done automatically by the compiler, &lt;br /&gt;
thus the three expressions should produce exactly the same executable code.&amp;nbsp; &lt;br /&gt;
A characteristic of this operator is that it can be used both as a prefix and as a suffix. That means that it can be &lt;br /&gt;
written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a &lt;br /&gt;
both have exactly the same meaning, in other expressions in which the result of the increase or decrease operation &lt;br /&gt;
is evaluated as a value in an outer expression they may have an important difference in their meaning: In the case &lt;br /&gt;
that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is &lt;br /&gt;
evaluated and therefore the increased value is considered in the outer expression; in case that it is used as a suffix &lt;br /&gt;
(a++) the value stored in a is increased after being evaluated and therefore the value stored before the increase &lt;br /&gt;
operation is evaluated in the outer expression. Notice the difference:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;b&gt;Example 1&lt;/b&gt; - B=3; &lt;br /&gt;
A=++B; &lt;br /&gt;
// A contains 4, B contains 4&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;b&gt;Example 2&amp;nbsp; &lt;/b&gt;B=3; &lt;br /&gt;
A=B++; &lt;br /&gt;
// A contains 3, B contains 4&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and then B is increased.&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Relational and equality operators ( ==, !=, &amp;gt;, &amp;lt;, &amp;gt;=, &amp;lt;= ) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result.&amp;nbsp; We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. Here is a list of the relational and equality operators that can be used in C++:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;== Equal to &lt;br /&gt;
!=&amp;nbsp; Not equal to &lt;br /&gt;
&amp;gt;&amp;nbsp;&amp;nbsp; Greater than &lt;br /&gt;
&amp;lt;&amp;nbsp;&amp;nbsp; Less than &lt;br /&gt;
&amp;gt;= Greater than or equal to &lt;br /&gt;
&amp;lt;= Less than or equal to &lt;br /&gt;
Here there are some examples:&amp;nbsp; &lt;br /&gt;
(7 == 5)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to false. &lt;br /&gt;
(5 &amp;gt; 4)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to true. &lt;br /&gt;
(3 != 2)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to true. &lt;br /&gt;
(6 &amp;gt;= 6)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to true. &lt;br /&gt;
(5 &amp;lt; 5)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to false. &lt;br /&gt;
Of course, instead of using only numeric constants, we can use any valid expression, including variables. Suppose &lt;br /&gt;
that a=2, b=3 and c=6,&amp;nbsp; &lt;br /&gt;
(a == 5)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to false since a is not equal to 5. &lt;br /&gt;
(a*b &amp;gt;= c)&amp;nbsp;&amp;nbsp; // evaluates to true since (2*3 &amp;gt;= 6) is true.&amp;nbsp; &lt;br /&gt;
(b+4 &amp;gt; a*c)&amp;nbsp; // evaluates to false since (3+4 &amp;gt; 2*6) is false. &lt;br /&gt;
((b=2) == a) // evaluates to true.&amp;nbsp;&amp;nbsp; &lt;br /&gt;
Be careful! The operator = (one equal sign) is not the same as the operator == (two equal signs), the first one is an assignment operator (assigns the value at its right to the variable at its left) and the other one (==) is the equality operator that compares whether both expressions in the two sides of it are equal to each other. Thus, in the last expression ((b=2) == a), we first assigned the value 2 to b and then we compared it to a, that also stores the value 2, so the result of the operation is true.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Logical operators ( !, &amp;amp;&amp;amp;, || )&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its &lt;br /&gt;
right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its &lt;br /&gt;
operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:&amp;nbsp; &lt;br /&gt;
!(5 == 5)&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to false because the expression at its right (5 == 5) is true. &lt;br /&gt;
!(6 &amp;lt;= 4)&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to true because (6 &amp;lt;= 4) would be false.&amp;nbsp; &lt;br /&gt;
!true&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to false &lt;br /&gt;
!false&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // evaluates to true.&amp;nbsp;&amp;nbsp; &lt;br /&gt;
The logical operators &amp;amp;&amp;amp; and || are used when evaluating two expressions to obtain a single relational result. The &lt;br /&gt;
operator &amp;amp;&amp;amp; corresponds with Boolean logical operation AND. This operation results true if both its two operands &lt;br /&gt;
are true, and false otherwise. The following panel shows the result of operator &amp;amp;&amp;amp; evaluating the expression a &amp;amp;&amp;amp; &lt;br /&gt;
b:&amp;nbsp; &lt;br /&gt;
&amp;amp;&amp;amp; OPERATOR&amp;nbsp; &lt;br /&gt;
a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&amp;nbsp;&amp;nbsp; a &amp;amp;&amp;amp; b &lt;br /&gt;
true true true &lt;br /&gt;
true false false &lt;br /&gt;
false true false &lt;br /&gt;
false false false &lt;br /&gt;
The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results of a || b:&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;|| OPERATOR&amp;nbsp; &lt;br /&gt;
a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&amp;nbsp;&amp;nbsp; a || b &lt;br /&gt;
true true true &lt;br /&gt;
true false true &lt;br /&gt;
false true true &lt;br /&gt;
false false false &lt;br /&gt;
For example:&amp;nbsp; &lt;br /&gt;
( (5 == 5) &amp;amp;&amp;amp; (3 &amp;gt; 6) )&amp;nbsp; // evaluates to false ( true &amp;amp;&amp;amp; false ). &lt;br /&gt;
( (5 == 5) || (3 &amp;gt; 6) )&amp;nbsp; // evaluates to true ( true || false ).&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Conditional operator ( ? )&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The conditional operator evaluates an expression returning a value if that expression is true and a different one if &lt;br /&gt;
the expression is evaluated as false. Its format is:&amp;nbsp; &lt;br /&gt;
condition ? result1 : result2 &lt;br /&gt;
If condition is true the expression will return result1, if it is not it will return result2.&amp;nbsp; &lt;br /&gt;
7==5 ? 4 : 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // returns 3, since 7 is not equal to 5. &lt;br /&gt;
7==5+2 ? 4 : 3&amp;nbsp;&amp;nbsp; // returns 4, since 7 is equal to 5+2. &lt;br /&gt;
5&amp;gt;3 ? a : b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // returns the value of a, since 5 is greater than 3. &lt;br /&gt;
a&amp;gt;b ? a : b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // returns whichever is greater, a or b.&amp;nbsp; &lt;br /&gt;
// conditional operator &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int a,b,c; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; a=2; &lt;br /&gt;
&amp;nbsp; b=7; &lt;br /&gt;
&amp;nbsp; c = (a&amp;gt;b) ? a : b; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; c; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
7 &lt;br /&gt;
In this example a was 2 and b was 7, so the expression being evaluated (a&amp;gt;b) was not true, thus the first value &lt;br /&gt;
specified after the question mark was discarded in favor of the second value (the one after the colon) which was b, with a value of 7.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Comma operator ( , ) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The comma operator (,) is used to separate two or more expressions that are included where only one expression &lt;br /&gt;
is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is &lt;br /&gt;
considered.&amp;nbsp; &lt;br /&gt;
For example, the following code:&amp;nbsp; &lt;br /&gt;
a = (b=3, b+2); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Bitwise Operators ( &amp;amp;, |, ^, ~, &amp;lt;&amp;lt;, &amp;gt;&amp;gt; )&amp;nbsp;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Bitwise operators modify variables considering the bit patterns that represent the values they store.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjx75BIMB2emeTlSw3dXYX0gbNdvWCX6Bw1c82a1d74-k0OKVL0_w50XIPAI2zN3UEnHXgZI3HbHPG5d2P25f-WJOOl54AyPEVnhrmcapxApNZ1BKbqOomyUYconCvR291qV6rf1LAlIkQO/s1600/amityyy.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Explicit type casting operator&amp;nbsp;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Type casting operators allow you to convert a datum of a given type to another. There are several ways to do this &lt;br /&gt;
in C++. The simplest one, which has been inherited from the C language, is to precede the expression to be &lt;br /&gt;
converted by the new type enclosed between parentheses (()):&amp;nbsp; &lt;br /&gt;
int i; &lt;br /&gt;
float f = 3.14; &lt;br /&gt;
i = (int) f; &lt;br /&gt;
The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Here, the &lt;br /&gt;
typecasting operator was (int). Another way to do the same thing in C++ is using the functional notation: &lt;br /&gt;
preceding the expression to be converted by the type and enclosing the expression between parentheses:&amp;nbsp; &lt;br /&gt;
i = int ( f ); &lt;br /&gt;
Both ways of type casting are valid in C++.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;sizeof()&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt; &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object:&amp;nbsp; &lt;br /&gt;
a = sizeof (char); &lt;br /&gt;
This will assign the value 1 to a because char is a one-byte long type. &lt;br /&gt;
The value returned by sizeof is a constant, so it is always determined before program execution.&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Other operators&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt; &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Later in these tutorials, we will see a few more operators, like the ones referring to pointers or the specifics for object-oriented programming. Each one is treated in its respective section.&amp;nbsp; &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Precedence of operators &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;When writing complex expressions with several operands, we may have some doubts about which operand is &lt;br /&gt;
evaluated first and which later. For example, in this expression:&amp;nbsp; &lt;br /&gt;
a = 5 + 7 % 2&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
27 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
we may doubt if it really means:&amp;nbsp; &lt;br /&gt;
a = 5 + (7 % 2)&amp;nbsp;&amp;nbsp;&amp;nbsp; // with a result of 6, or &lt;br /&gt;
a = (5 + 7) % 2&amp;nbsp;&amp;nbsp;&amp;nbsp; // with a result of 0&amp;nbsp; &lt;br /&gt;
The correct answer is the first of the two expressions, with a result of 6. There is an established order with the &lt;br /&gt;
priority of each operator, and not only the arithmetic ones (those whose preference come from mathematics) but for all the operators which can appear in C++. From greatest to lowest priority, the priority order is as follows:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh05O1dmWhVCndL_d7NnEMW5nl_5NjRmgzdahqAm-p0m0R9fHmlQpQRXqvIJkbE1WMo90Ub62wRPPpgm3EKMtqMUjdj6IciQL6-0WMQNtrVG8lRcNcdhVSNKb_PavgXgL_PPFSz6ehfOVsg/s1600/amit%252B%252B2.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Grouping defines the precedence order in which operators are evaluated in the case that there are several &lt;br /&gt;
operators of the same level in an expression.&amp;nbsp; &lt;br /&gt;
All these precedence levels for operators can be manipulated or become more legible by removing possible &lt;br /&gt;
ambiguities using parentheses signs ( and ), as in this example:&amp;nbsp; &lt;br /&gt;
a = 5 + 7 % 2;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;might be written either as:&amp;nbsp; &lt;br /&gt;
a = 5 + (7 % 2); &lt;br /&gt;
or &lt;br /&gt;
a = (5 + 7) % 2; &lt;br /&gt;
depending on the operation that we want to perform.&amp;nbsp; &lt;br /&gt;
So if you want to write complicated expressions and you are not completely sure of the precedence levels, always &lt;br /&gt;
include parentheses. It will also become a code easier to read. &lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/1334600648100760772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/operators_20.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/1334600648100760772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/1334600648100760772'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/operators_20.html' title='Operators'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA0fyroyrBizwfnNAq65b0o3BVu524plV_RrlUMNGjVIOrMReaY1yrLHhIe9sG8j43OqfXLflduzSS4uOwoW2rOyVBarsq3xl3MJGNwwQ_ZM4RrSSlaBOPl6sai-Csg0xNKHAKsacGu37f/s72-c/ye+amit.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-5692997256396513141</id><published>2011-03-18T22:41:00.000-07:00</published><updated>2011-05-27T22:46:43.619-07:00</updated><title type='text'>Polymorphism</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Before getting into this section, it is recommended that you have a proper understanding of pointers and class &lt;br /&gt;
inheritance. If any of the following statements seem strange to you, you should review the indicated sections:&amp;nbsp; &lt;br /&gt;
Statement: &lt;br /&gt;
Explained in: &lt;br /&gt;
int a::b(c) {};&amp;nbsp;&amp;nbsp;&amp;nbsp; Classes &lt;br /&gt;
a-&amp;gt;b&amp;nbsp; Data Structures &lt;br /&gt;
class a: public b; Friendship and inheritance &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Pointers to base class &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to &lt;br /&gt;
its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that &lt;br /&gt;
brings Object Oriented Methodologies to its full potential.&amp;nbsp; &lt;br /&gt;
We are going to start by rewriting our program about the rectangle and the triangle of the previous section taking &lt;br /&gt;
into consideration this pointer compatibility property:&amp;nbsp; &lt;br /&gt;
// pointers to base class &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CPolygon { &lt;br /&gt;
&amp;nbsp; protected: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b; } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CRectangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CTriangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height / 2); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect; &lt;br /&gt;
&amp;nbsp; CTriangle trgl; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly1 = &amp;amp;rect; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly2 = &amp;amp;trgl; &lt;br /&gt;
&amp;nbsp; ppoly1-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly2-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; rect.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; trgl.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
20 &lt;br /&gt;
10 &lt;br /&gt;
In function main, we create two pointers that point to objects of class CPolygon (ppoly1 and ppoly2). Then we &lt;br /&gt;
assign references to rect and trgl to these pointers, and because both are objects of classes derived from &lt;br /&gt;
CPolygon, both are valid assignment operations.&amp;nbsp; &lt;br /&gt;
The only limitation in using *ppoly1 and *ppoly2 instead of rect and trgl is that both *ppoly1 and *ppoly2 are &lt;br /&gt;
of type CPolygon* and therefore we can only use these pointers to refer to the members that CRectangle and CTriangle inherit from CPolygon. For that reason when we call the area() members at the end of the program we &lt;br /&gt;
have had to use directly the objects rect and trgl instead of the pointers *ppoly1 and *ppoly2.&amp;nbsp; &lt;br /&gt;
In order to use area() with the pointers to class CPolygon, this member should also have been declared in the &lt;br /&gt;
class CPolygon, and not only in its derived classes, but the problem is that CRectangle and CTriangle implement &lt;br /&gt;
different versions of area, therefore we cannot implement it in the base class. This is when virtual members &lt;br /&gt;
become handy:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Virtual members &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare &lt;br /&gt;
a member of a class as virtual, we must precede its declaration with the keyword virtual:&amp;nbsp; &lt;br /&gt;
// virtual members &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CPolygon { &lt;br /&gt;
&amp;nbsp; protected: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; virtual int area () &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (0); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CRectangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CTriangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height / 2); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect; &lt;br /&gt;
&amp;nbsp; CTriangle trgl; &lt;br /&gt;
&amp;nbsp; CPolygon poly; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly1 = &amp;amp;rect; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly2 = &amp;amp;trgl; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly3 = &amp;amp;poly; &lt;br /&gt;
&amp;nbsp; ppoly1-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly2-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly3-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; ppoly1-&amp;gt;area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; ppoly2-&amp;gt;area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; ppoly3-&amp;gt;area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
20 &lt;br /&gt;
10 &lt;br /&gt;
0 &lt;br /&gt;
Now the three classes (CPolygon, CRectangle and CTriangle) have all the same members: width, height, &lt;br /&gt;
set_values() and area().&amp;nbsp; &lt;br /&gt;
The member function area() has been declared as virtual in the base class because it is later redefined in each &lt;br /&gt;
derived class. You can verify if you want that if you remove this virtual keyword from the declaration of area() &lt;br /&gt;
within CPolygon, and then you run the program the result will be 0 for the three polygons instead of 20, 10 and 0. &lt;br /&gt;
That is because instead of calling the corresponding area() function for each object (CRectangle::area(), &lt;br /&gt;
CTriangle::area() and CPolygon::area(), respectively), CPolygon::area() will be called in all cases since the &lt;br /&gt;
calls are via a pointer whose type is CPolygon*.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Therefore, what the virtual keyword does is to allow a member of a derived class with the same name as one in &lt;br /&gt;
the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a &lt;br /&gt;
pointer to the base class but is pointing to an object of the derived class, as in the above example.&amp;nbsp; &lt;br /&gt;
A class that declares or inherits a virtual function is called a polymorphic class.&amp;nbsp; &lt;br /&gt;
Note that despite of its virtuality, we have also been able to declare an object of type CPolygon and to call its own &lt;br /&gt;
area() function, which always returns 0.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Abstract base classes &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Abstract base classes are something very similar to our CPolygon class of our previous example. The only &lt;br /&gt;
difference is that in our previous example we have defined a valid area() function with a minimal functionality for &lt;br /&gt;
objects that were of class CPolygon (like the object poly), whereas in an abstract base classes we could leave that &lt;br /&gt;
area() member function without implementation at all. This is done by appending =0 (equal to zero) to the &lt;br /&gt;
function declaration.&amp;nbsp; &lt;br /&gt;
An abstract base CPolygon class could look like this:&amp;nbsp; &lt;br /&gt;
// abstract class CPolygon &lt;br /&gt;
class CPolygon { &lt;br /&gt;
&amp;nbsp; protected: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; virtual int area () =0; &lt;br /&gt;
}; &lt;br /&gt;
Notice how we appended =0 to virtual int area () instead of specifying an implementation for the function. &lt;br /&gt;
This type of function is called a pure virtual function, and all classes that contain at least one pure virtual function &lt;br /&gt;
are abstract base classes.&amp;nbsp; &lt;br /&gt;
The main difference between an abstract base class and a regular polymorphic class is that because in abstract &lt;br /&gt;
base classes at least one of its members lacks implementation we cannot create instances (objects) of it.&amp;nbsp; &lt;br /&gt;
But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take advantage of &lt;br /&gt;
all its polymorphic abilities. Therefore a declaration like:&amp;nbsp; &lt;br /&gt;
CPolygon poly; &lt;br /&gt;
would not be valid for the abstract base class we have just declared, because tries to instantiate an object. &lt;br /&gt;
Nevertheless, the following pointers:&amp;nbsp; &lt;br /&gt;
CPolygon * ppoly1; &lt;br /&gt;
CPolygon * ppoly2; &lt;br /&gt;
would be perfectly valid.&amp;nbsp; &lt;br /&gt;
This is so for as long as CPolygon includes a pure virtual function and therefore it&#39;s an abstract base class. &lt;br /&gt;
However, pointers to this abstract base class can be used to point to objects of derived classes.&amp;nbsp; &lt;br /&gt;
Here you have the complete example:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
110 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// abstract base class &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CPolygon { &lt;br /&gt;
&amp;nbsp; protected: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; virtual int area (void) =0; &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CRectangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CTriangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height / 2); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect; &lt;br /&gt;
&amp;nbsp; CTriangle trgl; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly1 = &amp;amp;rect; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly2 = &amp;amp;trgl; &lt;br /&gt;
&amp;nbsp; ppoly1-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly2-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; ppoly1-&amp;gt;area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; ppoly2-&amp;gt;area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
20 &lt;br /&gt;
10 &lt;br /&gt;
If you review the program you will notice that we refer to objects of different but related classes using a unique &lt;br /&gt;
type of pointer (CPolygon*). This can be tremendously useful. For example, now we can create a function member &lt;br /&gt;
of the abstract base class CPolygon that is able to print on screen the result of the area() function even though &lt;br /&gt;
CPolygon itself has no implementation for this function:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// pure virtual members can be called &lt;br /&gt;
// from the abstract base class &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CPolygon { &lt;br /&gt;
&amp;nbsp; protected: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; virtual int area (void) =0; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void printarea (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { cout &amp;lt;&amp;lt; this-&amp;gt;area() &amp;lt;&amp;lt; endl; } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CRectangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CTriangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height / 2); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect; &lt;br /&gt;
&amp;nbsp; CTriangle trgl; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly1 = &amp;amp;rect; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly2 = &amp;amp;trgl; &lt;br /&gt;
&amp;nbsp; ppoly1-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly2-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly1-&amp;gt;printarea(); &lt;br /&gt;
&amp;nbsp; ppoly2-&amp;gt;printarea(); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
20 &lt;br /&gt;
10 &lt;br /&gt;
Virtual members and abstract classes grant C++ the polymorphic characteristics that make object-oriented &lt;br /&gt;
programming such a useful instrument in big projects. Of course, we have seen very simple uses of these features, &lt;br /&gt;
but these features can be applied to arrays of objects or dynamically allocated objects.&amp;nbsp; &lt;br /&gt;
Let&#39;s end with the same example again, but this time with objects that are dynamically allocated:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// dynamic allocation and polymorphism &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CPolygon { &lt;br /&gt;
&amp;nbsp; protected: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; virtual int area (void) =0; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void printarea (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { cout &amp;lt;&amp;lt; this-&amp;gt;area() &amp;lt;&amp;lt; endl; } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CRectangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
class CTriangle: public CPolygon { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height / 2); } &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly1 = new CRectangle; &lt;br /&gt;
&amp;nbsp; CPolygon * ppoly2 = new CTriangle; &lt;br /&gt;
&amp;nbsp; ppoly1-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly2-&amp;gt;set_values (4,5); &lt;br /&gt;
&amp;nbsp; ppoly1-&amp;gt;printarea(); &lt;br /&gt;
&amp;nbsp; ppoly2-&amp;gt;printarea(); &lt;br /&gt;
&amp;nbsp; delete ppoly1; &lt;br /&gt;
&amp;nbsp; delete ppoly2; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
20 &lt;br /&gt;
10 &lt;br /&gt;
Notice that the ppoly pointers:&amp;nbsp; &lt;br /&gt;
CPolygon * ppoly1 = new CRectangle; &lt;br /&gt;
CPolygon * ppoly2 = new CTriangle; &lt;br /&gt;
are declared being of type pointer to CPolygon but the objects dynamically allocated have been declared having &lt;br /&gt;
the derived class type directly. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;iframe frameborder=&quot;0&quot; style=&quot;display:block;&quot; width=&quot;303px&quot; height=&quot;499px&quot; src=&quot;http://www.hotelscombined.com/Affiliate/Widgets/303x499/default.aspx?a_aid=29982&amp;languageCode=EN&amp;openInNewWindow=1&quot; scrolling=&quot;no&quot; allowtransparency=&quot;true&quot; &gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/5692997256396513141/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/polymorphism.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/5692997256396513141'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/5692997256396513141'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/polymorphism.html' title='Polymorphism'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-1167114768101761109</id><published>2011-03-17T01:39:00.000-07:00</published><updated>2011-05-27T01:43:55.207-07:00</updated><title type='text'>Friendship and inheritance</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Friend functions &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In principle, private and protected members of a class cannot be accessed from outside the same class in which &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;they are declared. However, this rule does not affect friends.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Friends are functions or classes declared as such.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;If we want to declare an external function as friend of a class, thus allowing this function to have access to the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;private and protected members of this class, we do it by declaring a prototype of this external function within the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class, and preceding it with the keyword friend:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// friend functions &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CRectangle { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int, int); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () {return (width * height);} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; friend CRectangle duplicate (CRectangle); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;void CRectangle::set_values (int a, int b) { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; width = a; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; height = b; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;CRectangle duplicate (CRectangle rectparam) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CRectangle rectres; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rectres.width = rectparam.width*2; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rectres.height = rectparam.height*2; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return (rectres); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CRectangle rect, rectb; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rect.set_values (2,3); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rectb = duplicate (rect); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; rectb.area(); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;24&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The duplicate function is a friend of CRectangle. From within that function we have been able to access the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;members width and height of different objects of type CRectangle, which are private members. Notice that &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;of class CRectangle. It isn&#39;t! It simply has access to its private and protected members without being a member.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The friend functions can serve, for example, to conduct operations between two different classes. Generally, the use of friend functions is out of an object-oriented programming methodology, so whenever possible it is better to use members of the same class to perform operations with them. Such as in the previous example, it would have been shorter to integrate duplicate() within the class CRectangle.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Friend classes &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Just as we have the possibility to define a friend function, we can also define a class as friend of another one, &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;granting that first class access to the protected and private members of the second one.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// friend class &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CSquare; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CRectangle { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {return (width * height);} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void convert (CSquare a); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CSquare { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; private: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int side; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_side (int a) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {side=a;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; friend class CRectangle; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;void CRectangle::convert (CSquare a) { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; width = a.side; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; height = a.side; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CSquare sqr; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CRectangle rect; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; sqr.set_side(4); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rect.convert(sqr); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; rect.area(); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;16 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In this example, we have declared CRectangle as a friend of CSquare so that CRectangle member functions could have access to the protected and private members of CSquare, more concretely to CSquare::side, which describes the side width of the square.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;You may also see something new at the beginning of the program: an empty declaration of class CSquare. This is necessary because within the declaration of CRectangle we refer to CSquare (as a parameter in convert()). The definition of CSquare is included later, so if we did not include a previous empty declaration for CSquare this class would not be visible from within the definition of CRectangle.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Consider that friendships are not corresponded if we do not explicitly specify so. In our example, CRectangle is considered as a friend class by CSquare, but CRectangle does not consider CSquare to be a friend, so CRectangle can access the protected and private members of CSquare but not the reverse way. Of course, we could have declared also CSquare as friend of CRectangle if we wanted to.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Another property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Inheritance between classes &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A key feature of C++ classes is inheritance. Inheritance allows to create classes which are derived from other &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;classes, so that they automatically include some of its &quot;parent&#39;s&quot; members, plus its own. For example, we are going to suppose that we want to declare a series of classes that describe polygons like our CRectangle, or like CTriangle. They have certain common properties, such as both can be described by means of only two sides: height and base.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This could be represented in the world of classes with a class CPolygon from which we would derive the two other ones: CRectangle and CTriangle.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTT8ORLGv2d1UYITaOKvCdtWLdl1jZQpzpmYE9pzWh4-Xs0iCQdgXs4GUYtDIqCVk6SI5JvyBDNvEJ1jbNGD8ajUHpwEFcoqICQ3BE_gtlXbAxd_l6Z3l1HE-2P0NlH6kRa-Ww3nB8RcMc/s1600/amitcll.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;The class CPolygon would contain members that are common for both types of polygon. In our case: width and &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;height. And CRectangle and CTriangle would be its derived classes, with specific features that are different from &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;one type of polygon to the other.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Classes that are derived from others inherit all the accessible members of the base class. That means that if a base &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class includes a member A and we derive it to another class with another member called B, the derived class will contain both members A and B.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;following format:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class derived_class_name: public base_class_name &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ /*...*/ }; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Where derived_class_name is the name of the derived class and base_class_name is the name of the class on &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;which it is based. The public access specifier may be replaced by any one of the other access specifiers protected and private. This access specifier describes the minimum access level for the members that are inherited from the base class.&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;// derived classes &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CPolygon { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; protected: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; }; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CRectangle: public CPolygon { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height); } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; }; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CTriangle: public CPolygon { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height / 2); } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; }; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CRectangle rect; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CTriangle trgl; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rect.set_values (4,5); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; trgl.set_values (4,5); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; rect.area() &amp;lt;&amp;lt; endl; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; trgl.area() &amp;lt;&amp;lt; endl; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;20 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;10 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The objects of the classes CRectangle and CTriangle each contain members inherited from CPolygon. These are: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;width, height and set_values().&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The protected access specifier is similar to private. Its only difference occurs in fact with inheritance. When a &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class inherits from another one, the members of the derived class can access the protected members inherited &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;from the base class, but not its private members.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Since we wanted width and height to be accessible from members of the derived classes CRectangle and &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;CTriangle and not only by members of CPolygon, we have used protected access instead of private.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We can summarize the different access types according to who can access them in the following way:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Access &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;public &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; protected &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; private &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;members of the same class yes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yes &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;members of derived classes yes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; no &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;not members&amp;nbsp; yes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; no&amp;nbsp; no&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Where &quot;not members&quot; represent any access from outside the class, such as from main(), from another class or &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;from a function.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In our example, the members inherited by CRectangle and CTriangle have the same access permissions as they had in their base class CPolygon:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;CPolygon::width&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // protected access &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;CRectangle::width&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // protected access &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;CPolygon::set_values()&amp;nbsp;&amp;nbsp;&amp;nbsp; // public access &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;CRectangle::set_values()&amp;nbsp; // public access&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This is because we have used the public keyword to define the inheritance relationship on each of the derived &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;classes:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CRectangle: public CPolygon { ... } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This public keyword after the colon (:) denotes the maximum access level for all the members inherited from the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class that follows it (in this case CPolygon). Since public is the most accessible level, by specifying this keyword &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;the derived class will inherit all the members with the same levels they had in the base class.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;If we specify a more restrictive access level like protected, all public members of the base class are inherited as &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;protected in the derived class. Whereas if we specify the most restricting of all access levels: private, all the base &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class members are inherited as private.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;For example, if daughter was a class derived from mother that we defined as:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class daughter: protected mother; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This would set protected as the maximum access level for the members of daughter that it inherited from &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;mother. That is, all members that were public in mother would become protected in daughter. Of course, this &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;would not restrict daughter to declare its own public members. That maximum access level is only set for the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;members inherited from mother.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;If we do not explicitly specify any access level for the inheritance, the compiler assumes private for classes &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;declared with class keyword and public for those declared with struct.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;What is inherited from the base class? &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In principle, a derived class inherits every member of a base class except:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; its constructor and its destructor&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; its operator=() members&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; its friends&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Although the constructors and destructors of the base class are not inherited themselves, its default constructor &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;(i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;is created or destroyed.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;If the base class has no default constructor or you want that an overloaded constructor is called when a new &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;derived object is created, you can specify it in each constructor definition of the derived class:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;derived_constructor_name (parameters) : base_constructor_name (parameters) {...} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;For example:&amp;nbsp; // constructors and derived classes &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class mother { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mother () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { cout &amp;lt;&amp;lt; &quot;mother: no parameters\n&quot;; } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mother (int a) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { cout &amp;lt;&amp;lt; &quot;mother: int parameter\n&quot;; } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class daughter : public mother { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; daughter (int a) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { cout &amp;lt;&amp;lt; &quot;daughter: int parameter\n\n&quot;; } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class son : public mother { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; son (int a) : mother (a) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { cout &amp;lt;&amp;lt; &quot;son: int parameter\n\n&quot;; } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; daughter cynthia (0); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; son daniel(0); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;mother: no parameters &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;daughter: int parameter &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;mother: int parameter &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;son: int parameter &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Notice the difference between which mother&#39;s constructor is called when a new daughter object is created and &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;which when it is a son object. The difference is because the constructor declaration of daughter and son:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;daughter (int a)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // nothing specified: call default &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;son (int a) : mother (a)&amp;nbsp; // constructor specified: call this&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Multiple inheritance &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In C++ it is perfectly possible that a class inherits members from more than one class. This is done by simply &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;separating the different base classes with commas in the derived class declaration. For example, if we had a &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;specific class to print on screen (COutput) and we wanted our classes CRectangle and CTriangle to also inherit its &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;members in addition to those of CPolygon we could write:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CRectangle: public CPolygon, public COutput; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CTriangle: public CPolygon, public COutput;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;here is the complete example:&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// multiple inheritance &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CPolygon { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; protected: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int a, int b) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { width=a; height=b;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; }; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class COutput { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void output (int i); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; }; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;void COutput::output (int i) { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CRectangle: public CPolygon, public COutput { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height); } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; }; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CTriangle: public CPolygon, public COutput { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; public: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return (width * height / 2); } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; }; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CRectangle rect; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; CTriangle trgl; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rect.set_values (4,5); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; trgl.set_values (4,5); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; rect.output (rect.area()); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; trgl.output (trgl.area()); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;20 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;10&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/1167114768101761109/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/05/friendship-and-inheritance.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/1167114768101761109'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/1167114768101761109'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/05/friendship-and-inheritance.html' title='Friendship and inheritance'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTT8ORLGv2d1UYITaOKvCdtWLdl1jZQpzpmYE9pzWh4-Xs0iCQdgXs4GUYtDIqCVk6SI5JvyBDNvEJ1jbNGD8ajUHpwEFcoqICQ3BE_gtlXbAxd_l6Z3l1HE-2P0NlH6kRa-Ww3nB8RcMc/s72-c/amitcll.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-2117124075831518686</id><published>2011-03-16T01:21:00.000-07:00</published><updated>2011-05-27T01:35:22.764-07:00</updated><title type='text'>Classes (II)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Overloading operators &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;C++ incorporates the option to use standard operators to perform operations with classes in addition to with &lt;br /&gt;
fundamental types. For example:&amp;nbsp; &lt;br /&gt;
int a, b, c; &lt;br /&gt;
a = b + c; &lt;br /&gt;
This is obviously valid code in C++, since the different variables of the addition are all fundamental types. &lt;br /&gt;
Nevertheless, it is not so obvious that we could perform an operation similar to the following one:&amp;nbsp; &lt;br /&gt;
struct { &lt;br /&gt;
&amp;nbsp; string product; &lt;br /&gt;
&amp;nbsp; float price; &lt;br /&gt;
} a, b, c; &lt;br /&gt;
a = b + c;&amp;nbsp; &lt;br /&gt;
In fact, this will cause a compilation error, since we have not defined the behavior our class should have with &lt;br /&gt;
addition operations. However, thanks to the C++ feature to overload operators, we can design classes able to perform operations using standard operators. Here is a list of all the operators that can be overloaded:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Overloadable operators &lt;br /&gt;
+&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;nbsp;&amp;nbsp;&amp;nbsp; *&amp;nbsp;&amp;nbsp;&amp;nbsp; /&amp;nbsp;&amp;nbsp;&amp;nbsp; =&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; +=&amp;nbsp;&amp;nbsp; -=&amp;nbsp;&amp;nbsp; *=&amp;nbsp;&amp;nbsp; /=&amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt;&amp;nbsp;&amp;nbsp; &amp;gt;&amp;gt; &lt;br /&gt;
&amp;lt;&amp;lt;=&amp;nbsp; &amp;gt;&amp;gt;=&amp;nbsp; ==&amp;nbsp;&amp;nbsp; !=&amp;nbsp;&amp;nbsp; &amp;lt;=&amp;nbsp;&amp;nbsp; &amp;gt;=&amp;nbsp;&amp;nbsp; ++&amp;nbsp;&amp;nbsp; --&amp;nbsp;&amp;nbsp; %&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ^&amp;nbsp;&amp;nbsp;&amp;nbsp; !&amp;nbsp;&amp;nbsp;&amp;nbsp; | &lt;br /&gt;
~&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;=&amp;nbsp;&amp;nbsp; ^=&amp;nbsp;&amp;nbsp; |=&amp;nbsp;&amp;nbsp; &amp;amp;&amp;amp;&amp;nbsp;&amp;nbsp; ||&amp;nbsp;&amp;nbsp; %=&amp;nbsp;&amp;nbsp; []&amp;nbsp;&amp;nbsp; ()&amp;nbsp;&amp;nbsp; ,&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;gt;*&amp;nbsp; -&amp;gt;&amp;nbsp;&amp;nbsp; new &lt;br /&gt;
delete&amp;nbsp;&amp;nbsp;&amp;nbsp; new[]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; delete[]&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;To overload an operator in order to use it with classes we declare operator functions, which are regular functions &lt;br /&gt;
whose names are the operator keyword followed by the operator sign that we want to overload. The format is:&amp;nbsp; &lt;br /&gt;
type operator sign (parameters) { /*...*/ } &lt;br /&gt;
Here you have an example that overloads the addition operator (+). We are going to create a class to store &lt;br /&gt;
bidimensional vectors and then we are going to add two of them: a(3,1) and b(1,2). The addition of two &lt;br /&gt;
bidimensional vectors is an operation as simple as adding the two x coordinates to obtain the resulting x coordinate and adding the two y coordinates to obtain the resulting y. In this case the result will be (3+1,1+2) = (4,3).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// vectors: overloading operators example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CVector { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int x,y; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CVector () {}; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CVector (int,int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CVector operator + (CVector); &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
CVector::CVector (int a, int b) { &lt;br /&gt;
&amp;nbsp; x = a; &lt;br /&gt;
&amp;nbsp; y = b; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
CVector CVector::operator+ (CVector param) { &lt;br /&gt;
&amp;nbsp; CVector temp; &lt;br /&gt;
&amp;nbsp; temp.x = x + param.x; &lt;br /&gt;
&amp;nbsp; temp.y = y + param.y; &lt;br /&gt;
&amp;nbsp; return (temp); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CVector a (3,1); &lt;br /&gt;
&amp;nbsp; CVector b (1,2); &lt;br /&gt;
&amp;nbsp; CVector c; &lt;br /&gt;
&amp;nbsp; c = a + b; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; c.x &amp;lt;&amp;lt; &quot;,&quot; &amp;lt;&amp;lt; c.y; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
4,3 &lt;br /&gt;
It may be a little confusing to see so many times the CVector identifier. But, consider that some of them refer to &lt;br /&gt;
the class name (type) CVector and some others are functions with that name (constructors must have the same &lt;br /&gt;
name as the class). Do not confuse them:&amp;nbsp; &lt;br /&gt;
CVector (int, int);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // function name CVector (constructor) &lt;br /&gt;
CVector operator+ (CVector);&amp;nbsp;&amp;nbsp; // function returns a CVector&amp;nbsp; &lt;br /&gt;
The function operator+ of class CVector is the one that is in charge of overloading the addition operator (+). This &lt;br /&gt;
function can be called either implicitly using the operator, or explicitly using the function name:&amp;nbsp; &lt;br /&gt;
c = a + b; &lt;br /&gt;
c = a.operator+ (b); &lt;br /&gt;
Both expressions are equivalent.&amp;nbsp; &lt;br /&gt;
Notice also that we have included the empty constructor (without parameters) and we have defined it with an &lt;br /&gt;
empty block:&amp;nbsp; &lt;br /&gt;
CVector () { }; &lt;br /&gt;
This is necessary, since we have explicitly declared another constructor:&amp;nbsp; &lt;br /&gt;
CVector (int, int); &lt;br /&gt;
And when we explicitly declare any constructor, with any number of parameters, the default constructor with no &lt;br /&gt;
parameters that the compiler can declare automatically is not declared, so we need to declare it ourselves in order to be able to construct objects of this type without parameters. Otherwise, the declaration:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;CVector c; &lt;br /&gt;
included in main() would not have been valid.&amp;nbsp; &lt;br /&gt;
Anyway, I have to warn you that an empty block is a bad implementation for a constructor, since it does not fulfill &lt;br /&gt;
the minimum functionality that is generally expected from a constructor, which is the initialization of all the &lt;br /&gt;
member variables in its class. In our case this constructor leaves the variables x and y undefined. Therefore, a &lt;br /&gt;
more advisable definition would have been something similar to this:&amp;nbsp; &lt;br /&gt;
CVector () { x=0; y=0; }; &lt;br /&gt;
which in order to simplify and show only the point of the code I have not included in the example.&amp;nbsp; &lt;br /&gt;
As well as a class includes a default constructor and a copy constructor even if they are not declared, it also &lt;br /&gt;
includes a default definition for the assignment operator (=) with the class itself as parameter. The behavior which &lt;br /&gt;
is defined by default is to copy the whole content of the data members of the object passed as argument (the one &lt;br /&gt;
at the right side of the sign) to the one at the left side:&amp;nbsp; &lt;br /&gt;
CVector d (2,3); &lt;br /&gt;
CVector e; &lt;br /&gt;
e = d;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // copy assignment operator &lt;br /&gt;
The copy assignment operator function is the only operator member function implemented by default. Of course, &lt;br /&gt;
you can redefine it to any other functionality that you want, like for example, copy only certain class members or &lt;br /&gt;
perform additional initialization procedures.&amp;nbsp; &lt;br /&gt;
The overload of operators does not force its operation to bear a relation to the mathematical or usual meaning of &lt;br /&gt;
the operator, although it is recommended. For example, the code may not be very intuitive if you use operator + &lt;br /&gt;
to subtract two classes or operator== to fill with zeros a class, although it is perfectly possible to do so.&amp;nbsp; &lt;br /&gt;
Although the prototype of a function operator+ can seem obvious since it takes what is at the right side of the &lt;br /&gt;
operator as the parameter for the operator member function of the object at its left side, other operators may not &lt;br /&gt;
be so obvious. Here you have a table with a summary on how the different operator functions have to be declared &lt;br /&gt;
(replace @ by the operator in each case):&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimgIPvwr4xBQZ35zuNPS9iN7evHA0Lvk9rY_CEHz5Cas-1xadyuDCjgJybLH3B2BsxCbsQbQXV0LER1fjqkCtpoFH7QCREX7-UlZSkAwOP6QvbtYe4LsbLB9baoSU1g5ml1fdgNjYYoM4c/s1600/C%252B%252B+operators+amit.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;66&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimgIPvwr4xBQZ35zuNPS9iN7evHA0Lvk9rY_CEHz5Cas-1xadyuDCjgJybLH3B2BsxCbsQbQXV0LER1fjqkCtpoFH7QCREX7-UlZSkAwOP6QvbtYe4LsbLB9baoSU1g5ml1fdgNjYYoM4c/s320/C%252B%252B+operators+amit.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Where a is an object of class A, b is an object of class B and c is an object of class C. &lt;br /&gt;
You can see in this panel that there are two ways to overload some class operators: as a member function and as a global function. Its use is indistinct, nevertheless I remind you that functions that are not members of a class cannot access the private or protected members of that class unless the global function is its friend (friendship is explained later).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;The keyword this &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;One of its uses can be to check if a parameter passed to a member function is the object itself. For example,&amp;nbsp; &lt;br /&gt;
// this &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CDummy { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int isitme (CDummy&amp;amp; param); &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
int CDummy::isitme (CDummy&amp;amp; param) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; if (&amp;amp;param == this) return true; &lt;br /&gt;
&amp;nbsp; else return false; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CDummy a; &lt;br /&gt;
&amp;nbsp; CDummy* b = &amp;amp;a; &lt;br /&gt;
&amp;nbsp; if ( b-&amp;gt;isitme(a) ) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;yes, &amp;amp;a is b&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
yes, &amp;amp;a is b &lt;br /&gt;
It is also frequently used in operator= member functions that return objects by reference (avoiding the use of &lt;br /&gt;
temporary objects). Following with the vector&#39;s examples seen before we could have written an operator= function &lt;br /&gt;
similar to this one:&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; &lt;br /&gt;
CVector&amp;amp; CVector::operator= (const CVector&amp;amp; param) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; x=param.x; &lt;br /&gt;
&amp;nbsp; y=param.y; &lt;br /&gt;
&amp;nbsp; return *this; &lt;br /&gt;
}&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;br /&gt;
In fact this function is very similar to the code that the compiler generates implicitly for this class if we do not &lt;br /&gt;
include an operator= member function to copy objects of this class.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Static members &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A class can contain static members, either data or functions.&amp;nbsp; &lt;br /&gt;
Static data members of a class are also known as &quot;class variables&quot;, because there is only one unique value for all the objects of that same class. Their content is not different from one object of this class to another.&amp;nbsp; &lt;br /&gt;
For example, it may be used for a variable within a class that can contain a counter with the number of objects of that class that are currently allocated, as in the following example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// static members in classes &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CDummy { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; static int n; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CDummy () { n++; }; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ~CDummy () { n--; }; &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
int CDummy::n=0; &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CDummy a; &lt;br /&gt;
&amp;nbsp; CDummy b[5]; &lt;br /&gt;
&amp;nbsp; CDummy * c = new CDummy; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; a.n &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; delete c; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; CDummy::n &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
7 &lt;br /&gt;
6 &lt;br /&gt;
In fact, static members have the same properties as global variables but they enjoy class scope. For that reason, &lt;br /&gt;
and to avoid them to be declared several times, we can only include the prototype (its declaration) in the class &lt;br /&gt;
declaration but not its definition (its initialization). In order to initialize a static data-member we must include a &lt;br /&gt;
formal definition outside the class, in the global scope, as in the previous example:&amp;nbsp; &lt;br /&gt;
int CDummy::n=0; &lt;br /&gt;
Because it is a unique variable value for all the objects of the same class, it can be referred to as a member of any &lt;br /&gt;
object of that class or even directly by the class name (of course this is only valid for static members):&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; a.n; &lt;br /&gt;
cout &amp;lt;&amp;lt; CDummy::n; &lt;br /&gt;
These two calls included in the previous example are referring to the same variable: the static variable n within &lt;br /&gt;
class CDummy shared by all objects of this class.&amp;nbsp; &lt;br /&gt;
Once again, I remind you that in fact it is a global variable. The only difference is its name and possible access &lt;br /&gt;
restrictions outside its class.&amp;nbsp; Just as we may include static data within a class, we can also include static functions. They represent the same: &lt;br /&gt;
they are global functions that are called as if they were object members of a given class. They can only refer to static data, in no case to non-static members of the class, as well as they do not allow the use of the keyword this, since it makes reference to an object pointer and these functions in fact are not members of any object but direct members of the class. &lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/2117124075831518686/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/classes-ii.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/2117124075831518686'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/2117124075831518686'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/classes-ii.html' title='Classes (II)'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimgIPvwr4xBQZ35zuNPS9iN7evHA0Lvk9rY_CEHz5Cas-1xadyuDCjgJybLH3B2BsxCbsQbQXV0LER1fjqkCtpoFH7QCREX7-UlZSkAwOP6QvbtYe4LsbLB9baoSU1g5ml1fdgNjYYoM4c/s72-c/C%252B%252B+operators+amit.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-319865695113834979</id><published>2011-03-15T01:05:00.000-07:00</published><updated>2011-05-27T01:21:20.552-07:00</updated><title type='text'>Classes (I)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and &lt;br /&gt;
functions.&amp;nbsp; &lt;br /&gt;
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the &lt;br /&gt;
variable.&amp;nbsp; &lt;br /&gt;
Classes are generally declared using the keyword class, with the following format:&amp;nbsp; &lt;br /&gt;
class class_name { &lt;br /&gt;
&amp;nbsp; access_specifier_1: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; member1; &lt;br /&gt;
&amp;nbsp; access_specifier_2: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; member2; &lt;br /&gt;
&amp;nbsp; ... &lt;br /&gt;
&amp;nbsp; } object_names; &lt;br /&gt;
Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this &lt;br /&gt;
class. The body of the declaration can contain members, that can be either data or function declarations, and &lt;br /&gt;
optionally access specifiers.&amp;nbsp; &lt;br /&gt;
All is very similar to the declaration on data structures, except that we can now include also functions and &lt;br /&gt;
members, but also this new thing called access specifier. An access specifier is one of the following three &lt;br /&gt;
keywords: private, public or protected. These specifiers modify the access rights that the members following &lt;br /&gt;
them acquire:&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private members of a class are accessible only from within other members of the same class or from &lt;br /&gt;
their friends.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected members are accessible from members of their same class and from their friends, but also &lt;br /&gt;
from members of their derived classes.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Finally, public members are accessible from anywhere where the object is visible.&amp;nbsp; &lt;br /&gt;
By default, all members of a class declared with the class keyword have private access for all its members. &lt;br /&gt;
Therefore, any member that is declared before one other class specifier automatically has private access. For &lt;br /&gt;
example:&amp;nbsp; &lt;br /&gt;
class CRectangle { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int x, y; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int,int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void); &lt;br /&gt;
&amp;nbsp; } rect; &lt;br /&gt;
Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect. This class &lt;br /&gt;
contains four members: two data members of type int (member x and member y) with private access (because &lt;br /&gt;
private is the default access level) and two member functions with public access: set_values() and area(), of &lt;br /&gt;
which for now we have only included their declaration, not their definition.&amp;nbsp; &lt;br /&gt;
Notice the difference between the class name and the object name: In the previous example, CRectangle was the class name (i.e., the type), whereas rect was an object of type CRectangle. It is the same relationship int and a have in the following declaration:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int a; &lt;br /&gt;
where int is the type name (the class) and a is the variable name (the object).&amp;nbsp; &lt;br /&gt;
After the previous declarations of CRectangle and rect, we can refer within the body of the program to any of the &lt;br /&gt;
public members of the object rect as if they were normal functions or normal variables, just by putting the object&#39;s &lt;br /&gt;
name followed by a dot (.) and then the name of the member. All very similar to what we did with plain data &lt;br /&gt;
structures before. For example:&amp;nbsp; &lt;br /&gt;
rect.set_values (3,4); &lt;br /&gt;
myarea = rect.area();&amp;nbsp; &lt;br /&gt;
The only members of rect that we cannot access from the body of our program outside the class are x and y, since &lt;br /&gt;
they have private access and they can only be referred from within other members of that same class.&amp;nbsp; &lt;br /&gt;
Here is the complete example of class CRectangle:&amp;nbsp; &lt;br /&gt;
// classes example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CRectangle { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int x, y; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int,int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () {return (x*y);} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) { &lt;br /&gt;
&amp;nbsp; x = a; &lt;br /&gt;
&amp;nbsp; y = b; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect; &lt;br /&gt;
&amp;nbsp; rect.set_values (3,4); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;area: &quot; &amp;lt;&amp;lt; rect.area(); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
area: 12 &lt;br /&gt;
The most important new thing in this code is the operator of scope (::, two colons) included in the definition of &lt;br /&gt;
set_values(). It is used to define a member of a class from outside the class definition itself.&amp;nbsp; &lt;br /&gt;
You may notice that the definition of the member function area() has been included directly within the definition of &lt;br /&gt;
the CRectangle class given its extreme simplicity, whereas set_values() has only its prototype declared within &lt;br /&gt;
the class, but its definition is outside it. In this outside declaration, we must use the operator of scope (::) to &lt;br /&gt;
specify that we are defining a function that is a member of the class CRectangle and not a regular global function.&amp;nbsp; &lt;br /&gt;
The scope operator (::) specifies the class to which the member being declared belongs, granting exactly the same &lt;br /&gt;
scope properties as if this function definition was directly included within the class definition. For example, in the &lt;br /&gt;
function set_values() of the previous code, we have been able to use the variables x and y, which are private &lt;br /&gt;
members of class CRectangle, which means they are only accessible from other members of their class. &lt;br /&gt;
&lt;br /&gt;
The only difference between defining a class member function completely within its class or to include only the &lt;br /&gt;
prototype and later its definition, is that in the first case the function will automatically be considered an inline &lt;br /&gt;
member function by the compiler, while in the second it will be a normal (not-inline) class member function, which &lt;br /&gt;
in fact supposes no difference in behavior.&amp;nbsp; &lt;br /&gt;
Members x and y have private access (remember that if nothing else is said, all members of a class defined with &lt;br /&gt;
keyword class have private access). By declaring them private we deny access to them from anywhere outside the class. This makes sense, since we have already defined a member function to set values for those members within &lt;br /&gt;
the object: the member function set_values(). Therefore, the rest of the program does not need to have direct &lt;br /&gt;
access to them. Perhaps in a so simple example as this, it is difficult to see an utility in protecting those two &lt;br /&gt;
variables, but in greater projects it may be very important that values cannot be modified in an unexpected way &lt;br /&gt;
(unexpected from the point of view of the object).&amp;nbsp; &lt;br /&gt;
One of the greater advantages of a class is that, as any other type, we can declare several objects of it. For &lt;br /&gt;
example, following with the previous example of class CRectangle, we could have declared the object rectb in &lt;br /&gt;
addition to the object rect:&amp;nbsp; &lt;br /&gt;
// example: one class, two objects &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CRectangle { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int x, y; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int,int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () {return (x*y);} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) { &lt;br /&gt;
&amp;nbsp; x = a; &lt;br /&gt;
&amp;nbsp; y = b; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect, rectb; &lt;br /&gt;
&amp;nbsp; rect.set_values (3,4); &lt;br /&gt;
&amp;nbsp; rectb.set_values (5,6); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rect area: &quot; &amp;lt;&amp;lt; rect.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rectb area: &quot; &amp;lt;&amp;lt; rectb.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
rect area: 12 &lt;br /&gt;
rectb area: 30&amp;nbsp;&amp;nbsp; &lt;br /&gt;
In this concrete case, the class (type of the objects) to which we are talking about is CRectangle, of which there &lt;br /&gt;
are two instances or objects: rect and rectb. Each one of them has its own member variables and member &lt;br /&gt;
functions.&amp;nbsp; &lt;br /&gt;
Notice that the call to rect.area() does not give the same result as the call to rectb.area(). This is because &lt;br /&gt;
each object of class CRectangle has its own variables x and y, as they, in some way, have also their own function members set_value() and area() that each uses its object&#39;s own variables to operate.&amp;nbsp; &lt;br /&gt;
That is the basic concept of object-oriented programming: Data and functions are both members of the object. We no longer use sets of global variables that we pass from one function to another as parameters, but instead we handle objects that have their own data and functions embedded as members. Notice that we have not had to give any parameters in any of the calls to rect.area or rectb.area. Those member functions directly used the data members of their respective objects rect and rectb.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Constructors and destructors&amp;nbsp; &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Objects generally need to initialize variables or assign dynamic memory during their process of creation to become &lt;br /&gt;
operative and to avoid returning unexpected values during their execution. For example, what would happen if in &lt;br /&gt;
the previous example we called the member function area() before having called function set_values()? Probably &lt;br /&gt;
we would have gotten an undetermined result since the members x and y would have never been assigned a value.&amp;nbsp; &lt;br /&gt;
In order to avoid that, a class can include a special function called constructor, which is automatically called &lt;br /&gt;
whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void.&amp;nbsp; &lt;br /&gt;
We are going to implement CRectangle including a constructor:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// example: class constructor &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CRectangle { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CRectangle (int,int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () {return (width*height);} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
CRectangle::CRectangle (int a, int b) { &lt;br /&gt;
&amp;nbsp; width = a; &lt;br /&gt;
&amp;nbsp; height = b; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect (3,4); &lt;br /&gt;
&amp;nbsp; CRectangle rectb (5,6); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rect area: &quot; &amp;lt;&amp;lt; rect.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rectb area: &quot; &amp;lt;&amp;lt; rectb.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
rect area: 12 &lt;br /&gt;
rectb area: 30&amp;nbsp;&amp;nbsp; &lt;br /&gt;
As you can see, the result of this example is identical to the previous one. But now we have removed the member &lt;br /&gt;
function set_values(), and have included instead a constructor that performs a similar action: it initializes the &lt;br /&gt;
values of x and y with the parameters that are passed to it.&amp;nbsp; &lt;br /&gt;
Notice how these arguments are passed to the constructor at the moment at which the objects of this class are &lt;br /&gt;
created:&amp;nbsp; &lt;br /&gt;
CRectangle rect (3,4); &lt;br /&gt;
CRectangle rectb (5,6); &lt;br /&gt;
Constructors cannot be called explicitly as if they were regular member functions. They are only executed when a &lt;br /&gt;
new object of that class is created.&amp;nbsp; &lt;br /&gt;
You can also see how neither the constructor prototype declaration (within the class) nor the latter constructor &lt;br /&gt;
definition include a return value; not even void.&amp;nbsp; &lt;br /&gt;
The destructor fulfills the opposite functionality. It is automatically called when an object is destroyed, either &lt;br /&gt;
because its scope of existence has finished (for example, if it was defined as a local object within a function and the &lt;br /&gt;
function ends) or because it is an object dynamically assigned and it is released using the operator delete.&amp;nbsp; &lt;br /&gt;
The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no &lt;br /&gt;
value.&amp;nbsp; &lt;br /&gt;
The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// example on constructors and destructors &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CRectangle { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int *width, *height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CRectangle (int,int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ~CRectangle (); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area () {return (*width * *height);} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
CRectangle::CRectangle (int a, int b) { &lt;br /&gt;
&amp;nbsp; width = new int; &lt;br /&gt;
&amp;nbsp; height = new int; &lt;br /&gt;
&amp;nbsp; *width = a; &lt;br /&gt;
&amp;nbsp; *height = b; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
CRectangle::~CRectangle () { &lt;br /&gt;
&amp;nbsp; delete width; &lt;br /&gt;
&amp;nbsp; delete height; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect (3,4), rectb (5,6); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rect area: &quot; &amp;lt;&amp;lt; rect.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rectb area: &quot; &amp;lt;&amp;lt; rectb.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
rect area: 12 &lt;br /&gt;
rectb area: 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Overloading Constructors &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Like any other function, a constructor can also be overloaded with more than one function that have the same &lt;br /&gt;
name but different types or number of parameters. Remember that for overloaded functions the compiler will call the one whose parameters match the arguments used in the function call. In the case of constructors, which are automatically called when an object is created, the one executed is the one that matches the arguments passed on the object declaration:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// overloading class constructors &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CRectangle { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CRectangle (); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CRectangle (int,int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) {return (width*height);} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
CRectangle::CRectangle () { &lt;br /&gt;
&amp;nbsp; width = 5; &lt;br /&gt;
&amp;nbsp; height = 5; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
CRectangle::CRectangle (int a, int b) { &lt;br /&gt;
&amp;nbsp; width = a; &lt;br /&gt;
&amp;nbsp; height = b; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle rect (3,4); &lt;br /&gt;
&amp;nbsp; CRectangle rectb; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rect area: &quot; &amp;lt;&amp;lt; rect.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;rectb area: &quot; &amp;lt;&amp;lt; rectb.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
rect area: 12 &lt;br /&gt;
rectb area: 25&amp;nbsp;&amp;nbsp; &lt;br /&gt;
In this case, rectb was declared without any arguments, so it has been initialized with the constructor that has no &lt;br /&gt;
parameters, which initializes both width and height with a value of 5.&amp;nbsp; &lt;br /&gt;
Important: Notice how if we declare a new object and we want to use its default constructor (the one without &lt;br /&gt;
parameters), we do not include parentheses ():&amp;nbsp; &lt;br /&gt;
CRectangle rectb;&amp;nbsp;&amp;nbsp; // right &lt;br /&gt;
CRectangle rectb(); // wrong!&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Default constructor &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;If you do not declare any constructors in a class definition, the compiler assumes the class to have a default &lt;br /&gt;
constructor with no arguments. Therefore, after declaring a class like this one:&amp;nbsp; &lt;br /&gt;
class CExample { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int a,b,c; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void multiply (int n, int m) { a=n; b=m; c=a*b; }; &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
The compiler assumes that CExample has a default constructor, so you can declare objects of this class by simply declaring them without any arguments:&amp;nbsp; &lt;br /&gt;
CExample ex; &lt;br /&gt;
But as soon as you declare your own constructor for a class, the compiler no longer provides an implicit default constructor. So you have to declare all objects of that class according to the constructor prototypes you defined for the class:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;class CExample { &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int a,b,c; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; CExample (int n, int m) { a=n; b=m; }; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void multiply () { c=a*b; }; &lt;br /&gt;
&amp;nbsp; }; &lt;br /&gt;
Here we have declared a constructor that takes two parameters of type int. Therefore the following object &lt;br /&gt;
declaration would be correct:&amp;nbsp; &lt;br /&gt;
CExample ex (2,3); &lt;br /&gt;
But, &lt;br /&gt;
CExample ex; &lt;br /&gt;
Would not be correct, since we have declared the class to have an explicit constructor, thus replacing the default &lt;br /&gt;
constructor.&amp;nbsp; &lt;br /&gt;
But the compiler not only creates a default constructor for you if you do not specify your own. It provides three &lt;br /&gt;
special member functions in total that are implicitly declared if you do not declare your own. These are the copy &lt;br /&gt;
constructor, the copy assignment operator, and the default destructor.&amp;nbsp; &lt;br /&gt;
The copy constructor and the copy assignment operator copy all the data contained in another object to the data &lt;br /&gt;
members of the current object. For CExample, the copy constructor implicitly declared by the compiler would be &lt;br /&gt;
something similar to:&amp;nbsp; &lt;br /&gt;
CExample::CExample (const CExample&amp;amp; rv) { &lt;br /&gt;
&amp;nbsp; a=rv.a;&amp;nbsp; b=rv.b;&amp;nbsp; c=rv.c; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
Therefore, the two following object declarations would be correct:&amp;nbsp; &lt;br /&gt;
CExample ex (2,3); &lt;br /&gt;
CExample ex2 (ex);&amp;nbsp;&amp;nbsp; // copy constructor (data copied from ex)&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Pointers to classes&amp;nbsp; &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;It is perfectly valid to create pointers that point to classes. We simply have to consider that once declared, a class &lt;br /&gt;
becomes a valid type, so we can use the class name as the type for the pointer. For example:&amp;nbsp; &lt;br /&gt;
CRectangle * prect; &lt;br /&gt;
is a pointer to an object of class CRectangle.&amp;nbsp; &lt;br /&gt;
As it happened with data structures, in order to refer directly to a member of an object pointed by a pointer we can use the arrow operator (-&amp;gt;) of indirection. Here is an example with some possible combinations:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// pointer to classes example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
class CRectangle { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int width, height; &lt;br /&gt;
&amp;nbsp; public: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; void set_values (int, int); &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int area (void) {return (width * height);} &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
void CRectangle::set_values (int a, int b) { &lt;br /&gt;
&amp;nbsp; width = a; &lt;br /&gt;
&amp;nbsp; height = b; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () { &lt;br /&gt;
&amp;nbsp; CRectangle a, *b, *c; &lt;br /&gt;
&amp;nbsp; CRectangle * d = new CRectangle[2]; &lt;br /&gt;
&amp;nbsp; b= new CRectangle; &lt;br /&gt;
&amp;nbsp; c= &amp;amp;a; &lt;br /&gt;
&amp;nbsp; a.set_values (1,2); &lt;br /&gt;
&amp;nbsp; b-&amp;gt;set_values (3,4); &lt;br /&gt;
&amp;nbsp; d-&amp;gt;set_values (5,6); &lt;br /&gt;
&amp;nbsp; d[1].set_values (7,8); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;a area: &quot; &amp;lt;&amp;lt; a.area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;*b area: &quot; &amp;lt;&amp;lt; b-&amp;gt;area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;*c area: &quot; &amp;lt;&amp;lt; c-&amp;gt;area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;d[0] area: &quot; &amp;lt;&amp;lt; d[0].area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;d[1] area: &quot; &amp;lt;&amp;lt; d[1].area() &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; delete[] d; &lt;br /&gt;
&amp;nbsp; delete b; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
a area: 2 &lt;br /&gt;
*b area: 12 &lt;br /&gt;
*c area: 2 &lt;br /&gt;
d[0] area: 30 &lt;br /&gt;
d[1] area: 56 &lt;br /&gt;
Next you have a summary on how can you read some pointer and class operators (*, &amp;amp;, ., -&amp;gt;, [ ]) that appear in &lt;br /&gt;
the previous example:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaWMhVvvXWQeszZ2_kKg5YrewzUjlMtyj-CHZ9OWchjAJO7qRGZ8K5ONleQgQ1vqN8BJrQ7qU5Ip31K4okJhLIzGtjbxc6Y6ds-sfr5N4Pn7dd8im4-isOsjxvdsF1Jj08zIA-GK26KCD9/s1600/amituuu.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Be sure that you understand the logic under all of these expressions before proceeding with the next sections. If you have doubts, read again this section and/or consult the previous sections about pointers and data structures.&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Classes defined with struct and union &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Classes can be defined not only with keyword class, but also with keywords struct and union.&amp;nbsp; &lt;br /&gt;
The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members). The only difference between both is that members of classes declared with the keyword struct have public access by default, while members of classes declared with the keyword class have private access. For all other purposes both keywords are equivalent.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The concept of unions is different from that of classes declared with struct and class, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold function members. The default access in union classes is public. &lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/319865695113834979/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/classes-i.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/319865695113834979'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/319865695113834979'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/classes-i.html' title='Classes (I)'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaWMhVvvXWQeszZ2_kKg5YrewzUjlMtyj-CHZ9OWchjAJO7qRGZ8K5ONleQgQ1vqN8BJrQ7qU5Ip31K4okJhLIzGtjbxc6Y6ds-sfr5N4Pn7dd8im4-isOsjxvdsF1Jj08zIA-GK26KCD9/s72-c/amituuu.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-4122227972525030999</id><published>2011-03-14T04:39:00.000-07:00</published><updated>2011-05-28T03:57:29.573-07:00</updated><title type='text'>Dynamic Memory</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Until now, in all our programs, we have only had as much memory available as we declared for our variables, &lt;br /&gt;
having the size of all of them to be determined in the source code, before the execution of the program. But, what if we need a variable amount of memory that can only be determined during runtime? For example, in the case that we need some user input to determine the necessary amount of memory space.&amp;nbsp; &lt;br /&gt;
The answer is dynamic memory, for which C++ integrates the operators new and delete.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Operators new and new[] &lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In order to request dynamic memory we use the operator new. new is followed by a data type specifier and -if a sequence of more than one element is required- the number of these within brackets []. It returns a pointer to the beginning of the new block of memory allocated. Its form is:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
pointer = new type &lt;br /&gt;
pointer = new type [number_of_elements]&amp;nbsp; &lt;br /&gt;
The first expression is used to allocate memory to contain one single element of type type. The second one is used to assign a block (an array) of elements of type type, where number_of_elements is an integer value representing the amount of these. For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
int * bobby; &lt;br /&gt;
bobby = new int [5]; &lt;br /&gt;
In this case, the system dynamically assigns space for five elements of type int and returns a pointer to the first &lt;br /&gt;
element of the sequence, which is assigned to bobby. Therefore, now, bobby points to a valid block of memory with &lt;br /&gt;
space for five elements of type int.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The first element pointed by bobby can be accessed either with the expression bobby[0] or the expression *bobby. &lt;br /&gt;
Both are equivalent as has been explained in the section about pointers. The second element can be accessed &lt;br /&gt;
either with bobby[1] or *(bobby+1) and so on...&amp;nbsp; &lt;br /&gt;
You could be wondering the difference between declaring a normal array and assigning dynamic memory to a &lt;br /&gt;
pointer, as we have just done. The most important difference is that the size of an array has to be a constant &lt;br /&gt;
value, which limits its size to what we decide at the moment of designing the program, before its execution, &lt;br /&gt;
whereas the dynamic memory allocation allows us to assign memory during the execution of the program &lt;br /&gt;
(runtime) using any variable or constant value as its size.&amp;nbsp; &lt;br /&gt;
The dynamic memory requested by our program is allocated by the system from the memory heap. However, &lt;br /&gt;
computer memory is a limited resource, and it can be exhausted. Therefore, it is important to have some &lt;br /&gt;
mechanism to check if our request to allocate memory was successful or not.&amp;nbsp; &lt;br /&gt;
C++ provides two standard methods to check if the allocation was successful:&amp;nbsp; &lt;br /&gt;
One is by handling exceptions. Using this method an exception of type bad_alloc is thrown when the allocation &lt;br /&gt;
fails. Exceptions are a powerful C++ feature explained later in these tutorials. But for now you should know that if &lt;br /&gt;
this exception is thrown and it is not handled by a specific handler, the program execution is terminated.&amp;nbsp; This exception method is the default method used by new, and is the one used in a declaration like:&amp;nbsp; &lt;br /&gt;
bobby = new int [5];&amp;nbsp; // if it fails an exception is thrown &lt;br /&gt;
The other method is known as nothrow, and what happens when it is used is that when a memory allocation fails, &lt;br /&gt;
instead of throwing a bad_alloc exception or terminating the program, the pointer returned by new is a null &lt;br /&gt;
pointer, and the program continues its execution.&amp;nbsp; &lt;br /&gt;
This method can be specified by using a special object called nothrow, declared in header &amp;lt;new&amp;gt;, as argument for &lt;br /&gt;
new:&amp;nbsp; &lt;br /&gt;
bobby = new (nothrow) int [5]; &lt;br /&gt;
In this case, if the allocation of this block of memory failed, the failure could be detected by checking if bobby took &lt;br /&gt;
a null pointer value:&amp;nbsp; &lt;br /&gt;
int * bobby; &lt;br /&gt;
bobby = new (nothrow) int [5]; &lt;br /&gt;
if (bobby == 0) { &lt;br /&gt;
&amp;nbsp; // error assigning memory. Take measures. &lt;br /&gt;
&amp;nbsp; };&amp;nbsp; &lt;br /&gt;
This nothrow method requires more work than the exception method, since the value returned has to be checked &lt;br /&gt;
after each and every memory allocation, but I will use it in our examples due to its simplicity. Anyway this method &lt;br /&gt;
can become tedious for larger projects, where the exception method is generally preferred. The exception method &lt;br /&gt;
will be explained in detail later in this tutorial.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Operators delete and delete[] &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Since the necessity of dynamic memory is usually limited to specific moments within a program, once it is no &lt;br /&gt;
longer needed it should be freed so that the memory becomes available again for other requests of dynamic &lt;br /&gt;
memory. This is the purpose of the operator delete, whose format is:&amp;nbsp; &lt;br /&gt;
delete pointer; &lt;br /&gt;
delete [] pointer; &lt;br /&gt;
The first expression should be used to delete memory allocated for a single element, and the second one for &lt;br /&gt;
memory allocated for arrays of elements.&amp;nbsp; &lt;br /&gt;
The value passed as argument to delete must be either a pointer to a memory block previously allocated with new, or a null pointer (in the case of a null pointer, delete produces no effect).&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// rememb-o-matic &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;new&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int i,n; &lt;br /&gt;
&amp;nbsp; int * p; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;How many numbers would you like to type? &quot;; &lt;br /&gt;
&amp;nbsp; cin &amp;gt;&amp;gt; i; &lt;br /&gt;
&amp;nbsp; p= new (nothrow) int[i]; &lt;br /&gt;
&amp;nbsp; if (p == 0) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Error: memory could not be allocated&quot;; &lt;br /&gt;
&amp;nbsp; else &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for (n=0; n&amp;lt;i; n++) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter number: &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cin &amp;gt;&amp;gt; p[n]; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;You have entered: &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for (n=0; n&amp;lt;i; n++) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; p[n] &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; delete[] p; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
How many numbers would you like to type? 5 &lt;br /&gt;
Enter number : 75 &lt;br /&gt;
Enter number : 436 &lt;br /&gt;
Enter number : 1067 &lt;br /&gt;
Enter number : 8 &lt;br /&gt;
Enter number : 32 &lt;br /&gt;
You have entered: 75, 436, 1067, 8, 32, &lt;br /&gt;
Notice how the value within brackets in the new statement is a variable value entered by the user (i), not a &lt;br /&gt;
constant value:&amp;nbsp; &lt;br /&gt;
p= new (nothrow) int[i]; &lt;br /&gt;
But the user could have entered a value for i so big that our system could not handle it. For example, when I tried &lt;br /&gt;
to give a value of 1 billion to the &quot;How many numbers&quot; question, my system could not allocate that much memory &lt;br /&gt;
for the program and I got the text message we prepared for this case (Error: memory could not be allocated). &lt;br /&gt;
Remember that in the case that we tried to allocate the memory without specifying the nothrow parameter in the &lt;br /&gt;
new expression, an exception would be thrown, which if it&#39;s not handled terminates the program.&amp;nbsp; &lt;br /&gt;
It is a good practice to always check if a dynamic memory block was successfully allocated. Therefore, if you use &lt;br /&gt;
the nothrow method, you should always check the value of the pointer returned. Otherwise, use the exception &lt;br /&gt;
method, even if you do not handle the exception. This way, the program will terminate at that point without &lt;br /&gt;
causing the unexpected results of continuing executing a code that assumes a block of memory to have been &lt;br /&gt;
allocated when in fact it has not.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Dynamic memory in ANSI-C &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Operators new and delete are exclusive of C++. They are not available in the C language. But using pure C &lt;br /&gt;
language and its library, dynamic memory can also be used through the functions malloc, calloc, realloc and free, which are also available in C++ including the &amp;lt;cstdlib&amp;gt; header file (see cstdlib for more info).&amp;nbsp; &lt;br /&gt;
The memory blocks allocated by these functions are not necessarily compatible with those returned by new, so each one should be manipulated with its own set of functions or operators. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/4122227972525030999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/05/dynamic-memory.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4122227972525030999'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4122227972525030999'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/05/dynamic-memory.html' title='Dynamic Memory'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-4099400170012709436</id><published>2011-03-14T00:20:00.000-07:00</published><updated>2011-05-27T01:03:01.425-07:00</updated><title type='text'>Other Data Types</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmZUpdojvLIq31hSFZhKnNZvOhetXSknxC0y2kl_Fw4kkkOvD21mqzHJayCahCbIbyf5T5RnX-SFcvcvog0hokNP9Mq2m1NLHTqxnurDajsWi59zoOcxGbMwY4Ieemc54CMjeX8ceHHfiF/s1600/amitkumar.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;101&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmZUpdojvLIq31hSFZhKnNZvOhetXSknxC0y2kl_Fw4kkkOvD21mqzHJayCahCbIbyf5T5RnX-SFcvcvog0hokNP9Mq2m1NLHTqxnurDajsWi59zoOcxGbMwY4Ieemc54CMjeX8ceHHfiF/s320/amitkumar.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Defined data types (typedef) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;C++ allows the definition of our own types based on other existing data types. We can do this using the keyword &lt;br /&gt;
typedef, whose format is:&amp;nbsp; &lt;br /&gt;
typedef existing_type new_type_name ; &lt;br /&gt;
where existing_type is a C++ fundamental or compound type and new_type_name is the name for the new type we are defining. For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
typedef char C; &lt;br /&gt;
typedef unsigned int WORD; &lt;br /&gt;
typedef char * pChar; &lt;br /&gt;
typedef char field [50];&amp;nbsp; &lt;br /&gt;
In this case we have defined four data types: C, WORD, pChar and field as char, unsigned int, char* and &lt;br /&gt;
char[50] respectively, that we could perfectly use in declarations later as any other valid type:&amp;nbsp; &lt;br /&gt;
C mychar, anotherchar, *ptc1; &lt;br /&gt;
WORD myword; &lt;br /&gt;
pChar ptc2; &lt;br /&gt;
field name;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
typedef does not create different types. It only creates synonyms of existing types. That means that the type of &lt;br /&gt;
myword can be considered to be either WORD or unsigned int, since both are in fact the same type.&amp;nbsp; &lt;br /&gt;
typedef can be useful to define an alias for a type that is frequently used within a program. It is also useful to &lt;br /&gt;
define types when it is possible that we will need to change the type in later versions of our program, or if a type you want to use has a name that is too long or confusing.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Unions &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Unions allow one same portion of memory to be accessed as different data types, since all of them are in fact the &lt;br /&gt;
same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally &lt;br /&gt;
different:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
union union_name { &lt;br /&gt;
&amp;nbsp; member_type1 member_name1; &lt;br /&gt;
&amp;nbsp; member_type2 member_name2; &lt;br /&gt;
&amp;nbsp; member_type3 member_name3; &lt;br /&gt;
&amp;nbsp; . &lt;br /&gt;
&amp;nbsp; . &lt;br /&gt;
} object_names; &lt;br /&gt;
All the elements of the union declaration occupy the same physical space in memory. Its size is the one of the &lt;br /&gt;
greatest element of the declaration. For example:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;union mytypes_t { &lt;br /&gt;
&amp;nbsp; char c; &lt;br /&gt;
&amp;nbsp; int i; &lt;br /&gt;
&amp;nbsp; float f; &lt;br /&gt;
&amp;nbsp; } mytypes; &lt;br /&gt;
defines three elements:&amp;nbsp; &lt;br /&gt;
mytypes.c &lt;br /&gt;
mytypes.i &lt;br /&gt;
mytypes.f &lt;br /&gt;
each one with a different data type. Since all of them are referring to the same location in memory, the &lt;br /&gt;
modification of one of the elements will affect the value of all of them. We cannot store different values in them &lt;br /&gt;
independent of each other.&amp;nbsp; &lt;br /&gt;
One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. &lt;br /&gt;
For example:&amp;nbsp; &lt;br /&gt;
union mix_t { &lt;br /&gt;
&amp;nbsp; long l; &lt;br /&gt;
&amp;nbsp; struct { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; short hi; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; short lo; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } s; &lt;br /&gt;
&amp;nbsp; char c[4]; &lt;br /&gt;
} mix; &lt;br /&gt;
defines three names that allow us to access the same group of 4 bytes: mix.l, mix.s and mix.c and which we can &lt;br /&gt;
use according to how we want to access these bytes, as if they were a single long-type data, as if they were two &lt;br /&gt;
short elements or as an array of char elements, respectively. I have mixed types, arrays and structures in the &lt;br /&gt;
union so that you can see the different ways that we can access the data. For a little-endian system (most PC &lt;br /&gt;
platforms), this union could be represented as:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4ezXJK3EXg3SXJFsfRK8fNfp2j6G9gp-3O9qKIFjlnsDNR75YXlPaehe-4c46XUQEf7zReAZCJ2HP90s5YbKSi1jXiqsb8BDAFqt-q8d4EWAz8Xq0AracUgOsaIuJAj27jDUP4mOiSW5x/s1600/amitc%252B%252B%252B.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The exact alignment and order of the members of a union in memory is platform dependant. Therefore be aware of possible portability issues with this type of use.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Anonymous unions &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In C++ we have the option to declare anonymous unions. If we declare a union without any name, the union will be anonymous and we will be able to access its members directly by their member names. For example, look at the difference between these two structure declarations:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmZUpdojvLIq31hSFZhKnNZvOhetXSknxC0y2kl_Fw4kkkOvD21mqzHJayCahCbIbyf5T5RnX-SFcvcvog0hokNP9Mq2m1NLHTqxnurDajsWi59zoOcxGbMwY4Ieemc54CMjeX8ceHHfiF/s1600/amitkumar.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;The only difference between the two pieces of code is that in the first one we have given a name to the union &lt;br /&gt;
(price) and in the second one we have not. The difference is seen when we access the members dollars and &lt;br /&gt;
yens of an object of this type. For an object of the first type, it would be:&amp;nbsp; &lt;br /&gt;
book.price.dollars &lt;br /&gt;
book.price.yens &lt;br /&gt;
whereas for an object of the second type, it would be:&amp;nbsp; &lt;br /&gt;
book.dollars &lt;br /&gt;
book.yens &lt;br /&gt;
Once again I remind you that because it is a union and not a struct, the members dollars and yens occupy the &lt;br /&gt;
same physical space in the memory so they cannot be used to store two different values simultaneously. You can set a value for price in dollars or in yens, but not in both.&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Enumerations (enum) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Enumerations create new data types to contain something different that is not limited to the values fundamental &lt;br /&gt;
data types may take. Its form is the following:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
enum enumeration_name { &lt;br /&gt;
&amp;nbsp; value1, &lt;br /&gt;
&amp;nbsp; value2, &lt;br /&gt;
&amp;nbsp; value3, &lt;br /&gt;
&amp;nbsp; . &lt;br /&gt;
&amp;nbsp; . &lt;br /&gt;
} object_names; &lt;br /&gt;
For example, we could create a new type of variable called color to store colors with the following declaration:&amp;nbsp; &lt;br /&gt;
enum colors_t {black, blue, green, cyan, red, purple, yellow, white}; &lt;br /&gt;
Notice that we do not include any fundamental data type in the declaration. To say it somehow, we have created a &lt;br /&gt;
whole new data type from scratch without basing it on any other existing type. The possible values that variables &lt;br /&gt;
of this new type color_t may take are the new constant values included within braces. For example, once the &lt;br /&gt;
colors_t enumeration is declared the following expressions will be valid:&amp;nbsp; &lt;br /&gt;
colors_t mycolor; &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
mycolor = blue; &lt;br /&gt;
if (mycolor == green) mycolor = red; &lt;br /&gt;
Enumerations are type compatible with numeric variables, so their constants are always assigned an integer &lt;br /&gt;
numerical value internally. If it is not specified, the integer value equivalent to the first possible value is equivalent &lt;br /&gt;
to 0 and the following ones follow a +1 progression. Thus, in our data type colors_t that we have defined above, black would be equivalent to 0, blue would be equivalent to 1, green to 2, and so on.&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;We can explicitly specify an integer value for any of the constant values that our enumerated type can take. If the &lt;br /&gt;
constant value that follows it is not given an integer value, it is automatically assumed the same value as the &lt;br /&gt;
previous one plus one. For example:&amp;nbsp; &lt;br /&gt;
enum months_t { january=1, february, march, april, &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; may, june, july, august, &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; september, october, november, december} y2k; &lt;br /&gt;
In this case, variable y2k of enumerated type months_t can contain any of the 12 possible values that go from &lt;br /&gt;
january to december and that are equivalent to values between 1 and 12 (not between 0 and 11, since we have made january equal to 1).&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/4099400170012709436/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/other-data-types.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4099400170012709436'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4099400170012709436'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/other-data-types.html' title='Other Data Types'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmZUpdojvLIq31hSFZhKnNZvOhetXSknxC0y2kl_Fw4kkkOvD21mqzHJayCahCbIbyf5T5RnX-SFcvcvog0hokNP9Mq2m1NLHTqxnurDajsWi59zoOcxGbMwY4Ieemc54CMjeX8ceHHfiF/s72-c/amitkumar.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-4402104131582128149</id><published>2011-03-13T08:23:00.000-07:00</published><updated>2011-05-27T00:04:50.136-07:00</updated><title type='text'>Data structures</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We have already learned how groups of sequential data can be used in C++. But this is somewhat restrictive, since in many occasions what we want to store are not mere sequences of elements all of the same data type, but sets of different elements with different data types.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Data structures &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A data structure is a group of data elements grouped together under one name. These data elements, known as &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;members, can have different types and different lengths. Data structures are declared in C++ using the following &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;syntax:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;struct structure_name { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;member_type1 member_name1; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;member_type2 member_name2; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;member_type3 member_name3; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} object_names;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;where structure_name is a name for the structure type, object_name can be a set of valid identifiers for objects &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;that have the type of this structure. Within braces { } there is a list with the data members, each one is specified &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;with a type and a valid identifier as its name.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The first thing we have to know is that a data structure creates a new type: Once a data structure is declared, a &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;new type with the identifier specified as structure_name is created and can be used in the rest of the program as &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;if it was any other type. For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;struct product { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int weight; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; float price; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} ; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;product apple; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;product banana, melon; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We have first declared a structure type called product with two members: weight and price, each of a different &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;fundamental type. We have then used this name of the structure type (product) to declare three objects of that &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;type: apple, banana and melon as we would have done with any fundamental data type.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Once declared, product has become a new valid type name like the fundamental ones int, char or short and &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;from that point on we are able to declare objects (variables) of this compound new type, like we have done with &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;apple, banana and melon.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Right at the end of the struct declaration, and before the ending semicolon, we can use the optional field &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;object_name to directly declare objects of the structure type. For example, we can also declare the structure &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;objects apple, banana and melon at the moment we define the data structure type this way:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;struct product { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int weight; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; float price; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} apple, banana, melon; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;It is important to clearly differentiate between what is the structure type name, and what is an object (variable) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;that has this structure type. We can instantiate many objects (i.e. variables, like apple, banana and melon) from a single structure type (product).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Once we have declared our three objects of a determined structure type (apple, banana and melon) we can &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;operate directly with their members. To do that we use a dot (.) inserted between the object name and the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;member name. For example, we could operate with any of these elements as if they were standard variables of &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;their respective types:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;apple.weight &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;apple.price &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;banana.weight &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;banana.price &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;melon.weight &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;melon.price &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Each one of these has the data type corresponding to the member they refer to: apple.weight, banana.weight &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;and melon.weight are of type int, while apple.price, banana.price and melon.price are of type float.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Let&#39;s see a real example where you can see how a structure type can be used in the same way as fundamental &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;types:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// example about structures &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;string&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;sstream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;struct movies_t { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string title; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} mine, yours; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;void printmovie (movies_t movie); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string mystr; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; mine.title = &quot;2001 A Space Odyssey&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; mine.year = 1968; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter title: &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; getline (cin,yours.title); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter year: &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; getline (cin,mystr); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; stringstream(mystr) &amp;gt;&amp;gt; yours.year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;My favorite movie is:\n &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; printmovie (mine); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;And yours is:\n &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; printmovie (yours); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;void printmovie (movies_t movie) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; movie.title; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot; (&quot; &amp;lt;&amp;lt; movie.year &amp;lt;&amp;lt; &quot;)\n&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter title: Alien &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter year: 1979 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;My favorite movie is: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;2001 A Space Odyssey (1968) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;And yours is: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;Alien (1979) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The example shows how we can use the members of an object as regular variables. For example, the member &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;yours.year is a valid variable of type int, and mine.title is a valid variable of type string.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The objects mine and yours can also be treated as valid variables of type movies_t, for example we have passed them to the function printmovie as we would have done with regular variables. Therefore, one of the most important advantages of data structures is that we can either refer to their members individually or to the entire structure as a block with only one identifier.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Data structures are a feature that can be used to represent databases, especially if we consider the possibility of &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;building arrays of them:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// array of structures &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;string&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;sstream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#define N_MOVIES 3 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;struct movies_t { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string title; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} films [N_MOVIES]; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;void printmovie (movies_t movie); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string mystr; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int n; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; for (n=0; n&amp;lt;N_MOVIES; n++) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter title: &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; getline (cin,films[n].title); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter year: &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; getline (cin,mystr); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; stringstream(mystr) &amp;gt;&amp;gt; films[n].year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; } &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;\nYou have entered these movies:\n&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; for (n=0; n&amp;lt;N_MOVIES; n++) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printmovie (films[n]); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;void printmovie (movies_t movie) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; movie.title; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot; (&quot; &amp;lt;&amp;lt; movie.year &amp;lt;&amp;lt; &quot;)\n&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter title: Blade Runner &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter year: 1982 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter title: Matrix &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter year: 1999 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter title: Taxi Driver &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter year: 1976 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;You have entered these movies: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Blade Runner (1982) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Matrix (1999) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Taxi Driver (1976)&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Pointers to structures &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Like any other type, structures can be pointed by its own type of pointers:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;struct movies_t { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string title; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;movies_t amovie; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;movies_t * pmovie; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Here amovie is an object of structure type movies_t, and pmovie is a pointer to point to objects of structure type &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;movies_t. So, the following code would also be valid:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;pmovie = &amp;amp;amovie; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The value of the pointer pmovie would be assigned to a reference to the object amovie (its memory address).&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We will now go with another example that includes pointers, which will serve to introduce a new operator: the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;arrow operator (-&amp;gt;):&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// pointers to structures &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;iostream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;string&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;#include &amp;lt;sstream&amp;gt; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;using namespace std; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;struct movies_t { &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string title; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; int year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;}; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;int main () &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;{ &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; string mystr; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; movies_t amovie; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; movies_t * pmovie; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; pmovie = &amp;amp;amovie; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter title: &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; getline (cin, pmovie-&amp;gt;title); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter year: &quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; getline (cin, mystr); &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; (stringstream) mystr &amp;gt;&amp;gt; pmovie-&amp;gt;year; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;\nYou have entered:\n&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; pmovie-&amp;gt;title; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; cout &amp;lt;&amp;lt; &quot; (&quot; &amp;lt;&amp;lt; pmovie-&amp;gt;year &amp;lt;&amp;lt; &quot;)\n&quot;; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; return 0; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;} &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter title: Invasion of the body snatchers &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Enter year: 1978 &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;You have entered: &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Invasion of the body snatchers (1978) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The previous code includes an important introduction: the arrow operator (-&amp;gt;). This is a dereference operator that &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;is used exclusively with pointers to objects with members. This operator serves to access a member of an object to &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;which we have a reference. In the example we used:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;pmovie-&amp;gt;title &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Which is for all purposes equivalent to:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;(*pmovie).title &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Both expressions pmovie-&amp;gt;title and (*pmovie).title are valid and both mean that we are evaluating the &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;member title of the data structure pointed by a pointer called pmovie. It must be clearly differentiated from:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;*pmovie.title &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;which is equivalent to:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;*(pmovie.title) &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;And that would access the value pointed by a hypothetical pointer member called title of the structure object &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;pmovie (which in this case would not be a pointer). The following panel summarizes possible combinations of &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;pointers and structure members:&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfNFDbGqQ0knKi9BSJVju7jG9_p4plSCMQ4aufT_N6it1sA7bDkYhj_69gJXMn5RzMIJxSQEOci1dhRSaWl5GDTHz1V7XnJedRm5gEdFiYWTtUH6PiIw6CBtu2aehrnj6tU2NsxVmi9ikp/s1600/akhaamit.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Nesting structures&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Structures can also be nested so that a valid element of a structure can also be in its turn another structure.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
struct movies_t { &lt;br /&gt;
&amp;nbsp; string title; &lt;br /&gt;
&amp;nbsp; int year; &lt;br /&gt;
}; &lt;br /&gt;
&lt;br /&gt;
struct friends_t { &lt;br /&gt;
&amp;nbsp; string name; &lt;br /&gt;
&amp;nbsp; string email; &lt;br /&gt;
&amp;nbsp; movies_t favorite_movie; &lt;br /&gt;
&amp;nbsp; } charlie, maria; &lt;br /&gt;
&lt;br /&gt;
friends_t * pfriends = &amp;amp;charlie; &lt;br /&gt;
After the previous declaration we could use any of the following expressions:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
charlie.name &lt;br /&gt;
maria.favorite_movie.title &lt;br /&gt;
charlie.favorite_movie.year &lt;br /&gt;
pfriends-&amp;gt;favorite_movie.year &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
(where, by the way, the last two expressions refer to the same member).&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/4402104131582128149/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/05/data-structures.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4402104131582128149'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/4402104131582128149'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/05/data-structures.html' title='Data structures'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfNFDbGqQ0knKi9BSJVju7jG9_p4plSCMQ4aufT_N6it1sA7bDkYhj_69gJXMn5RzMIJxSQEOci1dhRSaWl5GDTHz1V7XnJedRm5gEdFiYWTtUH6PiIw6CBtu2aehrnj6tU2NsxVmi9ikp/s72-c/akhaamit.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-3977766863934624604</id><published>2011-03-13T04:34:00.000-07:00</published><updated>2011-05-26T04:48:30.436-07:00</updated><title type='text'>Pointers</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This &lt;br /&gt;
way we did not have to care about the physical location of our data within memory, we simply used its identifier &lt;br /&gt;
whenever we wanted to refer to our variable.&amp;nbsp; &lt;br /&gt;
The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that &lt;br /&gt;
computers manage (one byte). These single-byte memory cells are numbered in a consecutive way, so as, within &lt;br /&gt;
any block of memory, every cell has the same number as the previous one plus one.&amp;nbsp; &lt;br /&gt;
This way, each cell can be easily located in the memory because it has a unique address and all the memory cells &lt;br /&gt;
follow a successive pattern. For example, if we are looking for cell 1776 we know that it is going to be right &lt;br /&gt;
between cells 1775 and 1777, exactly one thousand cells after 776 and exactly one thousand cells before cell 2776.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Reference operator (&amp;amp;) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory &lt;br /&gt;
(its memory address). We generally do not actively decide the exact location of the variable within the panel of &lt;br /&gt;
cells that we have imagined the memory to be - Fortunately, that is a task automatically performed by the &lt;br /&gt;
operating system during runtime. However, in some cases we may be interested in knowing the address where our &lt;br /&gt;
variable is being stored during runtime in order to operate with relative positions to it. &lt;br /&gt;
&lt;br /&gt;
The address that locates a variable within memory is what we call a reference to that variable. This reference to a &lt;br /&gt;
variable can be obtained by preceding the identifier of a variable with an ampersand sign (&amp;amp;), known as reference &lt;br /&gt;
operator, and which can be literally translated as &quot;address of&quot;. For example:&amp;nbsp; &lt;br /&gt;
ted = &amp;amp;andy; &lt;br /&gt;
This would assign to ted the address of variable andy, since when preceding the name of the variable andy with &lt;br /&gt;
the reference operator (&amp;amp;) we are no longer talking about the content of the variable itself, but about its reference &lt;br /&gt;
(i.e., its address in memory).&amp;nbsp; &lt;br /&gt;
From now on we are going to assume that andy is placed during runtime in the memory address 1776. This number &lt;br /&gt;
(1776) is just an arbitrary assumption we are inventing right now in order to help clarify some concepts in this &lt;br /&gt;
tutorial, but in reality, we cannot know before runtime the real value the address of a variable will have in memory.&amp;nbsp; &lt;br /&gt;
Consider the following code fragment:&amp;nbsp; &lt;br /&gt;
andy = 25; &lt;br /&gt;
fred = andy; &lt;br /&gt;
ted = &amp;amp;andy; &lt;br /&gt;
The values contained in each variable after the execution of this, are shown in the following diagram:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguUTsheET7fEm6IwOgXRDaRek0ZMFYWDJDAoE4a_55hMfPbE_VRlldme0ZNjQOm8f2AIuibmrpab50q8kdK9FCvPTdxLTi2LOnav-wTDjHFy3qTEvH6xGgDpSdDj3TRrdK8QHyqzkJq0Bc/s1600/amituiip.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;First, we have assigned the value 25 to andy (a variable whose address in memory we have assumed to be 1776).&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
The second statement copied to fred the content of variable andy (which is 25). This is a standard assignment &lt;br /&gt;
operation, as we have done so many times before. &lt;br /&gt;
Finally, the third statement copies to ted not the value contained in andy but a reference to it (i.e., its address, &lt;br /&gt;
which we have assumed to be 1776). The reason is that in this third assignment operation we have preceded the identifier andy with the reference operator (&amp;amp;), so we were no longer referring to the value of andy but to its reference (its address in memory).&amp;nbsp; The variable that stores the reference to another variable (like ted in the previous example) is what we call a pointer. Pointers are a very powerful feature of the C++ language that has many uses in advanced programming. Farther ahead, we will see how this type of variable is used and declared.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Dereference operator (*) &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We have just seen that a variable which stores a reference to another variable is called a pointer. Pointers are said to &quot;point to&quot; the variable whose reference they store.&amp;nbsp; &lt;br /&gt;
Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply &lt;br /&gt;
have to precede the pointer&#39;s identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to &quot;value pointed by&quot;.&amp;nbsp; &lt;br /&gt;
Therefore, following with the values of the previous example, if we write:&amp;nbsp; &lt;br /&gt;
beth = *ted; &lt;br /&gt;
(that we could read as: &quot;beth equal to value pointed by ted&quot;) beth would take the value 25, since ted is 1776, and the value pointed by 1776 is 25.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzMDLQlMvpb2ofiySHjXtAnFma3AXTt5vNXad-eB8wpSz_MutB1YNcfh3LMkYpFsLafrcZw5cGHmMo-yxd33VLiHEpBXwo7jTN767L26yU49TQgz6Q3eIfsNBUvxtSqVfl98BEw5O2eGCZ/s1600/amitiii.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;You must clearly differentiate that the expression ted refers to the value 1776, while *ted (with an asterisk * &lt;br /&gt;
preceding the identifier) refers to the value stored at address 1776, which in this case is 25. Notice the difference of &lt;br /&gt;
including or not including the dereference operator (I have included an explanatory commentary of how each of &lt;br /&gt;
these two expressions could be read):&amp;nbsp; &lt;br /&gt;
beth = ted;&amp;nbsp;&amp;nbsp; // beth equal to ted ( 1776 ) &lt;br /&gt;
beth = *ted;&amp;nbsp; // beth equal to value pointed by ted ( 25 ) &lt;br /&gt;
Notice the difference between the reference and dereference operators: &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp; is the reference operator and can be read as &quot;address of&quot;&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; * is the dereference operator and can be read as &quot;value pointed by&quot;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Thus, they have complementary (or opposite) meanings. A variable referenced with &amp;amp; can be dereferenced with *.&amp;nbsp; &lt;br /&gt;
Earlier we performed the following two assignment operations:&amp;nbsp; &lt;br /&gt;
andy = 25; &lt;br /&gt;
ted = &amp;amp;andy; &lt;br /&gt;
Right after these two statements, all of the following expressions would give true as result:&amp;nbsp; &lt;br /&gt;
andy == 25 &lt;br /&gt;
&amp;amp;andy == 1776 &lt;br /&gt;
ted == 1776 &lt;br /&gt;
*ted == 25 &lt;br /&gt;
The first expression is quite clear considering that the assignment operation performed on andy was andy=25. The &lt;br /&gt;
second one uses the reference operator (&amp;amp;), which returns the address of variable andy, which we assumed it to &lt;br /&gt;
have a value of 1776. The third one is somewhat obvious since the second expression was true and the assignment &lt;br /&gt;
operation performed on ted was ted=&amp;amp;andy. The fourth expression uses the dereference operator (*) that, as we &lt;br /&gt;
have just seen, can be read as &quot;value pointed by&quot;, and the value pointed by ted is indeed 25.&amp;nbsp; &lt;br /&gt;
So, after all that, you may also infer that for as long as the address pointed by ted remains unchanged the &lt;br /&gt;
following expression will also be true:&amp;nbsp; &lt;br /&gt;
*ted == andy&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Declaring variables of pointer types &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its &lt;br /&gt;
declaration which data type a pointer is going to point to. It is not the same thing to point to a char as to point to &lt;br /&gt;
an int or a float.&amp;nbsp; &lt;br /&gt;
The declaration of pointers follows this format:&amp;nbsp; &lt;br /&gt;
type * name;&amp;nbsp; &lt;br /&gt;
where type is the data type of the value that the pointer is intended to point to. This type is not the type of the &lt;br /&gt;
pointer itself! but the type of the data the pointer points to. For example:&amp;nbsp; &lt;br /&gt;
int * number; &lt;br /&gt;
char * character; &lt;br /&gt;
float * greatnumber; &lt;br /&gt;
These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of &lt;br /&gt;
them are pointers and all of them will occupy the same amount of space in memory (the size in memory of a &lt;br /&gt;
pointer depends on the platform where the code is going to run). Nevertheless, the data to which they point to do &lt;br /&gt;
not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a &lt;br /&gt;
char and the last one to a float. Therefore, although these three example variables are all of them pointers which &lt;br /&gt;
occupy the same size in memory, they are said to have different types: int*, char* and float* respectively, &lt;br /&gt;
depending on the type they point to.&amp;nbsp; &lt;br /&gt;
I want to emphasize that the asterisk sign (*) that we use when declaring a pointer only means that it is a pointer &lt;br /&gt;
(it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Now have a look at this code:&amp;nbsp; &lt;br /&gt;
// my first pointer &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int firstvalue, secondvalue; &lt;br /&gt;
&amp;nbsp; int * mypointer; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; mypointer = &amp;amp;firstvalue; &lt;br /&gt;
&amp;nbsp; *mypointer = 10; &lt;br /&gt;
&amp;nbsp; mypointer = &amp;amp;secondvalue; &lt;br /&gt;
&amp;nbsp; *mypointer = 20; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;firstvalue is &quot; &amp;lt;&amp;lt; firstvalue &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;secondvalue is &quot; &amp;lt;&amp;lt; secondvalue &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
firstvalue is 10 &lt;br /&gt;
secondvalue is 20 &lt;br /&gt;
Notice that even though we have never directly set a value to either firstvalue or secondvalue, both end up with &lt;br /&gt;
a value set indirectly through the use of mypointer. This is the procedure:&amp;nbsp; &lt;br /&gt;
First, we have assigned as value of mypointer a reference to firstvalue using the reference operator (&amp;amp;). And &lt;br /&gt;
then we have assigned the value 10 to the memory location pointed by mypointer, that because at this moment is &lt;br /&gt;
pointing to the memory location of firstvalue, this in fact modifies the value of firstvalue.&amp;nbsp; &lt;br /&gt;
In order to demonstrate that a pointer may take several different values during the same program I have repeated &lt;br /&gt;
the process with secondvalue and that same pointer, mypointer.&amp;nbsp; &lt;br /&gt;
Here is an example a little bit more elaborated:&amp;nbsp; &lt;br /&gt;
// more pointers &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int firstvalue = 5, secondvalue = 15; &lt;br /&gt;
&amp;nbsp; int * p1, * p2; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; p1 = &amp;amp;firstvalue;&amp;nbsp; // p1 = address of firstvalue &lt;br /&gt;
&amp;nbsp; p2 = &amp;amp;secondvalue; // p2 = address of secondvalue &lt;br /&gt;
&amp;nbsp; *p1 = 10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // value pointed by p1 = 10 &lt;br /&gt;
&amp;nbsp; *p2 = *p1;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // value pointed by p2 = value pointed by &lt;br /&gt;
p1 &lt;br /&gt;
&amp;nbsp; p1 = p2;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // p1 = p2 (value of pointer is copied) &lt;br /&gt;
&amp;nbsp; *p1 = 20;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // value pointed by p1 = 20 &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;firstvalue is &quot; &amp;lt;&amp;lt; firstvalue &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;secondvalue is &quot; &amp;lt;&amp;lt; secondvalue &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
firstvalue is 10 &lt;br /&gt;
secondvalue is 20 &lt;br /&gt;
I have included as a comment on each line how the code can be read: ampersand (&amp;amp;) as &quot;address of&quot; and asterisk &lt;br /&gt;
(*) as &quot;value pointed by&quot;.&amp;nbsp; &lt;br /&gt;
Notice that there are expressions with pointers p1 and p2, both with and without dereference operator (*). The meaning of an expression using the dereference operator (*) is very different from one that does not: When this operator precedes the pointer name, the expression refers to the value being pointed, while when a pointer name appears without this operator, it refers to the value of the pointer itself (i.e. the address of what the pointer is pointing to).&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Another thing that may call your attention is the line:&amp;nbsp; &lt;br /&gt;
int * p1, * p2; &lt;br /&gt;
This declares the two pointers used in the previous example. But notice that there is an asterisk (*) for each &lt;br /&gt;
pointer, in order for both to have type int* (pointer to int).&amp;nbsp; &lt;br /&gt;
Otherwise, the type for the second variable declared in that line would have been int (and not int*) because of &lt;br /&gt;
precedence relationships. If we had written:&amp;nbsp; &lt;br /&gt;
int * p1, p2; &lt;br /&gt;
p1 would indeed have int* type, but p2 would have type int (spaces do not matter at all for this purpose). This is &lt;br /&gt;
due to operator precedence rules. But anyway, simply remembering that you have to put one asterisk per pointer &lt;br /&gt;
is enough for most pointer users.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Pointers and arrays &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The concept of array is very much bound to the one of pointer. In fact, the identifier of an array is equivalent to the &lt;br /&gt;
address of its first element, as a pointer is equivalent to the address of the first element that it points to, so in fact &lt;br /&gt;
they are the same concept. For example, supposing these two declarations:&amp;nbsp; &lt;br /&gt;
int numbers [20]; &lt;br /&gt;
int * p; &lt;br /&gt;
The following assignment operation would be valid:&amp;nbsp; &lt;br /&gt;
p = numbers; &lt;br /&gt;
After that, p and numbers would be equivalent and would have the same properties. The only difference is that we &lt;br /&gt;
could change the value of pointer p by another one, whereas numbers will always point to the first of the 20 &lt;br /&gt;
elements of type int with which it was defined. Therefore, unlike p, which is an ordinary pointer, numbers is an &lt;br /&gt;
array, and an array can be considered a constant pointer. Therefore, the following allocation would not be valid:&amp;nbsp; &lt;br /&gt;
numbers = p; &lt;br /&gt;
Because numbers is an array, so it operates as a constant pointer, and we cannot assign values to constants.&amp;nbsp; &lt;br /&gt;
Due to the characteristics of variables, all expressions that include pointers in the following example are perfectly &lt;br /&gt;
valid:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
// more pointers &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int numbers[5]; &lt;br /&gt;
&amp;nbsp; int * p; &lt;br /&gt;
&amp;nbsp; p = numbers;&amp;nbsp; *p = 10; &lt;br /&gt;
&amp;nbsp; p++;&amp;nbsp; *p = 20; &lt;br /&gt;
&amp;nbsp; p = &amp;amp;numbers[2];&amp;nbsp; *p = 30; &lt;br /&gt;
&amp;nbsp; p = numbers + 3;&amp;nbsp; *p = 40; &lt;br /&gt;
&amp;nbsp; p = numbers;&amp;nbsp; *(p+4) = 50; &lt;br /&gt;
&amp;nbsp; for (int n=0; n&amp;lt;5; n++) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; numbers[n] &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
10, 20, 30, 40, 50, &lt;br /&gt;
In the chapter about arrays we used brackets ([]) several times in order to specify the index of an element of the &lt;br /&gt;
array to which we wanted to refer. Well, these bracket sign operators [] are also a dereference operator known as &lt;br /&gt;
offset operator. They dereference the variable they follow just as * does, but they also add the number between &lt;br /&gt;
brackets to the address being dereferenced. For example: &lt;br /&gt;
a[5] = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // a [offset of 5] = 0 &lt;br /&gt;
*(a+5) = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // pointed by (a+5) = 0 &lt;br /&gt;
These two expressions are equivalent and valid both if a is a pointer or if a is an array.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Pointer initialization&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;When declaring pointers we may want to explicitly specify which variable we want them to point to:&amp;nbsp; &lt;br /&gt;
int number; &lt;br /&gt;
int *tommy = &amp;amp;number; &lt;br /&gt;
The behavior of this code is equivalent to:&amp;nbsp; &lt;br /&gt;
int number; &lt;br /&gt;
int *tommy; &lt;br /&gt;
tommy = &amp;amp;number; &lt;br /&gt;
When a pointer initialization takes place we are always assigning the reference value to where the pointer points &lt;br /&gt;
(tommy), never the value being pointed (*tommy). You must consider that at the moment of declaring a pointer, the &lt;br /&gt;
asterisk (*) indicates only that it is a pointer, it is not the dereference operator (although both use the same sign: &lt;br /&gt;
*). Remember, they are two different functions of one sign. Thus, we must take care not to confuse the previous &lt;br /&gt;
code with:&amp;nbsp; &lt;br /&gt;
int number; &lt;br /&gt;
int *tommy; &lt;br /&gt;
*tommy = &amp;amp;number; &lt;br /&gt;
that is incorrect, and anyway would not have much sense in this case if you think about it.&amp;nbsp; &lt;br /&gt;
As in the case of arrays, the compiler allows the special case that we want to initialize the content at which the &lt;br /&gt;
pointer points with constants at the same moment the pointer is declared:&amp;nbsp; &lt;br /&gt;
char * terry = &quot;hello&quot;;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In this case, memory space is reserved to contain &quot;hello&quot; and then a pointer to the first character of this memory &lt;br /&gt;
block is assigned to terry. If we imagine that &quot;hello&quot; is stored at the memory locations that start at addresses &lt;br /&gt;
1702.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;It is important to indicate that terry contains the value 1702, and not &#39;h&#39; nor &quot;hello&quot;, although 1702 indeed is &lt;br /&gt;
the address of both of these.&amp;nbsp; &lt;br /&gt;
The pointer terry points to a sequence of characters and can be read as if it was an array (remember that an &lt;br /&gt;
array is just like a constant pointer). For example, we can access the fifth element of the array with any of these &lt;br /&gt;
two expression:&amp;nbsp; &lt;br /&gt;
*(terry+4) &lt;br /&gt;
terry[4] &lt;br /&gt;
Both expressions have a value of &#39;o&#39; (the fifth element of the array).&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Pointer arithmetics &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;To conduct arithmetical operations on pointers is a little different than to conduct them on regular integer data &lt;br /&gt;
types. To begin with, only addition and subtraction operations are allowed to be conducted with them, the others &lt;br /&gt;
make no sense in the world of pointers. But both addition and subtraction have a different behavior with pointers &lt;br /&gt;
according to the size of the data type to which they point.&amp;nbsp; &lt;br /&gt;
When we saw the different fundamental data types, we saw that some occupy more or less space than others in &lt;br /&gt;
the memory. For example, let&#39;s assume that in a given compiler for a specific machine, char takes 1 byte, short &lt;br /&gt;
takes 2 bytes and long takes 4.&amp;nbsp; &lt;br /&gt;
Suppose that we define three pointers in this compiler:&amp;nbsp; &lt;br /&gt;
char *mychar; &lt;br /&gt;
short *myshort; &lt;br /&gt;
long *mylong; &lt;br /&gt;
and that we know that they point to memory locations 1000, 2000 and 3000 respectively.&amp;nbsp; &lt;br /&gt;
So if we write:&amp;nbsp; &lt;br /&gt;
mychar++; &lt;br /&gt;
myshort++; &lt;br /&gt;
mylong++; &lt;br /&gt;
mychar, as you may expect, would contain the value 1001. But not so obviously, myshort would contain the value &lt;br /&gt;
2002, and mylong would contain 3004, even though they have each been increased only once. The reason is that &lt;br /&gt;
when adding one to a pointer we are making it to point to the following element of the same type with which it has &lt;br /&gt;
been defined, and therefore the size in bytes of the type pointed is added to the pointer.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxLSE5r-goGC1OxUOw-ddB-RhS-8kcJzUoEF5D1o1mJX0PALJOpTE0WroqVM8KSEnHpJFXEkkuhpuh8dujZ7iUTdAWhsNdihVefamK2AxjKigaUX8Lat74Z0dbxCoCmbi0_3ED9ijyIhiB/s1600/amitiy.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This is applicable both when adding and subtracting any number to a pointer. It would happen exactly the same if &lt;br /&gt;
we write:&amp;nbsp; &lt;br /&gt;
mychar = mychar + 1; &lt;br /&gt;
myshort = myshort + 1; &lt;br /&gt;
mylong = mylong + 1; &lt;br /&gt;
Both the increase (++) and decrease (--) operators have greater operator precedence than the dereference &lt;br /&gt;
operator (*), but both have a special behavior when used as suffix (the expression is evaluated with the value it &lt;br /&gt;
had before being increased). Therefore, the following expression may lead to confusion:&amp;nbsp; &lt;br /&gt;
*p++ &lt;br /&gt;
Because ++ has greater precedence than *, this expression is equivalent to *(p++). Therefore, what it does is to &lt;br /&gt;
increase the value of p (so it now points to the next element), but because ++ is used as postfix the whole &lt;br /&gt;
expression is evaluated as the value pointed by the original reference (the address the pointer pointed to before &lt;br /&gt;
being increased).&amp;nbsp; &lt;br /&gt;
Notice the difference with:&amp;nbsp; &lt;br /&gt;
(*p)++&amp;nbsp; &lt;br /&gt;
Here, the expression would have been evaluated as the value pointed by p increased by one. The value of p (the &lt;br /&gt;
pointer itself) would not be modified (what is being modified is what it is being pointed to by this pointer).&amp;nbsp; &lt;br /&gt;
If we write:&amp;nbsp; &lt;br /&gt;
*p++ = *q++; &lt;br /&gt;
Because ++ has a higher precedence than *, both p and q are increased, but because both increase operators (++) &lt;br /&gt;
are used as postfix and not prefix, the value assigned to *p is *q before both p and q are increased. And then both &lt;br /&gt;
are increased. It would be roughly equivalent to:&amp;nbsp; &lt;br /&gt;
*p = *q; &lt;br /&gt;
++p; &lt;br /&gt;
++q; &lt;br /&gt;
Like always, I recommend you to use parentheses () in order to avoid unexpected results and to give more &lt;br /&gt;
legibility to the code.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Pointers to pointers &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;C++ allows the use of pointers that point to pointers, that these, in its turn, point to data (or even to other &lt;br /&gt;
pointers). In order to do that, we only need to add an asterisk (*) for each level of reference in their declarations:&amp;nbsp; &lt;br /&gt;
char a; &lt;br /&gt;
char * b; &lt;br /&gt;
char ** c; &lt;br /&gt;
a = &#39;z&#39;; &lt;br /&gt;
b = &amp;amp;a; &lt;br /&gt;
c = &amp;amp;b; &lt;br /&gt;
This, supposing the randomly chosen memory locations for each variable of 7230, 8092 and 10502, could be &lt;br /&gt;
represented as:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZG8pfZj-FYtSN3KOp5Dx0-gXcIfTbanJPB5rUurDSGQBm-9x_7D9epAqECRlldm4rFuWduaI7EEOURi2MwGWvMrPr8to-etDsPrE8kOQyYAGbHhTyGelT5VHuLsXZvGddb2ikKnnAZObS/s1600/amitcool.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The value of each variable is written inside each cell; under the cells are their respective addresses in memory.&amp;nbsp; &lt;br /&gt;
The new thing in this example is variable c, which can be used in three different levels of indirection, each one of &lt;br /&gt;
them would correspond to a different value:&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c has type char** and a value of 8092&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *c has type char* and a value of 7230&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; **c has type char and a value of &#39;z&#39;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;void pointers &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers &lt;br /&gt;
are pointers that point to a value that has no type (and thus also an undetermined length and undetermined &lt;br /&gt;
dereference properties).&amp;nbsp; &lt;br /&gt;
This allows void pointers to point to any data type, from an integer value or a float to a string of characters. But in &lt;br /&gt;
exchange they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, &lt;br /&gt;
since we have no type to dereference to), and for that reason we will always have to cast the address in the void &lt;br /&gt;
pointer to some other pointer type that points to a concrete data type before dereferencing it.&amp;nbsp; &lt;br /&gt;
One of its uses may be to pass generic parameters to a function:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
72 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// increaser &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void increase (void* data, int psize) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; if ( psize == sizeof(char) ) &lt;br /&gt;
&amp;nbsp; { char* pchar; pchar=(char*)data; ++(*pchar); } &lt;br /&gt;
&amp;nbsp; else if (psize == sizeof(int) ) &lt;br /&gt;
&amp;nbsp; { int* pint; pint=(int*)data; ++(*pint); } &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; char a = &#39;x&#39;; &lt;br /&gt;
&amp;nbsp; int b = 1602; &lt;br /&gt;
&amp;nbsp; increase (&amp;amp;a,sizeof(a)); &lt;br /&gt;
&amp;nbsp; increase (&amp;amp;b,sizeof(b)); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; &quot;, &quot; &amp;lt;&amp;lt; b &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
y, 1603 &lt;br /&gt;
sizeof is an operator integrated in the C++ language that returns the size in bytes of its parameter. For non-&lt;br /&gt;
dynamic data types this value is a constant. Therefore, for example, sizeof(char) is 1, because char type is one &lt;br /&gt;
byte long.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Null pointer &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing &lt;br /&gt;
to any valid reference or memory address. This value is the result of type-casting the integer value zero to any &lt;br /&gt;
pointer type.&amp;nbsp; &lt;br /&gt;
int * p; &lt;br /&gt;
p = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // p has a null pointer value &lt;br /&gt;
Do not confuse null pointers with void pointers. A null pointer is a value that any pointer may take to represent &lt;br /&gt;
that it is pointing to &quot;nowhere&quot;, while a void pointer is a special type of pointer that can point to somewhere &lt;br /&gt;
without a specific type. One refers to the value stored in the pointer itself and the other to the type of data it points &lt;br /&gt;
to.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Pointers to functions&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;C++ allows operations with pointers to functions. The typical use of this is for passing a function as an argument to &lt;br /&gt;
another function, since these cannot be passed dereferenced. In order to declare a pointer to a function we have to &lt;br /&gt;
declare it like the prototype of the function except that the name of the function is enclosed between parentheses &lt;br /&gt;
() and an asterisk (*) is inserted before the name: // pointer to functions &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int addition (int a, int b) &lt;br /&gt;
{ return (a+b); } &lt;br /&gt;
&lt;br /&gt;
int subtraction (int a, int b) &lt;br /&gt;
{ return (a-b); } &lt;br /&gt;
&lt;br /&gt;
int operation (int x, int y, int &lt;br /&gt;
(*functocall)(int,int)) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int g; &lt;br /&gt;
&amp;nbsp; g = (*functocall)(x,y); &lt;br /&gt;
&amp;nbsp; return (g); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int m,n; &lt;br /&gt;
&amp;nbsp; int (*minus)(int,int) = subtraction; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; m = operation (7, 5, addition); &lt;br /&gt;
&amp;nbsp; n = operation (20, m, minus); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt;n; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
8 &lt;br /&gt;
In the example, minus is a pointer to a function that has two parameters of type int. It is immediately assigned to &lt;br /&gt;
point to the function subtraction, all in a single line:&amp;nbsp; &lt;br /&gt;
int (* minus)(int,int) = subtraction;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/3977766863934624604/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/pointers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3977766863934624604'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3977766863934624604'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/pointers.html' title='Pointers'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguUTsheET7fEm6IwOgXRDaRek0ZMFYWDJDAoE4a_55hMfPbE_VRlldme0ZNjQOm8f2AIuibmrpab50q8kdK9FCvPTdxLTi2LOnav-wTDjHFy3qTEvH6xGgDpSdDj3TRrdK8QHyqzkJq0Bc/s72-c/amituiip.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-3246783673615624418</id><published>2011-03-11T04:26:00.000-08:00</published><updated>2011-05-26T04:34:32.711-07:00</updated><title type='text'>Character Sequences</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;As you may already know, the C++ Standard Library implements a powerful string class, which is very useful to &lt;br /&gt;
handle and manipulate strings of characters. However, because strings are in fact sequences of characters, we can &lt;br /&gt;
represent them also as plain arrays of char elements.&amp;nbsp; &lt;br /&gt;
For example, the following array:&amp;nbsp; &lt;br /&gt;
char jenny [20]; &lt;br /&gt;
is an array that can store up to 20 elements of type char.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Therefore, in this array, in theory, we can store sequences of characters up to 20 characters long. But we can also &lt;br /&gt;
store shorter sequences. For example, jenny could store at some point in a program either the sequence &quot;Hello&quot; &lt;br /&gt;
or the sequence &quot;Merry christmas&quot;, since both are shorter than 20 characters.&amp;nbsp; &lt;br /&gt;
Therefore, since the array of characters can store shorter sequences than its total length, a special character is &lt;br /&gt;
used to signal the end of the valid sequence: the null character, whose literal constant can be written as &#39;\0&#39; &lt;br /&gt;
(backslash, zero).&amp;nbsp; &lt;br /&gt;
Our array of 20 elements of type char, called jenny, can be represented storing the characters sequences &quot;Hello&quot; &lt;br /&gt;
and &quot;Merry Christmas&quot; as:&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjzhfGyL1fGMHvjhbCe1kUKqyk0BN95pCwyam-D2nwgtSXOsYe5awDcSt5TbosImNpi_j752_L-TltYK-L9by6TrBOO0CglAvNWJP30X0i6S1K7v53-YFSFn2QYA7vB4QaBySmg8EyjIXi/s1600/amitkkk.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Notice how after the valid content a null character (&#39;\0&#39;) has been included in order to indicate the end of the &lt;br /&gt;
sequence. The panels in gray color represent char elements with undetermined values.&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Initialization of null-terminated character sequences &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Because arrays of characters are ordinary arrays they follow all their same rules. For example, if we want to &lt;br /&gt;
initialize an array of characters with some predetermined sequence of characters we can do it just like any other &lt;br /&gt;
array:&amp;nbsp; &lt;br /&gt;
char myword[] = { &#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;\0&#39; }; &lt;br /&gt;
In this case we would have declared an array of 6 elements of type char initialized with the characters that form &lt;br /&gt;
the word &quot;Hello&quot; plus a null character &#39;\0&#39; at the end. &lt;br /&gt;
But arrays of char elements have an additional method to initialize their values: using string literals.&amp;nbsp; &lt;br /&gt;
In the expressions we have used in some examples in previous chapters, constants that represent entire strings of characters have already showed up several times. These are specified enclosing the text to become a string literal between double quotes (&quot;). For example:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&quot;the result is: &quot;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;is a constant string literal that we have probably used already.&amp;nbsp; &lt;br /&gt;
Double quoted strings (&quot;) are literal constants whose type is in fact a null-terminated array of characters. So string &lt;br /&gt;
literals enclosed between double quotes always have a null character (&#39;\0&#39;) automatically appended at the end.&amp;nbsp; &lt;br /&gt;
Therefore we can initialize the array of char elements called myword with a null-terminated sequence of characters &lt;br /&gt;
by either one of these two methods:&amp;nbsp; &lt;br /&gt;
char myword [] = { &#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;\0&#39; }; &lt;br /&gt;
char myword [] = &quot;Hello&quot;;&amp;nbsp; &lt;br /&gt;
In both cases the array of characters myword is declared with a size of 6 elements of type char: the 5 characters &lt;br /&gt;
that compose the word &quot;Hello&quot; plus a final null character (&#39;\0&#39;) which specifies the end of the sequence and that, &lt;br /&gt;
in the second case, when using double quotes (&quot;) it is appended automatically.&amp;nbsp; &lt;br /&gt;
Please notice that we are talking about initializing an array of characters in the moment it is being declared, and &lt;br /&gt;
not about assigning values to them once they have already been declared. In fact because this type of null-&lt;br /&gt;
terminated arrays of characters are regular arrays we have the same restrictions that we have with any other &lt;br /&gt;
array, so we are not able to copy blocks of data with an assignment operation.&amp;nbsp; &lt;br /&gt;
Assuming mystext is a char[] variable, expressions within a source code like:&amp;nbsp; &lt;br /&gt;
mystext = &quot;Hello&quot;; &lt;br /&gt;
mystext[] = &quot;Hello&quot;; &lt;br /&gt;
would not be valid, like neither would be:&amp;nbsp; &lt;br /&gt;
mystext = { &#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;\0&#39; }; &lt;br /&gt;
The reason for this may become more comprehensible once you know a bit more about pointers, since then it will be clarified that an array is in fact a constant pointer pointing to a block of memory.&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Using null-terminated sequences of characters &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Null-terminated sequences of characters are the natural way of treating strings in C++, so they can be used as &lt;br /&gt;
such in many procedures. In fact, regular string literals have this type (char[]) and can also be used in most &lt;br /&gt;
cases.&amp;nbsp; For example, cin and cout support null-terminated sequences as valid containers for sequences of characters, so they can be used directly to extract strings of characters from cin or to insert them into cout. For example:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
62 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// null-terminated sequences of characters &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; char question[] = &quot;Please, enter your first &lt;br /&gt;
name: &quot;; &lt;br /&gt;
&amp;nbsp; char greeting[] = &quot;Hello, &quot;; &lt;br /&gt;
&amp;nbsp; char yourname [80]; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; question; &lt;br /&gt;
&amp;nbsp; cin &amp;gt;&amp;gt; yourname; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; greeting &amp;lt;&amp;lt; yourname &amp;lt;&amp;lt; &quot;!&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Please, enter your first name: John &lt;br /&gt;
Hello, John! &lt;br /&gt;
As you can see, we have declared three arrays of char elements. The first two were initialized with string literal &lt;br /&gt;
constants, while the third one was left uninitialized. In any case, we have to speficify the size of the array: in the &lt;br /&gt;
first two (question and greeting) the size was implicitly defined by the length of the literal constant they were &lt;br /&gt;
initialized to. While for yourname we have explicitly specified that it has a size of 80 chars.&amp;nbsp; &lt;br /&gt;
Finally, sequences of characters stored in char arrays can easily be converted into string objects just by using the &lt;br /&gt;
assignment operator:&amp;nbsp; &lt;br /&gt;
string mystring; &lt;br /&gt;
char myntcs[]=&quot;some text&quot;; &lt;br /&gt;
mystring = myntcs;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/3246783673615624418/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/character-sequences.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3246783673615624418'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3246783673615624418'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/character-sequences.html' title='Character Sequences'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjzhfGyL1fGMHvjhbCe1kUKqyk0BN95pCwyam-D2nwgtSXOsYe5awDcSt5TbosImNpi_j752_L-TltYK-L9by6TrBOO0CglAvNWJP30X0i6S1K7v53-YFSFn2QYA7vB4QaBySmg8EyjIXi/s72-c/amitkkk.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-893439303612181921</id><published>2011-03-10T00:45:00.000-08:00</published><updated>2011-05-27T18:22:09.720-07:00</updated><title type='text'>Arrays</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;An array is a series of elements of the same type placed in contiguous memory locations that can be individually &lt;br /&gt;
referenced by adding an index to a unique identifier.&amp;nbsp; &lt;br /&gt;
That means that, for example, we can store 5 values of type int in an array without having to declare 5 different &lt;br /&gt;
variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the &lt;br /&gt;
same type, int for example, with a unique identifier.&amp;nbsp; &lt;br /&gt;
For example, an array to contain 5 integer values of type int called billy could be represented like this: &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNhkRKXHQv-xcfTKcb-DPKeq1YtKkT_RK3aDDtFhZio0azbBqspQlzKxiXWyppJ2-4rlaLrqzX8QhDySl9f26lCme2JT3Fr0r0Br56zAqiNNwZd0TyuLx7mKCZeKK5n7AahXhGb4nOToEu/s1600/amkk%252B%252B%252B.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;where each blank panel represents an element of the array, that in this case are integer values of type int. These &lt;/div&gt;elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length.&amp;nbsp; &lt;br /&gt;
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:&amp;nbsp; &lt;br /&gt;
type name [elements]; &lt;br /&gt;
where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is always &lt;br /&gt;
enclosed in square brackets []), specifies how many of these elements the array has to contain.&amp;nbsp; &lt;br /&gt;
Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:&amp;nbsp; &lt;br /&gt;
int billy [5]; &lt;br /&gt;
NOTE: The elements field within brackets [] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Initializing arrays. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its &lt;/div&gt;elements will not be initialized to any value by default, so their content will be undetermined until we store some &lt;br /&gt;
value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their &lt;br /&gt;
default values, which for all fundamental types this means they are filled with zeros.&amp;nbsp; &lt;br /&gt;
In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }. For example:&amp;nbsp; &lt;br /&gt;
int billy [5] = { 16, 2, 77, 40, 12071 }; &lt;br /&gt;
This declaration would have created an array like this:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;The amount of values between braces { } must not be larger than the number of elements that we declare for the &lt;/div&gt;array between square brackets [ ]. For example, in the example of array billy we have declared that it has 5 &lt;br /&gt;
elements and in the list of initial values within braces { } we have specified 5 values, one for each element.&amp;nbsp; &lt;br /&gt;
When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets &lt;br /&gt;
empty [ ]. In this case, the compiler will assume a size for the array that matches the number of values included &lt;br /&gt;
between braces { }:&amp;nbsp; &lt;br /&gt;
int billy [] = { 16, 2, 77, 40, 12071 }; &lt;br /&gt;
After this declaration, array billy would be 5 ints long, since we have provided 5 initialization values.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Accessing the values of an array. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;In any point of a program in which an array is visible, we can access the value of any of its elements individually as &lt;/div&gt;if it was a normal variable, thus being able to both read and modify its value. The format is as simple as:&amp;nbsp; &lt;br /&gt;
name[index]&amp;nbsp; &lt;br /&gt;
Following the previous examples in which billy had 5 elements and each of those elements was of type int, the &lt;br /&gt;
name which we can use to refer to each element is the following:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
For example, to store the value 75 in the third element of billy, we could write the following statement:&amp;nbsp; &lt;br /&gt;
billy[2] = 75; &lt;br /&gt;
and, for example, to pass the value of the third element of billy to a variable called a, we could write:&amp;nbsp; &lt;br /&gt;
a = billy[2]; &lt;br /&gt;
Therefore, the expression billy[2] is for all purposes like a variable of type int.&amp;nbsp; &lt;br /&gt;
Notice that the third element of billy is specified billy[2], since the first one is billy[0], the second one is &lt;br /&gt;
billy[1], and therefore, the third one is billy[2]. By this same reason, its last element is billy[4]. Therefore, if &lt;br /&gt;
we write billy[5], we would be accessing the sixth element of billy and therefore exceeding the size of the array.&amp;nbsp; &lt;br /&gt;
In C++ it is syntactically correct to exceed the valid range of indices for an array. This can create problems, since &lt;br /&gt;
accessing out-of-range elements do not cause compilation errors but can cause runtime errors. The reason why &lt;br /&gt;
this is allowed will be seen further ahead when we begin to use pointers.&amp;nbsp; &lt;br /&gt;
At this point it is important to be able to clearly distinguish between the two uses that brackets [ ] have related to arrays. They perform two different tasks: one is to specify the size of arrays when they are declared; and the second one is to specify indices for concrete array elements. Do not confuse these two possible uses of brackets [ ] with arrays.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;int billy[5];&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // declaration of a new array &lt;/div&gt;billy[2] = 75;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // access to an element of the array. &lt;br /&gt;
If you read carefully, you will see that a type specifier always precedes a variable or array declaration, while it &lt;br /&gt;
never precedes an access.&amp;nbsp; &lt;br /&gt;
Some other valid operations with arrays:&amp;nbsp; &lt;br /&gt;
billy[0] = a; &lt;br /&gt;
billy[a] = 75; &lt;br /&gt;
b = billy [a+2]; &lt;br /&gt;
billy[billy[a]] = billy[2] + 5; &lt;br /&gt;
// arrays example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int billy [] = {16, 2, 77, 40, 12071}; &lt;br /&gt;
int n, result=0; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; for ( n=0 ; n&amp;lt;5 ; n++ ) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; result += billy[n]; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; result; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
12206&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Multidimensional arrays&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;Multidimensional arrays can be described as &quot;arrays of arrays&quot;. For example, a bidimensional array can be &lt;/div&gt;imagined as a bidimensional table made of elements, all of them of a same uniform data type.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqwAJrpjkC-KOTuhwCRcW_qED1DRCbyF8JKXiCnX0K7_BSQ1SnrhFK5I8Odl-fqUaMGIzT6a8257wmKO_Xeu9DiDwhGDeeevWXY_BqzKaACOYKorI90BIGai_6DsH7Bazz1iC3qdOWGdTf/s1600/amitonli9c%252B%252B.jpg&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;jimmy represents a bidimensional array of 3 per 5 elements of type int. The way to declare this array in C++ &lt;/div&gt;would be:&amp;nbsp; &lt;br /&gt;
int jimmy [3][5]; &lt;br /&gt;
and, for example, the way to reference the second element vertically and fourth horizontally in an expression would be:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;jimmy[1][3] &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;(remember that array indices always begin by zero).&amp;nbsp; &lt;/div&gt;Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can contain as many indices as &lt;br /&gt;
needed. But be careful! The amount of memory needed for an array rapidly increases with each dimension. For &lt;br /&gt;
example:&amp;nbsp; &lt;br /&gt;
char century [100][365][24][60][60]; &lt;br /&gt;
declares an array with a char element for each second in a century, that is more than 3 billion chars. So this &lt;br /&gt;
declaration would consume more than 3 gigabytes of memory!&amp;nbsp; &lt;br /&gt;
Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a &lt;br /&gt;
simple array just by putting a factor between its indices:&amp;nbsp; &lt;br /&gt;
int jimmy [3][5];&amp;nbsp;&amp;nbsp; // is equivalent to &lt;br /&gt;
int jimmy [15];&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // (3 * 5 = 15)&amp;nbsp; &lt;br /&gt;
With the only difference that with multidimensional arrays the compiler remembers the depth of each imaginary &lt;br /&gt;
dimension for us. Take as example these two pieces of code, with both exactly the same result. One uses a &lt;br /&gt;
bidimensional array and the other one uses a simple array:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAaeKz15OGuSA0z7EtApvbjAffQYgJ5xc0UyZkM31l-62RNw9v7lUglUB4BZG0u8RRgmwO4g3FSKKhykNYJo2qyPWeEYJDixOCLKJFYvxQ0qrhCkDIbHwOAsH9qOqt9K5bBpni0zPDjLO8/s1600/amitupadhyayuuu.jpg&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;We have used &quot;defined constants&quot; (#define) to simplify possible future modifications of the program. For example, &lt;/div&gt;in case that we decided to enlarge the array to a height of 4 instead of 3 it could be done simply by changing the &lt;br /&gt;
line:&amp;nbsp; &lt;br /&gt;
#define HEIGHT 3 &lt;br /&gt;
to: &lt;br /&gt;
#define HEIGHT 4 &lt;br /&gt;
with no need to make any other modifications to the program.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Arrays as parameters&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a &lt;/div&gt;complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In &lt;br /&gt;
practice this has almost the same effect and it is a much faster and more efficient operation.&amp;nbsp; &lt;br /&gt;
In order to accept arrays as parameters the only thing that we have to do when declaring the function is to specify &lt;br /&gt;
in its parameters the element type of the array, an identifier and a pair of void brackets []. For example, the &lt;br /&gt;
following function:&amp;nbsp; &lt;br /&gt;
void procedure (int arg[]) &lt;br /&gt;
accepts a parameter of type &quot;array of int&quot; called arg. In order to pass to this function an array declared as:&amp;nbsp; &lt;br /&gt;
int myarray [40]; &lt;br /&gt;
it would be enough to write a call like this:&amp;nbsp; &lt;br /&gt;
procedure (myarray); &lt;br /&gt;
Here you have a complete example:&amp;nbsp; &lt;br /&gt;
// arrays as parameters &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void printarray (int arg[], int length) { &lt;br /&gt;
&amp;nbsp; for (int n=0; n&amp;lt;length; n++) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; arg[n] &amp;lt;&amp;lt; &quot; &quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;\n&quot;; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int firstarray[] = {5, 10, 15}; &lt;br /&gt;
&amp;nbsp; int secondarray[] = {2, 4, 6, 8, 10}; &lt;br /&gt;
&amp;nbsp; printarray (firstarray,3); &lt;br /&gt;
&amp;nbsp; printarray (secondarray,5); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
5 10 15 &lt;br /&gt;
2 4 6 8 10 &lt;br /&gt;
As you can see, the first parameter (int arg[]) accepts any array whose elements are of type int, whatever its &lt;br /&gt;
length. For that reason we have included a second parameter that tells the function the length of each array that we pass to it as its first parameter. This allows the for loop that prints out the array to know the range to iterate in &lt;br /&gt;
the passed array without going out of range.&amp;nbsp; &lt;br /&gt;
In a function declaration it is also possible to include multidimensional arrays. The format for a tridimensional array &lt;br /&gt;
parameter is:&amp;nbsp; &lt;br /&gt;
base_type[][depth][depth] &lt;br /&gt;
for example, a function with a multidimensional array as argument could be:&amp;nbsp; &lt;br /&gt;
void procedure (int myarray[][3][4]) &lt;br /&gt;
Notice that the first brackets [] are left blank while the following ones are not. This is so because the compiler &lt;br /&gt;
must be able to determine within the function which is the depth of each additional dimension.&amp;nbsp; &lt;br /&gt;
Arrays, both simple or multidimensional, passed as function parameters are a quite common source of errors for novice programmers. I recommend the reading of the chapter about Pointers for a better understanding on how arrays operate. &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;________________________________________________________________&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;FRIENDS SIGN UP HERE AND GET CHANCE TO MAKE MONEY-&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;MAKE Rs. 25 FOR EACH SIGN UP AND Rs. 12 FOR EACH REFERRAL FROM THIS WEBSITE.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: red; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;NOTE :- EVERY ACTION MUCH BE TAKEN FROM THIS WEBSITE ONLY.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;iframe frameborder=&quot;0&quot; height=&quot;1&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://origin-img.shaadi.com/shaadi-rewards/get-html-banner.php?ptnr=ys4eq&amp;amp;banner_type=popup&amp;amp;banner_size=300x250&quot; width=&quot;1&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;a href=&quot;http://www.shaadi.com/ptnr.php?ptnr=ys4eq&quot;&gt;&lt;span style=&quot;color: blue; font-family: Ms Sans Serif; font-size: xx-small;&quot;&gt;Shaadi.com Indian Matrimonials&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;iframe frameborder=&quot;0&quot; height=&quot;280&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://origin-img.shaadi.com/shaadi-rewards/get-html-banner.php?ptnr=ys4eq&amp;amp;banner_type=searchbanners&amp;amp;banner_size=336x280&quot; width=&quot;336&quot;&gt;&lt;/iframe&gt;&lt;/center&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/893439303612181921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/893439303612181921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/893439303612181921'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/arrays.html' title='Arrays'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNhkRKXHQv-xcfTKcb-DPKeq1YtKkT_RK3aDDtFhZio0azbBqspQlzKxiXWyppJ2-4rlaLrqzX8QhDySl9f26lCme2JT3Fr0r0Br56zAqiNNwZd0TyuLx7mKCZeKK5n7AahXhGb4nOToEu/s72-c/amkk%252B%252B%252B.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-7797639281638827319</id><published>2011-03-09T00:32:00.000-08:00</published><updated>2011-05-26T00:43:57.210-07:00</updated><title type='text'>Functions (II)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Arguments passed by value and by reference. &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. &lt;br /&gt;
This means that when calling a function with parameters, what we have passed to the function were copies of their &lt;br /&gt;
values but never the variables themselves. For example, suppose that we called our first function addition using &lt;br /&gt;
the following code:&amp;nbsp; &lt;br /&gt;
int x=5, y=3, z; &lt;br /&gt;
z = addition ( x , y ); &lt;br /&gt;
What we did in this case was to call to function addition passing the values of x and y, i.e. 5 and 3 respectively, but not the variables x and y themselves.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;This way, when the function addition is called, the value of its local variables a and b become 5 and 3 respectively, &lt;br /&gt;
but any modification to either a or b within the function addition will not have any effect in the values of x and y outside it, because variables x and y were not themselves passed to the function, but only copies of their values at the moment the function was called.&amp;nbsp; &lt;br /&gt;
But there might be some cases where you need to manipulate from inside a function the value of an external &lt;br /&gt;
variable. For that purpose we can use arguments passed by reference, as in the function duplicate of the following &lt;br /&gt;
example:&amp;nbsp; &lt;br /&gt;
// passing parameters by reference &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void duplicate (int&amp;amp; a, int&amp;amp; b, int&amp;amp; c) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; a*=2; &lt;br /&gt;
&amp;nbsp; b*=2; &lt;br /&gt;
&amp;nbsp; c*=2; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x=1, y=3, z=7; &lt;br /&gt;
&amp;nbsp; duplicate (x, y, z); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x=&quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &quot;, y=&quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &quot;, z=&quot; &amp;lt;&amp;lt; z; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
x=2, y=6, z=14 &lt;br /&gt;
The first thing that should call your attention is that in the declaration of duplicate the type of each parameter &lt;br /&gt;
was followed by an ampersand sign (&amp;amp;). This ampersand is what specifies that their corresponding arguments are to be passed by reference instead of by value.&amp;nbsp; &lt;br /&gt;
When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
47 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
Functions (II) &lt;br /&gt;
Arguments passed by value and by reference. &lt;br /&gt;
Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. &lt;br /&gt;
This means that when calling a function with parameters, what we have passed to the function were copies of their &lt;br /&gt;
values but never the variables themselves. For example, suppose that we called our first function addition using &lt;br /&gt;
the following code: &lt;br /&gt;
int x=5, y=3, z; &lt;br /&gt;
z = addition ( x , y ); &lt;br /&gt;
What we did in this case was to call to function addition passing the values of x and y, i.e. 5 and 3 respectively, but &lt;br /&gt;
not the variables x and y themselves. &lt;br /&gt;
&lt;br /&gt;
This way, when the function addition is called, the value of its local variables a and b become 5 and 3 respectively, &lt;br /&gt;
but any modification to either a or b within the function addition will not have any effect in the values of x and y &lt;br /&gt;
outside it, because variables x and y were not themselves passed to the function, but only copies of their values at &lt;br /&gt;
the moment the function was called. &lt;br /&gt;
But there might be some cases where you need to manipulate from inside a function the value of an external &lt;br /&gt;
variable. For that purpose we can use arguments passed by reference, as in the function duplicate of the following &lt;br /&gt;
example: &lt;br /&gt;
// passing parameters by reference &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void duplicate (int&amp;amp; a, int&amp;amp; b, int&amp;amp; c) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; a*=2; &lt;br /&gt;
&amp;nbsp; b*=2; &lt;br /&gt;
&amp;nbsp; c*=2; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x=1, y=3, z=7; &lt;br /&gt;
&amp;nbsp; duplicate (x, y, z); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x=&quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &quot;, y=&quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &quot;, z=&quot; &amp;lt;&amp;lt; z; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
x=2, y=6, z=14 &lt;br /&gt;
The first thing that should call your attention is that in the declaration of duplicate the type of each parameter &lt;br /&gt;
was followed by an ampersand sign (&amp;amp;). This ampersand is what specifies that their corresponding arguments are &lt;br /&gt;
to be passed by reference instead of by value. &lt;br /&gt;
When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the &lt;br /&gt;
variable itself to the function and any modification that we do to the local variables will have an effect in their &lt;br /&gt;
counterpart variables passed as arguments in the call to the function.&amp;nbsp; TThhee&amp;nbsp; CC++++&amp;nbsp; LLaanngguuaaggee&amp;nbsp; TTuuttoorriiaall &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
48 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
&lt;br /&gt;
To explain it in another way, we associate a, b and c with the arguments passed on the function call (x, y and z) &lt;br /&gt;
and any change that we do on a within the function will affect the value of x outside it. Any change that we do on b &lt;br /&gt;
will affect y, and the same with c and z. &lt;br /&gt;
That is why our program&#39;s output, that shows the values stored in x, y and z after the call to duplicate, shows the &lt;br /&gt;
values of all the three variables of main doubled. &lt;br /&gt;
If when declaring the following function: &lt;br /&gt;
void duplicate (int&amp;amp; a, int&amp;amp; b, int&amp;amp; c) &lt;br /&gt;
we had declared it this way: &lt;br /&gt;
void duplicate (int a, int b, int c) &lt;br /&gt;
i.e., without the ampersand signs (&amp;amp;), we would have not passed the variables by reference, but a copy of their &lt;br /&gt;
values instead, and therefore, the output on screen of our program would have been the values of x, y and z &lt;br /&gt;
without having been modified. &lt;br /&gt;
Passing by reference is also an effective way to allow a function to return more than one value. For example, here &lt;br /&gt;
is a function that returns the previous and next numbers of the first parameter passed. &lt;br /&gt;
// more than one returning value &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void prevnext (int x, int&amp;amp; prev, int&amp;amp; next) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; prev = x-1; &lt;br /&gt;
&amp;nbsp; next = x+1; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x=100, y, z; &lt;br /&gt;
&amp;nbsp; prevnext (x, y, z); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Previous=&quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &quot;, Next=&quot; &amp;lt;&amp;lt; z; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Previous=99, Next=101&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Default values in parameters. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;.&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;When declaring a function we can specify a default value for each of the last parameters. This value will be used if &lt;br /&gt;
the corresponding argument is left blank when calling to the function. To do that, we simply have to use the &lt;br /&gt;
assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not &lt;br /&gt;
passed when the function is called, the default value is used, but if a value is specified this default value is ignored &lt;br /&gt;
and the passed value is used instead. For example:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
49 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// default values in functions &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int divide (int a, int b=2) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int r; &lt;br /&gt;
&amp;nbsp; r=a/b; &lt;br /&gt;
&amp;nbsp; return (r); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; divide (12); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; endl; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; divide (20,4); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
6 &lt;br /&gt;
5 &lt;br /&gt;
As we can see in the body of the program there are two calls to function divide. In the first one:&amp;nbsp; &lt;br /&gt;
divide (12) &lt;br /&gt;
we have only specified one argument, but the function divide allows up to two. So the function divide has &lt;br /&gt;
assumed that the second parameter is 2 since that is what we have specified to happen if this parameter was not &lt;br /&gt;
passed (notice the function declaration, which finishes with int b=2, not just int b). Therefore the result of this &lt;br /&gt;
function call is 6 (12/2).&amp;nbsp; &lt;br /&gt;
In the second call:&amp;nbsp; &lt;br /&gt;
divide (20,4) &lt;br /&gt;
there are two parameters, so the default value for b (int b=2) is ignored and b takes the value passed as &lt;br /&gt;
argument, that is 4, making the result returned equal to 5 (20/4).&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Overloaded functions. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In C++ two different functions can have the same name if their parameter types or number are different. That &lt;br /&gt;
means that you can give the same name to more than one function if they have either a different number of &lt;br /&gt;
parameters or different types in their parameters. For example:&amp;nbsp; &lt;br /&gt;
// overloaded function &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int operate (int a, int b) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; return (a*b); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
float operate (float a, float b) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; return (a/b); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x=5,y=2; &lt;br /&gt;
&amp;nbsp; float n=5.0,m=2.0; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; operate (x,y); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;\n&quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; operate (n,m); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
10 &lt;br /&gt;
2.5 &lt;br /&gt;
In this case we have defined two functions with the same name, operate, but one of them accepts two parameters &lt;br /&gt;
of type int and the other one accepts them of type float. The compiler knows which one to call in each case by &lt;br /&gt;
examining the types passed as arguments when the function is called. If it is called with two ints as its arguments &lt;br /&gt;
it calls to the function that has two int parameters in its prototype and if it is called with two floats it will call to &lt;br /&gt;
the one which has two float parameters in its prototype.&amp;nbsp; &lt;br /&gt;
In the first call to operate the two arguments passed are of type int, therefore, the function with the first &lt;br /&gt;
prototype is called; This function returns the result of multiplying both parameters. While the second call passes &lt;br /&gt;
two arguments of type float, so the function with the second prototype is called. This one has a different &lt;br /&gt;
behavior: it divides one parameter by the other. So the behavior of a call to operate depends on the type of the &lt;br /&gt;
arguments passed because the function has been overloaded.&amp;nbsp; &lt;br /&gt;
Notice that a function cannot be overloaded only by its return type. At least one of its parameters must have a &lt;br /&gt;
different type.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Inline functions. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The inline specifier indicates the compiler that inline substitution is preferred to the usual function call mechanism &lt;br /&gt;
for a specific function. This does not change the behavior of a function itself, but is used to suggest to the compiler &lt;br /&gt;
that the code generated by the function body is inserted at each point the function is called, instead of being &lt;br /&gt;
inserted only once and perform a regular call to it, which generally involves some additional overhead in running &lt;br /&gt;
time.&amp;nbsp; &lt;br /&gt;
The format for its declaration is:&amp;nbsp; &lt;br /&gt;
inline type name ( arguments ... ) { instructions ... } &lt;br /&gt;
and the call is just like the call to any other function. You do not have to include the inline keyword when calling the function, only in its declaration.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Most compilers already optimize code to generate inline functions when it is more convenient. This specifier only indicates the compiler that inline is preferred for this function.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Recursivity. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Recursivity is the property that functions have to be called by themselves. It is useful for many tasks, like sorting &lt;br /&gt;
or calculate the factorial of numbers. For example, to obtain the factorial of a number (n!) the mathematical &lt;br /&gt;
formula would be:&amp;nbsp; &lt;br /&gt;
n! = n * (n-1) * (n-2) * (n-3) ... * 1 &lt;br /&gt;
more concretely, 5! (factorial of 5) would be:&amp;nbsp; &lt;br /&gt;
5! = 5 * 4 * 3 * 2 * 1 = 120 &lt;br /&gt;
and a recursive function to calculate this in C++ could be:&amp;nbsp; &lt;br /&gt;
// factorial calculator &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
long factorial (long a) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; if (a &amp;gt; 1) &lt;br /&gt;
&amp;nbsp;&amp;nbsp; return (a * factorial (a-1)); &lt;br /&gt;
&amp;nbsp; else &lt;br /&gt;
&amp;nbsp;&amp;nbsp; return (1); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; long number; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Please type a number: &quot;; &lt;br /&gt;
&amp;nbsp; cin &amp;gt;&amp;gt; number; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; number &amp;lt;&amp;lt; &quot;! = &quot; &amp;lt;&amp;lt; factorial (number); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Please type a number: 9 &lt;br /&gt;
9! = 362880&amp;nbsp;&amp;nbsp; &lt;br /&gt;
Notice how in function factorial we included a call to itself, but only if the argument passed was greater than 1, &lt;br /&gt;
since otherwise the function would perform an infinite recursive loop in which once it arrived to 0 it would continue multiplying by all the negative numbers (probably provoking a stack overflow error on runtime).&amp;nbsp; &lt;br /&gt;
This function has a limitation because of the data type we used in its design (long) for more simplicity. The results given will not be valid for values much greater than 10! or 15!, depending on the system you compile it.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Declaring functions. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Until now, we have defined all of the functions before the first appearance of calls to them in the source code. &lt;br /&gt;
These calls were generally in function main which we have always left at the end of the source code. If you try to &lt;br /&gt;
repeat some of the examples of functions described so far, but placing the function main before any of the other &lt;br /&gt;
functions that were called from within it, you will most likely obtain compiling errors. The reason is that to be able &lt;br /&gt;
to call a function it must have been declared in some earlier point of the code, like we have done in all our &lt;br /&gt;
examples.&amp;nbsp; &lt;br /&gt;
But there is an alternative way to avoid writing the whole code of a function before it can be used in main or in &lt;br /&gt;
some other function. This can be achieved by declaring just a prototype of the function before it is used, instead of &lt;br /&gt;
the entire definition. This declaration is shorter than the entire definition, but significant enough for the compiler to &lt;br /&gt;
determine its return type and the types of its parameters.&amp;nbsp; &lt;br /&gt;
Its form is:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;type name ( argument_type1, argument_type2, ...); &lt;br /&gt;
It is identical to a function definition, except that it does not include the body of the function itself (i.e., the &lt;br /&gt;
function statements that in normal definitions are enclosed in braces { }) and instead of that we end the prototype &lt;br /&gt;
declaration with a mandatory semicolon (;).&amp;nbsp; &lt;br /&gt;
The parameter enumeration does not need to include the identifiers, but only the type specifiers. The inclusion of a &lt;br /&gt;
name for each parameter as in the function definition is optional in the prototype declaration. For example, we can &lt;br /&gt;
declare a function called protofunction with two int parameters with any of the following declarations:&amp;nbsp; &lt;br /&gt;
int protofunction (int first, int second); &lt;br /&gt;
int protofunction (int, int); &lt;br /&gt;
Anyway, including a name for each variable makes the prototype more legible.&amp;nbsp; &lt;br /&gt;
// declaring functions prototypes &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void odd (int a); &lt;br /&gt;
void even (int a); &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int i; &lt;br /&gt;
&amp;nbsp; do { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Type a number (0 to exit): &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cin &amp;gt;&amp;gt; i; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; odd (i); &lt;br /&gt;
&amp;nbsp; } while (i!=0); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void odd (int a) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; if ((a%2)!=0) cout &amp;lt;&amp;lt; &quot;Number is odd.\n&quot;; &lt;br /&gt;
&amp;nbsp; else even (a); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void even (int a) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; if ((a%2)==0) cout &amp;lt;&amp;lt; &quot;Number is even.\n&quot;; &lt;br /&gt;
&amp;nbsp; else odd (a); &lt;br /&gt;
} &lt;br /&gt;
Type a number (0 to exit): 9 &lt;br /&gt;
Number is odd. &lt;br /&gt;
Type a number (0 to exit): 6 &lt;br /&gt;
Number is even. &lt;br /&gt;
Type a number (0 to exit): 1030 &lt;br /&gt;
Number is even. &lt;br /&gt;
Type a number (0 to exit): 0 &lt;br /&gt;
Number is even. &lt;br /&gt;
This example is indeed not an example of efficiency. I am sure that at this point you can already make a program &lt;br /&gt;
with the same result, but using only half of the code lines that have been used in this example. Anyway this &lt;br /&gt;
example illustrates how prototyping works. Moreover, in this concrete example the prototyping of at least one of &lt;br /&gt;
the two functions is necessary in order to compile the code without errors.&amp;nbsp; &lt;br /&gt;
The first things that we see are the declaration of functions odd and even:&amp;nbsp; &lt;br /&gt;
void odd (int a); &lt;br /&gt;
void even (int a); &lt;br /&gt;
This allows these functions to be used before they are defined, for example, in main, which now is located where &lt;br /&gt;
some people find it to be a more logical place for the start of a program: the beginning of the source code.&amp;nbsp; &lt;br /&gt;
Anyway, the reason why this program needs at least one of the functions to be declared before it is defined is &lt;br /&gt;
because in odd there is a call to even and in even there is a call to odd. If none of the two functions had been&amp;nbsp;&lt;/div&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;previously declared, a compilation error would happen, since either odd would not not be visible from even &lt;/span&gt;&lt;br style=&quot;font-family: Verdana,sans-serif;&quot; /&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;(because it has still not been declared), or even would not be visible from odd (for the same reason).&amp;nbsp; &lt;/span&gt;&lt;br style=&quot;font-family: Verdana,sans-serif;&quot; /&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Having the prototype of all functions together in the same place within the source code is found practical by some &lt;/span&gt;&lt;br style=&quot;font-family: Verdana,sans-serif;&quot; /&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;programmers, and this can be easily achieved by declaring all functions prototypes at the beginning of a program. &lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/7797639281638827319/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/functions-ii.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/7797639281638827319'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/7797639281638827319'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/functions-ii.html' title='Functions (II)'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-3675316012957898854</id><published>2011-03-08T22:33:00.000-08:00</published><updated>2011-05-26T00:27:05.795-07:00</updated><title type='text'>Functions (I)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Using functions we can structure our programs in a more modular way, accessing all the potential that structured &lt;br /&gt;
programming can offer to us in C++.&amp;nbsp; &lt;br /&gt;
A function is a group of statements that is executed when it is called from some point of the program. The &lt;br /&gt;
following is its format:&amp;nbsp; &lt;br /&gt;
type name ( parameter1, parameter2, ...) { statements } &lt;br /&gt;
where: &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; type is the data type specifier of the data returned by the function.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name is the identifier by which it will be possible to call the function.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parameters (as many as needed): Each parameter consists of a data type specifier followed by an &lt;br /&gt;
identifier, like any regular variable declaration (for example: int x) and which acts within the function as &lt;br /&gt;
a regular local variable. They allow to pass arguments to the function when it is called. The different &lt;br /&gt;
parameters are separated by commas.&amp;nbsp; &lt;br /&gt;
•&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; statements is the function&#39;s body. It is a block of statements surrounded by braces { }.&amp;nbsp; &lt;br /&gt;
Here you have the first function example:&amp;nbsp; &lt;br /&gt;
// function example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int addition (int a, int b) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int r; &lt;br /&gt;
&amp;nbsp; r=a+b; &lt;br /&gt;
&amp;nbsp; return (r); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int z; &lt;br /&gt;
&amp;nbsp; z = addition (5,3); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;The result is &quot; &amp;lt;&amp;lt; z; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
The result is 8 &lt;br /&gt;
In order to examine this code, first of all remember something said at the beginning of this tutorial: a C++ &lt;br /&gt;
program always begins its execution by the main function. So we will begin there.&amp;nbsp; &lt;br /&gt;
We can see how the main function begins by declaring the variable z of type int. Right after that, we see a call to a function called addition. Paying attention we will be able to see the similarity between the structure of the call to the function and the declaration of the function itself some code lines above:&amp;nbsp; &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggO_C8sMf3VQu44iq6nNmextyD7DSU46_Ynlifas3RqGudxJ9g3wm3Cwm_M4Drc59E1ww3T0FKmqsE_sZHo5_TP47ws2yVW6kjOy0iHmz-m4nW_yiOCbXthXPpbl-1tp3DsLPn2kEExSCc/s1600/ami%252B%252B%252B%252B.jpg&quot; /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The parameters and arguments have a clear correspondence. Within the main function we called to addition &lt;br /&gt;
passing two values: 5 and 3, that correspond to the int a and int b parameters declared for function addition.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
At the point at which the function is called from within main, the control is lost by main and passed to function &lt;br /&gt;
addition. The value of both arguments passed in the call (5 and 3) are copied to the local variables int a and int b within the function. &lt;br /&gt;
Function addition declares another local variable (int r), and by means of the expression r=a+b, it assigns to r &lt;br /&gt;
the result of a plus b. Because the actual parameters passed for a and b are 5 and 3 respectively, the result is 8.&amp;nbsp; The following line of code: &lt;br /&gt;
return (r); finalizes function addition, and returns the control back to the function that called it in the first place (in this case, main). At this moment the program follows it regular course from the same point at which it was interrupted by the call to addition. But additionally, because the return statement in function addition specified a value: the content of variable r (return (r);), which at that moment had a value of 8. This value becomes the value of evaluating the function call.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7MlchJt4XeUJsQ4GAJ1E5ZLnPTZo6WBV6oPlZ85G8jcL5phyrNfuLubWaPeh2hdnBvA9_m6UWZAxES21o_5Z2FXPIo5MwjQ9CRHlJk3DJE1LeiNJ6T11HLrv3Pb8x_aKYGXpWDr59ABWk/s1600/amitupadhyay%252B%252B%252B%252B%252B.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&amp;nbsp;So being the value returned by a function the value given to the function call itself when it is evaluated, the &lt;br /&gt;
variable z will be set to the value returned by addition (5, 3), that is 8. To explain it another way, you can &lt;br /&gt;
imagine that the call to a function (addition (5,3)) is literally replaced by the value it returns (8). &lt;br /&gt;
The following line of code in main is: &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;The result is &quot; &amp;lt;&amp;lt; z; &lt;br /&gt;
That, as you may already expect, produces the printing of the result on the screen.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKjZW5cMIO47tgybcoT2QSoMdUv-RHW83AHsTSAB_bPDjoDHqe4LEDzKW7hfVzIMFNAwChbR17yRakTs9EWitWDmM-26YB9k1mIF3QGPoq65NCoYRjaXtkKBRcICCZLEhjfuCO6G8A1okk/s1600/a%252B%252B%252B.jpg&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-family: Verdana,sans-serif; text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;And here is another example about functions:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// function example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int subtraction (int a, int b) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int r; &lt;br /&gt;
&amp;nbsp; r=a-b; &lt;br /&gt;
&amp;nbsp; return (r); &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int x=5, y=3, z; &lt;br /&gt;
&amp;nbsp; z = subtraction (7,2); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;The first result is &quot; &amp;lt;&amp;lt; z &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;The second result is &quot; &amp;lt;&amp;lt; subtraction (7,2) &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;The third result is &quot; &amp;lt;&amp;lt; subtraction (x,y) &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; z= 4 + subtraction (x,y); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;The fourth result is &quot; &amp;lt;&amp;lt; z &amp;lt;&amp;lt; &#39;\n&#39;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
The first result is 5 &lt;br /&gt;
The second result is 5 &lt;br /&gt;
The third result is 2 &lt;br /&gt;
The fourth result is 6 &lt;br /&gt;
In this case we have created a function called subtraction. The only thing that this function does is to subtract &lt;br /&gt;
both passed parameters and to return the result.&amp;nbsp; &lt;br /&gt;
Nevertheless, if we examine function main we will see that we have made several calls to function subtraction. &lt;br /&gt;
We have used some different calling methods so that you see other ways or moments when a function can be &lt;br /&gt;
called.&amp;nbsp; &lt;br /&gt;
In order to fully understand these examples you must consider once again that a call to a function could be &lt;br /&gt;
replaced by the value that the function call itself is going to return. For example, the first case (that you should &lt;br /&gt;
already know because it is the same pattern that we have used in previous examples):&amp;nbsp; &lt;br /&gt;
z = subtraction (7,2); &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;The first result is &quot; &amp;lt;&amp;lt; z; &lt;br /&gt;
If we replace the function call by the value it returns (i.e., 5), we would have:&amp;nbsp; &lt;br /&gt;
z = 5; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;The first result is &quot; &amp;lt;&amp;lt; z; &lt;br /&gt;
As well as&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;The second result is &quot; &amp;lt;&amp;lt; subtraction (7,2); &lt;br /&gt;
has the same result as the previous call, but in this case we made the call to subtraction directly as an insertion &lt;br /&gt;
parameter for cout. Simply consider that the result is the same as if we had written:&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;The second result is &quot; &amp;lt;&amp;lt; 5; &lt;br /&gt;
since 5 is the value returned by subtraction (7,2).&amp;nbsp; &lt;br /&gt;
In the case of:&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;The third result is &quot; &amp;lt;&amp;lt; subtraction (x,y);&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The only new thing that we introduced is that the parameters of subtraction are variables instead of constants. &lt;br /&gt;
That is perfectly valid. In this case the values passed to function subtraction are the values of x and y, that are 5 &lt;br /&gt;
and 3 respectively, giving 2 as result.&amp;nbsp; &lt;br /&gt;
The fourth case is more of the same. Simply note that instead of:&amp;nbsp; &lt;br /&gt;
z = 4 + subtraction (x,y); &lt;br /&gt;
we could have written:&amp;nbsp; &lt;br /&gt;
z = subtraction (x,y) + 4; &lt;br /&gt;
with exactly the same result. I have switched places so you can see that the semicolon sign (;) goes at the end of &lt;br /&gt;
the whole statement. It does not necessarily have to go right after the function call. The explanation might be once &lt;br /&gt;
again that you imagine that a function can be replaced by its returned value:&amp;nbsp; &lt;br /&gt;
z = 4 + 2; &lt;br /&gt;
z = 2 + 4;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Functions with no type. The use of void. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;If you remember the syntax of a function declaration:&amp;nbsp; &lt;br /&gt;
type name ( argument1, argument2 ...) statement &lt;br /&gt;
you will see that the declaration begins with a type, that is the type of the function itself (i.e., the type of the &lt;br /&gt;
datum that will be returned by the function with the return statement). But what if we want to return no value?&amp;nbsp; &lt;br /&gt;
Imagine that we want to make a function just to show a message on the screen. We do not need it to return any &lt;br /&gt;
value. In this case we should use the void type specifier for the function. This is a special specifier that indicates &lt;br /&gt;
absence of type.&amp;nbsp; &lt;br /&gt;
// void function example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
void printmessage () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;I&#39;m a function!&quot;; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; printmessage (); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
I&#39;m a function! &lt;br /&gt;
void can also be used in the function&#39;s parameter list to explicitly specify that we want the function to take no &lt;br /&gt;
actual parameters when it is called. For example, function printmessage could have been declared as:&amp;nbsp; &lt;br /&gt;
void printmessage (void) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;I&#39;m a function!&quot;; &lt;br /&gt;
} &lt;br /&gt;
Although it is optional to specify void in the parameter list. In C++, a parameter list can simply be left blank if we want a function with no parameters. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;What you must always remember is that the format for calling a function includes specifying its name and &lt;br /&gt;
enclosing its parameters between parentheses. The non-existence of parameters does not exempt us from the &lt;br /&gt;
obligation to write the parentheses. For that reason the call to printmessage is:&amp;nbsp; &lt;br /&gt;
printmessage (); &lt;br /&gt;
The parentheses clearly indicate that this is a call to a function and not the name of a variable or some other C++ &lt;br /&gt;
statement. The following call would have been incorrect:&amp;nbsp; &lt;br /&gt;
printmessage;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/3675316012957898854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/functions-i.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3675316012957898854'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/3675316012957898854'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/functions-i.html' title='Functions (I)'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggO_C8sMf3VQu44iq6nNmextyD7DSU46_Ynlifas3RqGudxJ9g3wm3Cwm_M4Drc59E1ww3T0FKmqsE_sZHo5_TP47ws2yVW6kjOy0iHmz-m4nW_yiOCbXthXPpbl-1tp3DsLPn2kEExSCc/s72-c/ami%252B%252B%252B%252B.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-6565138071015841650</id><published>2011-03-07T22:33:00.000-08:00</published><updated>2011-05-25T22:50:51.428-07:00</updated><title type='text'>Control Structures</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat &lt;br /&gt;
code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done &lt;br /&gt;
by our program, when and under which circumstances.&amp;nbsp; &lt;br /&gt;
With the introduction of control structures we are going to have to introduce a new concept: the compound-&lt;br /&gt;
statement or block. A block is a group of statements which are separated by semicolons (;) like all C++ &lt;br /&gt;
statements, but grouped together in a block enclosed in braces: { }:&amp;nbsp; &lt;br /&gt;
{ statement1; statement2; statement3; } &lt;br /&gt;
Most of the control structures that we will see in this section require a generic statement as part of its syntax. A &lt;br /&gt;
statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound &lt;br /&gt;
statement (several instructions grouped in a block), like the one just described. In the case that we want the &lt;br /&gt;
statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the &lt;br /&gt;
statement to be a compound statement it must be enclosed between braces ({}), forming a block.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Conditional structure: if and else&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:&amp;nbsp; &lt;br /&gt;
if (condition) statement &lt;br /&gt;
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is &lt;br /&gt;
false, statement is ignored (not executed) and the program continues right after this conditional structure. &lt;br /&gt;
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:&amp;nbsp; &lt;br /&gt;
if (x == 100) &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is 100&quot;; &lt;br /&gt;
If we want more than a single statement to be executed in case that the condition is true we can specify a block &lt;br /&gt;
using braces { }:&amp;nbsp; &lt;br /&gt;
if (x == 100) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; x; &lt;br /&gt;
} &lt;br /&gt;
We can additionally specify what we want to happen if the condition is not fulfilled by using the keyword else. Its &lt;br /&gt;
form used in conjunction with if is:&amp;nbsp; &lt;br /&gt;
if (condition) statement1 else statement2 &lt;br /&gt;
For example:&amp;nbsp;&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
if (x == 100) &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is 100&quot;; &lt;br /&gt;
else &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is not 100&quot;; &lt;br /&gt;
prints on the screen x is 100 if indeed x has a value of 100, but if it has not -and only if not- it prints out x is &lt;br /&gt;
not 100.&amp;nbsp; &lt;br /&gt;
The if + else structures can be concatenated with the intention of verifying a range of values. The following &lt;br /&gt;
example shows its use telling if the value currently stored in x is positive, negative or none of them (i.e. zero):&amp;nbsp; &lt;br /&gt;
if (x &amp;gt; 0) &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is positive&quot;; &lt;br /&gt;
else if (x &amp;lt; 0) &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is negative&quot;; &lt;br /&gt;
else &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is 0&quot;; &lt;br /&gt;
Remember that in case that we want more than a single statement to be executed, we must group them in a block by enclosing them in braces { }.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Iteration structures (loops) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.&lt;br /&gt;
&lt;br /&gt;
&lt;u style=&quot;color: #20124d;&quot;&gt;&lt;b&gt;The while loop&amp;nbsp; &lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Its format is: &lt;br /&gt;
while (expression) statement &lt;br /&gt;
and its functionality is simply to repeat statement while the condition set in expression is true. &lt;br /&gt;
For example, we are going to make a program to countdown using a while-loop: &lt;br /&gt;
// custom countdown using while &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int n; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter the starting number &amp;gt; &quot;; &lt;br /&gt;
&amp;nbsp; cin &amp;gt;&amp;gt; n; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; while (n&amp;gt;0) { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; --n; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;FIRE!\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Enter the starting number &amp;gt; 8 &lt;br /&gt;
8, 7, 6, 5, 4, 3, 2, 1, FIRE! &lt;br /&gt;
When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop &lt;br /&gt;
begins, if the value entered by the user fulfills the condition n&amp;gt;0 (that n is greater than zero) the block that follows &lt;br /&gt;
the condition will be executed and repeated while the condition (n&amp;gt;0) remains being true. &lt;br /&gt;
The whole process of the previous program can be interpreted according to the following script (beginning in &lt;br /&gt;
main):&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
1.&amp;nbsp;&amp;nbsp;&amp;nbsp; User assigns a value to n&amp;nbsp; &lt;br /&gt;
2.&amp;nbsp;&amp;nbsp;&amp;nbsp; The while condition is checked (n&amp;gt;0). At this point there are two posibilities: &lt;br /&gt;
* condition is true: statement is executed (to step 3) &lt;br /&gt;
* condition is false: ignore statement and continue after it (to step 5)&amp;nbsp; &lt;br /&gt;
3.&amp;nbsp;&amp;nbsp;&amp;nbsp; Execute statement: &lt;br /&gt;
cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
--n; &lt;br /&gt;
(prints the value of n on the screen and decreases n by 1)&amp;nbsp; &lt;br /&gt;
4.&amp;nbsp;&amp;nbsp;&amp;nbsp; End of block. Return automatically to step 2&amp;nbsp; &lt;br /&gt;
5.&amp;nbsp;&amp;nbsp;&amp;nbsp; Continue the program right after the block: print FIRE! and end program.&amp;nbsp; &lt;br /&gt;
When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide &lt;br /&gt;
within the block some method to force the condition to become false at some point, otherwise the loop will &lt;br /&gt;
continue looping forever. In this case we have included --n; that decreases the value of the variable that is being &lt;br /&gt;
evaluated in the condition (n) by one - this will eventually make the condition (n&amp;gt;0) to become false after a certain &lt;br /&gt;
number of loop iterations: to be more specific, when n becomes 0, that is where our while-loop and our countdown &lt;br /&gt;
end.&amp;nbsp; &lt;br /&gt;
Of course this is such a simple action for our computer that the whole countdown is performed instantly without &lt;br /&gt;
any practical delay between numbers.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The do-while loop &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Its format is:&amp;nbsp; &lt;br /&gt;
do statement while (condition); &lt;br /&gt;
Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after &lt;br /&gt;
the execution of statement instead of before, granting at least one execution of statement even if condition is &lt;br /&gt;
never fulfilled. For example, the following example program echoes any number you enter until you enter 0.&amp;nbsp; &lt;br /&gt;
// number echoer &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; unsigned long n; &lt;br /&gt;
&amp;nbsp; do { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter number (0 to end): &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cin &amp;gt;&amp;gt; n; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;You entered: &quot; &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &quot;\n&quot;; &lt;br /&gt;
&amp;nbsp; } while (n != 0); &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Enter number (0 to end): 12345 &lt;br /&gt;
You entered: 12345 &lt;br /&gt;
Enter number (0 to end): 160277 &lt;br /&gt;
You entered: 160277 &lt;br /&gt;
Enter number (0 to end): 0 &lt;br /&gt;
You entered: 0 &lt;br /&gt;
The do-while loop is usually used when the condition that has to determine the end of the loop is determined within &lt;br /&gt;
the loop statement itself, like in the previous case, where the user input within the block is what is used to &lt;br /&gt;
determine if the loop has to end. In fact if you never enter the value 0 in the previous example you can be &lt;br /&gt;
prompted for more numbers forever.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The for loop&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Its format is:&amp;nbsp; &lt;br /&gt;
for (initialization; condition; increase) statement;&lt;br /&gt;
&lt;br /&gt;
and its main function is to repeat statement while condition remains true, like the while loop. But in addition, the &lt;br /&gt;
for loop provides specific locations to contain an initialization statement and an increase statement. So this &lt;br /&gt;
loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each &lt;br /&gt;
iteration.&amp;nbsp; &lt;br /&gt;
It works in the following way:&amp;nbsp; &lt;br /&gt;
1.&amp;nbsp;&amp;nbsp;&amp;nbsp; initialization is executed. Generally it is an initial value setting for a counter variable. This is executed &lt;br /&gt;
only once.&amp;nbsp; &lt;br /&gt;
2.&amp;nbsp;&amp;nbsp;&amp;nbsp; condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped &lt;br /&gt;
(not executed).&amp;nbsp; &lt;br /&gt;
3.&amp;nbsp;&amp;nbsp;&amp;nbsp; statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.&amp;nbsp; &lt;br /&gt;
4.&amp;nbsp;&amp;nbsp;&amp;nbsp; finally, whatever is specified in the increase field is executed and the loop gets back to step 2.&amp;nbsp; &lt;br /&gt;
Here is an example of countdown using a for loop:&amp;nbsp; &lt;br /&gt;
// countdown using a for loop &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; for (int n=10; n&amp;gt;0; n--) { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;FIRE!\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE! &lt;br /&gt;
The initialization and increase fields are optional. They can remain empty, but in all cases the semicolon signs &lt;br /&gt;
between them must be written. For example we could write: for (;n&amp;lt;10;) if we wanted to specify no initialization &lt;br /&gt;
and no increase; or for (;n&amp;lt;10;n++) if we wanted to include an increase field but no initialization (maybe because &lt;br /&gt;
the variable was already initialized before).&amp;nbsp; &lt;br /&gt;
Optionally, using the comma operator (,) we can specify more than one expression in any of the fields included in &lt;br /&gt;
a for loop, like in initialization, for example. The comma operator (,) is an expression separator, it serves to &lt;br /&gt;
separate more than one expression where only one is generally expected. For example, suppose that we wanted to &lt;br /&gt;
initialize more than one variable in our loop:&amp;nbsp; &lt;br /&gt;
for ( n=0, i=100 ; n!=i ; n++, i-- ) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp;&amp;nbsp; // whatever here... &lt;br /&gt;
} &lt;br /&gt;
This loop will execute for 50 times if neither n or i are modified within the loop:&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAloTwR8h5UB5Xx4uD2dY8uEpM06TfozasOElb7lhS10yQVXN8nuaMoVrWJI5IVhJsWdqsk9q2y-IowwV8_6NNOg01rY5Kzm5CPu_ylanKvYVb4cRybDG0tSIldD2cm5Xeuj2iVwDUw4eS/s1600/c%252B%252B%252B2.jpg&quot; /&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;n starts with a value of 0, and i with 100, the condition is n!=i (that n is not equal to i). Because n is increased by one and i decreased by one, the loop&#39;s condition will become false after the 50th loop, when both n and i will be equal to 50.&amp;nbsp;&amp;nbsp; &lt;/div&gt;&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Jump statements. &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;The break statement &lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite &lt;br /&gt;
loop, or to force it to end before its natural end. For example, we are going to stop the count down before its &lt;br /&gt;
natural end (maybe because of an engine check failure?):&amp;nbsp; &lt;br /&gt;
// break loop example &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int n; &lt;br /&gt;
&amp;nbsp; for (n=10; n&amp;gt;0; n--) &lt;br /&gt;
&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (n==3) &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;countdown aborted!&quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The continue statement &lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the &lt;br /&gt;
statement block had been reached, causing it to jump to the start of the following iteration. For example, we are &lt;br /&gt;
going to skip the number 5 in our countdown:&amp;nbsp; &lt;br /&gt;
// continue loop example &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; for (int n=10; n&amp;gt;0; n--) { &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (n==5) continue; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;FIRE!\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The goto statement&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
goto allows to make an absolute jump to another point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations. &lt;br /&gt;
The destination point is identified by a label, which is then used as an argument for the goto statement. A label is made of a valid identifier followed by a colon (:).&amp;nbsp; Generally speaking, this instruction has no concrete use in structured or object oriented programming aside from those that low-level programming fans may find for it. For example, here is our countdown loop using goto: &lt;br /&gt;
&lt;br /&gt;
// goto loop example &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int n=10; &lt;br /&gt;
&amp;nbsp; loop: &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &quot;, &quot;; &lt;br /&gt;
&amp;nbsp; n--; &lt;br /&gt;
&amp;nbsp; if (n&amp;gt;0) goto loop; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;FIRE!\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The exit function&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
exit is a function defined in the cstdlib library. &lt;br /&gt;
The purpose of exit is to terminate the current program with a specific exit code. Its prototype is: &lt;br /&gt;
void exit (int exitcode); The exitcode is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means that some error or unexpected results happened.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The selective structure: switch&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The syntax of the switch statement is a bit peculiar. Its objective is to check several possible constant values for an &lt;br /&gt;
expression. Something similar to what we did at the beginning of this section with the concatenation of several if &lt;br /&gt;
and else if instructions. Its form is the following:&amp;nbsp; &lt;br /&gt;
switch (expression) &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; case constant1: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; group of statements 1; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break; &lt;br /&gt;
&amp;nbsp; case constant2: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; group of statements 2; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break; &lt;br /&gt;
&amp;nbsp; . &lt;br /&gt;
&amp;nbsp; . &lt;br /&gt;
&amp;nbsp; . &lt;br /&gt;
&amp;nbsp; default: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default group of statements &lt;br /&gt;
} &lt;br /&gt;
It works in the following way: switch evaluates expression and checks if it is equivalent to constant1, if it is, it &lt;br /&gt;
executes group of statements 1 until it finds the break statement. When it finds this break statement the &lt;br /&gt;
program jumps to the end of the switch selective structure.&amp;nbsp; If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure.&amp;nbsp; Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default: label, if it exists (since it is optional).&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Both of the following code fragments have the same behavior:&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiflVeDPHIiEu2-TQgNog06RCayXibW-2KvlMLOF51rA1SxozuKczbyq9y8-AEb9efHIidsLdDvI3BURE54g79vZ4E5wobnLGI3V-QsrH7y7N_qT3S1K8yINPZYQX7IqHwIOJPAkXlkC_xN/s1600/c%252B%252Ba.jpg&quot; /&gt;&lt;/div&gt;&lt;br /&gt;
The switch statement is a bit peculiar within the C++ language because it uses labels instead of blocks. This &lt;br /&gt;
forces us to put break statements after the group of statements that we want to be executed for a specific &lt;br /&gt;
condition. Otherwise the remainder statements -including those corresponding to other labels- will also be &lt;br /&gt;
executed until the end of the switch selective block or a break statement is reached.&amp;nbsp; &lt;br /&gt;
For example, if we did not include a break statement after the first group for case one, the program will not &lt;br /&gt;
automatically jump to the end of the switch selective block and it would continue executing the rest of statements &lt;br /&gt;
until it reaches either a break instruction or the end of the switch selective block. This makes unnecessary to &lt;br /&gt;
include braces { } surrounding the statements for each of the cases, and it can also be useful to execute the same &lt;br /&gt;
block of instructions for different possible values for the expression being evaluated. For example:&amp;nbsp; &lt;br /&gt;
switch (x) { &lt;br /&gt;
&amp;nbsp; case 1: &lt;br /&gt;
&amp;nbsp; case 2: &lt;br /&gt;
&amp;nbsp; case 3: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is 1, 2 or 3&quot;; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; break; &lt;br /&gt;
&amp;nbsp; default: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; &quot;x is not 1, 2 nor 3&quot;; &lt;br /&gt;
&amp;nbsp; } &lt;br /&gt;
Notice that switch can only be used to compare an expression against constants. Therefore we cannot put variables as labels (for example case n: where n is a variable) or ranges (case (1..3):) because they are not valid C++ constants.&amp;nbsp; If you need to check ranges or values that are not constants, use a concatenation of if and else if statements. &lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/6565138071015841650/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/control-structures.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/6565138071015841650'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/6565138071015841650'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/control-structures.html' title='Control Structures'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAloTwR8h5UB5Xx4uD2dY8uEpM06TfozasOElb7lhS10yQVXN8nuaMoVrWJI5IVhJsWdqsk9q2y-IowwV8_6NNOg01rY5Kzm5CPu_ylanKvYVb4cRybDG0tSIldD2cm5Xeuj2iVwDUw4eS/s72-c/c%252B%252B%252B2.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7009649579956966078.post-8211150145385325808</id><published>2011-03-06T22:19:00.000-08:00</published><updated>2011-05-25T22:32:14.900-07:00</updated><title type='text'>Basic Input/Output</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Until now, the example programs of previous sections provided very little interaction with the user, if any at all. Using the standard input and output library, we will be able to interact with the user by printing messages on the screen and getting the user&#39;s input from the keyboard.&amp;nbsp; &lt;br /&gt;
C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen or the keyboard. A stream is an object where a program can either insert or extract characters to/from it. We do not really need to care about many specifications about the physical media associated with the stream - we only need to know it will accept or provide characters sequentially.&amp;nbsp; &lt;br /&gt;
The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Standard Output (cout) &lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout. &lt;br /&gt;
cout is used in conjunction with the insertion operator, which is written as &amp;lt;&amp;lt; (two &quot;less than&quot; signs). &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;Output sentence&quot;; // prints Output sentence on screen &lt;br /&gt;
cout &amp;lt;&amp;lt; 120;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints number 120 on screen &lt;br /&gt;
cout &amp;lt;&amp;lt; x;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints the content of x on screen &lt;br /&gt;
The &amp;lt;&amp;lt; operator inserts the data that follows it into the stream preceding it. In the examples above it inserted the &lt;br /&gt;
constant string Output sentence, the numerical constant 120 and variable x into the standard output stream cout. &lt;br /&gt;
Notice that the sentence in the first instruction is enclosed between double quotes (&quot;) because it is a constant &lt;br /&gt;
string of characters. Whenever we want to use constant strings of characters we must enclose them between &lt;br /&gt;
double quotes (&quot;) so that they can be clearly distinguished from variable names. For example, these two sentences &lt;br /&gt;
have very different results: &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;Hello&quot;;&amp;nbsp; // prints Hello &lt;br /&gt;
cout &amp;lt;&amp;lt; Hello;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prints the content of Hello variable &lt;br /&gt;
The insertion operator (&amp;lt;&amp;lt;) may be used more than once in a single statement: &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;Hello, &quot; &amp;lt;&amp;lt; &quot;I am &quot; &amp;lt;&amp;lt; &quot;a C++ statement&quot;; &lt;br /&gt;
This last statement would print the message Hello, I am a C++ statement on the screen. The utility of repeating &lt;br /&gt;
the insertion operator (&amp;lt;&amp;lt;) is demonstrated when we want to print out a combination of variables and constants or &lt;br /&gt;
more than one variable: &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;Hello, I am &quot; &amp;lt;&amp;lt; age &amp;lt;&amp;lt; &quot; years old and my zipcode is &quot; &amp;lt;&amp;lt; zipcode; &lt;br /&gt;
If we assume the age variable to contain the value 24 and the zipcode variable to contain 90064 the output of the previous statement would be:&amp;nbsp; Hello, I am 24 years old and my zipcode is 90064 &lt;br /&gt;
It is important to notice that cout does not add a line break after its output unless we explicitly indicate it, &lt;br /&gt;
therefore, the following statements:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;cout &amp;lt;&amp;lt; &quot;This is a sentence.&quot;; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;This is another sentence.&quot;; &lt;br /&gt;
will be shown on the screen one following the other without any line break between them:&amp;nbsp; &lt;br /&gt;
This is a sentence.This is another sentence.&amp;nbsp; &lt;br /&gt;
even though we had written them in two different insertions into cout. In order to perform a line break on the &lt;br /&gt;
output we must explicitly insert a new-line character into cout. In C++ a new-line character can be specified as \n &lt;br /&gt;
(backslash, n):&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;First sentence.\n &quot;; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;Second sentence.\nThird sentence.&quot;; &lt;br /&gt;
This produces the following output:&amp;nbsp; &lt;br /&gt;
First sentence. &lt;br /&gt;
Second sentence. &lt;br /&gt;
Third sentence. &lt;br /&gt;
Additionally, to add a new-line, you may also use the endl manipulator. For example:&amp;nbsp; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;First sentence.&quot; &amp;lt;&amp;lt; endl; &lt;br /&gt;
cout &amp;lt;&amp;lt; &quot;Second sentence.&quot; &amp;lt;&amp;lt; endl; &lt;br /&gt;
would print out:&amp;nbsp; &lt;br /&gt;
First sentence. &lt;br /&gt;
Second sentence.&amp;nbsp; &lt;br /&gt;
The endl manipulator produces a newline character, exactly as the insertion of &#39;\n&#39; does, but it also has an &lt;br /&gt;
additional behavior when it is used with buffered streams: the buffer is flushed. Anyway, cout will be an &lt;br /&gt;
unbuffered stream in most cases, so you can generally use both the \n escape character and the endl manipulator in order to specify a new line without any difference in its behavior.&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Standard Input (cin).&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the &lt;br /&gt;
overloaded operator of extraction (&amp;gt;&amp;gt;) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream. For example:&amp;nbsp; int age; cin &amp;gt;&amp;gt; age; &lt;br /&gt;
The first statement declares a variable of type int called age, and the second one waits for an input from cin (the keyboard) in order to store it in this integer variable.&amp;nbsp; &lt;br /&gt;
cin can only process the input from the keyboard once the RETURN key has been pressed. Therefore, even if you request a single character, the extraction from cin will not process the input until the user presses RETURN after the character has been introduced.&amp;nbsp; &lt;br /&gt;
You must always consider the type of the variable that you are using as a container with cin extractions. If you &lt;br /&gt;
request an integer you will get an integer, if you request a character you will get a character and if you request a string of characters you will get a string of characters.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt; &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
31 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
© cplusplus.com 2008. All rights reserved &lt;br /&gt;
// i/o example &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; int i; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Please enter an integer value: &quot;; &lt;br /&gt;
&amp;nbsp; cin &amp;gt;&amp;gt; i; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;The value you entered is &quot; &amp;lt;&amp;lt; i; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot; and its double is &quot; &amp;lt;&amp;lt; i*2 &amp;lt;&amp;lt; &quot;.\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Please enter an integer value: 702 &lt;br /&gt;
The value you entered is 702 and its double is &lt;br /&gt;
1404. &lt;br /&gt;
The user of a program may be one of the factors that generate errors even in the simplest programs that use cin &lt;br /&gt;
(like the one we have just seen). Since if you request an integer value and the user introduces a name (which &lt;br /&gt;
generally is a string of characters), the result may cause your program to misoperate since it is not what we were &lt;br /&gt;
expecting from the user. So when you use the data input provided by cin extractions you will have to trust that &lt;br /&gt;
the user of your program will be cooperative and that he/she will not introduce his/her name or something similar &lt;br /&gt;
when an integer value is requested. A little ahead, when we see the stringstream class we will see a possible &lt;br /&gt;
solution for the errors that can be caused by this type of user input.&amp;nbsp; &lt;br /&gt;
You can also use cin to request more than one datum input from the user:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
cin &amp;gt;&amp;gt; a &amp;gt;&amp;gt; b; &lt;br /&gt;
is equivalent to:&amp;nbsp; &lt;br /&gt;
cin &amp;gt;&amp;gt; a; &lt;br /&gt;
cin &amp;gt;&amp;gt; b; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
In both cases the user must give two data, one for variable a and another one for variable b that may be separated by any valid blank separator: a space, a tab character or a newline.&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;cin and strings&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;We can use cin to get strings with the extraction operator (&amp;gt;&amp;gt;) as we do with fundamental data type variables:&amp;nbsp; cin &amp;gt;&amp;gt; mystring; &lt;br /&gt;
However, as it has been said, cin extraction stops reading as soon as if finds any blank space character, so in this case we will be able to get just one word for each extraction. This behavior may or may not be what we want; for example if we want to get a sentence from the user, this extraction operation would not be useful.&amp;nbsp; &lt;br /&gt;
In order to get entire lines, we can use the function getline, which is the more recommendable way to get user &lt;br /&gt;
input with cin:&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;// cin with strings &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;string&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; string mystr; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;What&#39;s your name? &quot;; &lt;br /&gt;
&amp;nbsp; getline (cin, mystr); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Hello &quot; &amp;lt;&amp;lt; mystr &amp;lt;&amp;lt; &quot;.\n&quot;; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;What is your favorite team? &quot;; &lt;br /&gt;
&amp;nbsp; getline (cin, mystr); &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;I like &quot; &amp;lt;&amp;lt; mystr &amp;lt;&amp;lt; &quot; too!\n&quot;; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
What&#39;s your name? Juan SouliÃ‾Â¿Â½ &lt;br /&gt;
Hello Juan SouliÃ‾Â¿Â½. &lt;br /&gt;
What is your favorite team? The Isotopes &lt;br /&gt;
I like The Isotopes too! &lt;br /&gt;
Notice how in both calls to getline we used the same string identifier (mystr). What the program does in the &lt;br /&gt;
second call is simply to replace the previous content by the new one that is introduced.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;stringstream&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The standard header file &amp;lt;sstream&amp;gt; defines a class called stringstream that allows a string-based object to be &lt;br /&gt;
treated as a stream. This way we can perform extraction or insertion operations from/to strings, which is especially &lt;br /&gt;
useful to convert strings to numerical values and vice versa. For example, if we want to extract an integer from a &lt;br /&gt;
string we can write:&amp;nbsp; &lt;br /&gt;
string mystr (&quot;1204&quot;); &lt;br /&gt;
int myint; &lt;br /&gt;
stringstream(mystr) &amp;gt;&amp;gt; myint; &lt;br /&gt;
This declares a string object with a value of &quot;1204&quot;, and an int object. Then we use stringstream&#39;s constructor &lt;br /&gt;
to construct an object of this type from the string object. Because we can use stringstream objects as if they &lt;br /&gt;
were streams, we can extract an integer from it as we would have done on cin by applying the extractor operator &lt;br /&gt;
(&amp;gt;&amp;gt;) on it followed by a variable of type int.&amp;nbsp; &lt;br /&gt;
After this piece of code, the variable myint will contain the numerical value 1204.&amp;nbsp; &lt;br /&gt;
// stringstreams &lt;br /&gt;
#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;string&amp;gt; &lt;br /&gt;
#include &amp;lt;sstream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
&lt;br /&gt;
int main () &lt;br /&gt;
{ &lt;br /&gt;
&amp;nbsp; string mystr; &lt;br /&gt;
&amp;nbsp; float price=0; &lt;br /&gt;
&amp;nbsp; int quantity=0; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter price: &quot;; &lt;br /&gt;
&amp;nbsp; getline (cin,mystr); &lt;br /&gt;
&amp;nbsp; stringstream(mystr) &amp;gt;&amp;gt; price; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Enter quantity: &quot;; &lt;br /&gt;
&amp;nbsp; getline (cin,mystr); &lt;br /&gt;
&amp;nbsp; stringstream(mystr) &amp;gt;&amp;gt; quantity; &lt;br /&gt;
&amp;nbsp; cout &amp;lt;&amp;lt; &quot;Total price: &quot; &amp;lt;&amp;lt; price*quantity &amp;lt;&amp;lt; &lt;br /&gt;
endl; &lt;br /&gt;
&amp;nbsp; return 0; &lt;br /&gt;
} &lt;br /&gt;
Enter price: 22.25 &lt;br /&gt;
Enter quantity: 7 &lt;br /&gt;
Total price: 155.75 &lt;br /&gt;
In this example, we acquire numeric values from the standard input indirectly. Instead of extracting numeric values directly from the standard input, we get lines from the standard input (cin) into a string object (mystr), and then we extract the integer values from this string into a variable of type int (quantity).&amp;nbsp; &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Using this method, instead of direct extractions of integer values, we have more control over what happens with the input of numeric values from the user, since we are separating the process of obtaining input from the user (we now simply ask for lines) with the interpretation of that input. Therefore, this method is usually preferred to get numerical values from the user in all programs that are intensive in user input. &lt;/div&gt;&lt;div style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://onli9.blogspot.com/feeds/8211150145385325808/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://onli9.blogspot.com/2011/03/basic-inputoutput.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/8211150145385325808'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7009649579956966078/posts/default/8211150145385325808'/><link rel='alternate' type='text/html' href='http://onli9.blogspot.com/2011/03/basic-inputoutput.html' title='Basic Input/Output'/><author><name>Amit Upadhyay</name><uri>http://www.blogger.com/profile/11118944254541170953</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>