<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="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" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4347663708493617035</atom:id><lastBuildDate>Thu, 29 Aug 2024 04:01:43 +0000</lastBuildDate><title>C++ site</title><description>This is a C++ site</description><link>http://cppsite.blogspot.com/</link><managingEditor>noreply@blogger.com (Rachifu)</managingEditor><generator>Blogger</generator><openSearch:totalResults>47</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4929859175328121125</guid><pubDate>Thu, 12 Mar 2009 10:51:00 +0000</pubDate><atom:updated>2009-03-12T03:54:38.513-07:00</atom:updated><title>Free C++ Compilers</title><description>&lt;span style=&quot;font-size: 85%;&quot;&gt;I know there are a lot of C++ compilers availiable and the best free compiler I&#39;ve used is code:blocks. I know there is a quite a few compilers that could be better than code:blocks. But I would be unaware of it since I&#39;m happy with code:blocks. Although Code:BLocks has a friendly user interface it is a little bit more complicated to get everything running. A good compiler for a programmer who has good C++ knowledge.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;You can get code:blocks from this website&lt;/span&gt;&lt;br /&gt;&lt;a href=&quot;http://www.codeblocks.org/&quot;&gt;www.codeblocks.org/&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;Dev C++ is also a very good compiler and I have had very good results with this compiler and is why I recomend it for begginers to C++. It has a very easy and friendly user interface and is easy for anyone.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;You can get Dev C++ from there website which is located at&lt;/span&gt;&lt;br /&gt;&lt;a href=&quot;http://www.bloodshed.net/devcpp.html&quot;&gt;www.bloodshed.net/devcpp.html&lt;/a&gt;</description><link>http://cppsite.blogspot.com/2009/03/free-c-compilers.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-3238967485930446055</guid><pubDate>Sat, 07 Mar 2009 21:56:00 +0000</pubDate><atom:updated>2009-03-07T13:58:58.672-08:00</atom:updated><title>Stupid C++ Tricks</title><description>&lt;p&gt;I recently came across this &lt;a href=&quot;http://blog.cplusplus-soup.com/2009/03/fp-and-c-left-fold-fibonacci.html&quot; onclick=&quot;javascript:pageTracker._trackPageview(&#39;/outbound/article/blog.cplusplus-soup.com&#39;);&quot;&gt;C++ Soup&lt;/a&gt; post, which demonstrates some cute C++ tricks. Now by cute, I mean stupid, similar to Dave Letterman’s segment “stupid pet tricks” (you can google that if you’ve never seen this). It’s a useless language exercise with little practical application except to show how obfuscated one can make C++. Are there any benefit to these tricks? Lets explore.&lt;/p&gt;  &lt;p&gt;Now all C++ programmers do this (myself included) just to see if they understand template programming, so keep in mind that this is &lt;span class=&quot;caps&quot;&gt;NOT &lt;/span&gt;a critique of this particular author, who I’m sure is a C++ Ninja. It’s just a recent example that triggered this rant. No, just have a look at half the &lt;a href=&quot;http://boost.org/&quot; onclick=&quot;javascript:pageTracker._trackPageview(&#39;/outbound/article/boost.org&#39;);&quot;&gt;boost&lt;/a&gt; libraries to see this thinking taken to the extreme. Or even the C++ standard library; does anyone really use the cumbersome std::for_each function or any of that bind stuff? They all feel like they’re unfinished ideas: they’re onto something, but they’re not quite there yet.&lt;/p&gt;  &lt;p&gt;These (what I’m calling) C++ tricks are often segments of templated C++ code that push generic programming to solve some kind of basic problem, that is typically solved using simpler (but more verbose) code. A trick author is often amazed (and proud) at how small or flexible the code is, but hopefully realizes that the amount of work or expertise required to make the trick outweighs most benefits. The code is then shelved. Lets cut this silliness out.&lt;/p&gt;  &lt;p&gt;Furthermore, most application developers will steer clear (or be ignorant) of such tricks, and popular C++ libraries do well to not require such expertise from their users.&lt;/p&gt;  &lt;p&gt;Is there any benefit to these tricks? Yes, only to show what could be possible with C++.&lt;/p&gt;  &lt;p&gt;C++ is an extremely powerful language, giving you &lt;i&gt;compile time&lt;/i&gt; dynamic/generic programming with no loss of runtime performance. No other language does this so fully and completely, and C++ is still the only language option in many demanding areas.&lt;/p&gt;  &lt;p&gt;However, the language needs to grow, and it needs to grow fast. We must look at what these tricks are trying to do, and then think how we can change the language to do the same thing in a sane, logical fashion. C++0x is working on this with lambdas, type inference, better error reporting and some other niceties. But we needed these features yesterday, and get people using them. Lets steal more features from other languages. Lets turn up the meta programming capabilities so we can extend the language without meta-compilers and preprocessors, which until now provide the bulk of the useful new language extensions.&lt;/p&gt;  &lt;p&gt;There is no reason why C++ can’t offer easy to use and powerful compile-time dynamic programming, resulting in code that looks like a dynamic scripting language, but with safer types and kick-ass performance, memory use and code size.&lt;/p&gt;</description><link>http://cppsite.blogspot.com/2009/03/stupid-c-tricks.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-2318310197055314732</guid><pubDate>Mon, 02 Mar 2009 13:58:00 +0000</pubDate><atom:updated>2009-03-02T06:01:58.971-08:00</atom:updated><title>How to load the PNG image?</title><description>hmm, to load the PNG image, most people use libpng. But, wait.. But WAIT!!  That library is  “NOT SAFE FOR DIRECT USE”. The API is Awful, yeah.. It&#39;s Right.   If you don’t believe me, spend a day with it, struggle through it, THEN &lt;a href=&quot;http://openil.sourceforge.net/&quot;&gt;check out OpenIL&lt;/a&gt;. This is an example of how DIFFICULT using libpng directly is.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;#undef _UNICODE&lt;br /&gt;#include &quot;il.h&quot;&lt;br /&gt;&lt;br /&gt;#pragma comment( lib, &quot;DevIL.lib&quot; )&lt;br /&gt;&lt;br /&gt;// Wow. DevIL is amazing.&lt;br /&gt;&lt;br /&gt;// From http://gpwiki.org/index.php/DevIL:Tutorials:Basics&lt;br /&gt;&lt;br /&gt;// The library consists of three sub-libraries:&lt;br /&gt;//  * IL - main DevIL library. It allows you to load and save images to files. Every function in this library have &#39;il&#39; prefixed to their name.&lt;br /&gt;//  * ILU - this library contains functions for altering images. Every function in this library have &#39;ilu&#39; prefixed to their name.&lt;br /&gt;//  * ILUT - this library connects DevIL with OpenGL. Every function in this library have &#39;ilut&#39; prefixed to their name.&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; ilInit();&lt;br /&gt; printf(&quot;DevIL has been initialized\n&quot;);&lt;br /&gt;&lt;br /&gt; // Loading an image&lt;br /&gt; ILboolean result = ilLoadImage( &quot;4px.png&quot; ) ;&lt;br /&gt;&lt;br /&gt; if( result == true )&lt;br /&gt; {&lt;br /&gt;   printf(&quot;the image loaded successfully\n&quot;);&lt;br /&gt; }&lt;br /&gt; else&lt;br /&gt; {&lt;br /&gt;   printf(&quot;The image failed to load\n&quot; ) ;&lt;br /&gt;&lt;br /&gt;   ILenum err = ilGetError() ;&lt;br /&gt;   printf( &quot;the error %d\n&quot;, err );&lt;br /&gt;   printf( &quot;string is %s\n&quot;, ilGetString( err ) );&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; int size = ilGetInteger( IL_IMAGE_SIZE_OF_DATA ) ;&lt;br /&gt; printf(&quot;Data size:  %d\n&quot;, size );&lt;br /&gt; ILubyte * bytes = ilGetData() ;&lt;br /&gt;&lt;br /&gt; for( int i = 0 ; i &lt; size; i++ )&lt;br /&gt; {&lt;br /&gt;   // see we should see the byte data of the image now.&lt;br /&gt;   printf( &quot;%d\n&quot;, bytes[ i ] );&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;</description><link>http://cppsite.blogspot.com/2009/03/how-to-load-png-image.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4812301718410336713</guid><pubDate>Wed, 25 Feb 2009 13:21:00 +0000</pubDate><atom:updated>2009-02-25T05:24:42.722-08:00</atom:updated><title>Quick Tips On Using Whole Program Optimization</title><description>&lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;If you’re writing native C++ code, you can typically speed up your optimized code by about another 3-4% by adding the /GL flag to your compiles.&lt;span style=&quot;&quot;&gt;  &lt;/span&gt;This flag tells the compiler to defer code generation until you link your program. Then at link time the linker calls back to the compiler to finish compilation. If you compile all your sources this way, the compiler optimizes your program as a whole rather than one source file at a time. For users building with the IDE, this option is found under the C/C++ optimization settings, and is already on by default for retail builds in new projects.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;o:p&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; &lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;Using Whole Program Optimization provides the optimizer with a number of extra optimization opportunities, but I’ll give just one example. Many people are already familiar with the benefits of inlining a called function into the caller. We can only do inlining when we are generating code for both the calling function and the called function at the same time. With Link Time Code Gen we can inline functions from one source file into callers defined in another source file, as long as both source files were compiled with /GL. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;o:p&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; &lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;If you do use /GL, here are four caveats to keep in mind:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;o:p&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; &lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;&quot;&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt;1.&lt;/span&gt;&lt;span style=&quot;font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal;font-family:&#39;Times New Roman&#39;;font-size:7;&quot;  &gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;When building from the command line or via makefiles, you need to add the /LTCG switch to the link command line to tell the linker to expect to see one or more object files that were compiled with /GL. If you don’t, some build time will be wasted because the linker will have to start over when it gets to the module compiled with /GL. If you build through the IDE this is in your project configuration settings on the Linker optimization page.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;&quot;&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt;2.&lt;/span&gt;&lt;span style=&quot;font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal;font-family:&#39;Times New Roman&#39;;font-size:7;&quot;  &gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;Using /GL reduces your compile times, but your link time will increase, because work is being moved to during the link. Overall build time might increase a little, but shouldn’t increase a lot.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;&quot;&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt;3.&lt;/span&gt;&lt;span style=&quot;font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal;font-family:&#39;Times New Roman&#39;;font-size:7;&quot;  &gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;Don’t compile managed code with /GL. Link time code gen provides little or no benefit to managed code, and this option combination (/GL /clr) is being removed in the next compiler release, so you can future-proof your build by using link time code generation only for native code. If you’re building managed code using the IDE, the default setting is to use /GL in release builds, and I recommend you disable it for managed code. For mixed managed and native code, compiling only the native code with /GL and linking with /LTCG gives best results.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;&quot;&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt;4.&lt;/span&gt;&lt;span style=&quot;font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-size-adjust: none; font-stretch: normal;font-family:&#39;Times New Roman&#39;;font-size:7;&quot;  &gt;       &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;Never use /GL for code you intend to put in a library and ship to your customers. Doing so means that your customers will be doing the code gen for your library when they link their application. Since some of your customers could have different versions of the compiler, shipping a lib built this way could cause various maintenance problems for you. If your customer’s compiler is from a prior release, their link may fail. If their version is newer than yours, the code they generate won’t be exactly equal to what you’ve tested, and could behave differently for them than when you tested it. In VS 2008, the IDE default for the class library template release configuration is to build using /GL, and I strongly encourage everyone to reset that.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;o:p&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; &lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;Here are links for more information on this topic:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;o:p&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; &lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; text-indent: 35.4pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/0zza0de8.aspx&quot;&gt;&lt;span style=&quot;font-family:&#39;Times New Roman&#39;,&#39;serif&#39;;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 255);font-size:100%;&quot; &gt;The /GL compiler switch&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; (&lt;/span&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/0zza0de8.aspx&quot;&gt;&lt;span style=&quot;font-family:&#39;Times New Roman&#39;,&#39;serif&#39;;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 255);font-size:100%;&quot; &gt;http://msdn.microsoft.com/en-us/library/0zza0de8.aspx&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;&quot;&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt;            &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/xbf3tbeh.aspx&quot;&gt;&lt;span style=&quot;font-family:&#39;Times New Roman&#39;,&#39;serif&#39;;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 255);font-size:100%;&quot; &gt;The /LTCG linker switch&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; (&lt;/span&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/xbf3tbeh.aspx&quot;&gt;&lt;span style=&quot;font-family:&#39;Times New Roman&#39;,&#39;serif&#39;;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 255);font-size:100%;&quot; &gt;http://msdn.microsoft.com/en-us/library/xbf3tbeh.aspx&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: rgb(31, 73, 125);&quot;&gt;&lt;span style=&quot;&quot;&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt;            &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/magazine/cc301698.aspx&quot;&gt;&lt;span style=&quot;font-family:&#39;Times New Roman&#39;,&#39;serif&#39;;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 255);font-size:100%;&quot; &gt;A detailed article about Link Time Code Generation&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; (&lt;/span&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/magazine/cc301698.aspx&quot;&gt;&lt;span style=&quot;font-family:&#39;Times New Roman&#39;,&#39;serif&#39;;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 255);font-size:100%;&quot; &gt;http://msdn.microsoft.com/en-us/magazine/cc301698.aspx&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-family:Calibri;&quot;&gt;) &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in 0in 0pt; color: rgb(0, 0, 0);&quot;&gt;&lt;o:p&gt;&lt;span style=&quot;;font-family:Calibri;font-size:100%;&quot;  &gt; &lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;</description><link>http://cppsite.blogspot.com/2009/02/quick-tips-on-using-whole-program.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-2378073828184864619</guid><pubDate>Sat, 14 Feb 2009 11:00:00 +0000</pubDate><atom:updated>2009-02-19T01:15:59.634-08:00</atom:updated><title>Just simple guess game</title><description>&lt;a href=&quot;http://www.free-submit.co.cc/&quot;&gt;Free-submit.co.cc&lt;/a&gt; - FREE SUBMIT YOUR BLOG AND GET HIGH TRAFFIC&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In this posting, I will show you about a simple guess game.&lt;br /&gt;This is the code.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;#include &lt;iostream.h&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   int myBirthday = 13;&lt;br /&gt;   int guess;&lt;br /&gt;&lt;br /&gt;   cout &lt;&lt; &quot;Please guess the day of my birth, from 1 to 31&quot;&lt;&lt;endl; cout=&quot;&quot;&gt;&lt;&lt; &quot;Enter your guess please&quot;;     cin &gt;&gt; guess;&lt;br /&gt;&lt;br /&gt;   if (guess &lt; else=&quot;&quot; if=&quot;&quot; guess=&quot;&quot;&gt;31)&lt;br /&gt;   {&lt;br /&gt;       cout &lt;&lt; &quot;Pretty long month, genius!!&quot; &lt;&lt;endl; else=&quot;&quot; if=&quot;&quot; guess=&quot;=&quot; cout=&quot;&quot;&gt;&lt;&lt; &quot;Incredible,, you are right. Congratulations..&quot;&lt;&lt;endl; else=&quot;&quot; if=&quot;&quot; guess=&quot;&quot;&gt;&lt; else=&quot;&quot; cout=&quot;&quot;&gt;&lt;&lt; &quot;Lower&quot; &lt;&lt;&gt;&lt;/endl;&gt;&lt;/endl;&gt;&lt;/endl;&gt;&lt;/endl;&gt;&lt;/iostream.h&gt;&lt;/blockquote&gt;</description><link>http://cppsite.blogspot.com/2009/02/just-simple-guess-game.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-7256611162721327280</guid><pubDate>Thu, 12 Feb 2009 06:07:00 +0000</pubDate><atom:updated>2009-02-11T22:23:54.843-08:00</atom:updated><title>Essentials of System Analysis and Design or Expert C Programming</title><description>&lt;h4&gt;Expert C Programming &lt;/h4&gt; &lt;p&gt;Author: &lt;strong&gt;Peter van der Linden&lt;/strong&gt; &lt;/p&gt;&lt;p&gt;This is a very different book on the C language! In an easy, conversational style, Peter van der Linden, of Sun&#39;s compiler and OS kernel group, presents dozens of astonishing examples drawn from practical experience, including:&lt;br /&gt;&lt;br /&gt;• Software that blew up the space probe to Venus&lt;br /&gt;• The C bug that shut down the entire AT&amp;amp;T phone system&lt;br /&gt;• C programmer job interview secrets&lt;br /&gt;• Why programmers can&#39;t tell Halloween from Christmas day&lt;br /&gt;• The C code for a complete BASIC interpreter&lt;/p&gt;&lt;p&gt;&lt;i&gt;Expert C Programming&lt;/i&gt; reveals the coding techniques used by the best C programmers. It relates C to other languages, and includes an introduction to C++ that can be understood by an programmer without weeks of mind-bending study. Covering both the IBM PC and UNIX systems, it is an entertaining and educational romp through C showing how experts really use it. &lt;i&gt;Expert C Programming&lt;/i&gt; is a must read for anyone who wants to learn more about the implementation, practical use, and folklore of C.&lt;/p&gt;&lt;blockquote&gt;&quot;Not just clearly written, but fun to read. The tone and style of this text should make this a popular book with professional programmers. However, the tone of this book will make it very popular with undergraduates. Appendix A alone would make the purchase of this book a must. It&#39;s filled with great advice.&quot;&lt;/blockquote&gt;&lt;p&gt;—Professor Jack Beidler, Chairman, Department of Computer Science, University of Scranton&lt;/p&gt;&lt;blockquote&gt;&quot;So that&#39;s why extern char *cp isn&#39;t the same as extern char cp. I knew that it didn&#39;t work despite their superficial equivalence, but I didn&#39;t know why. I also love the job interview test questions on C.&quot;&lt;/blockquote&gt;&lt;p&gt;—David S.Platt, Rolling Thunder Computing&lt;/p&gt;&lt;blockquote&gt;&quot;In Expert C Programming, Peter van der Linden combines C language expertise and a subtle sense of humor to deliver a C programming book that stands out from the pack. In a genre too often known for windy, lifeless prose, van der Linden&#39;s crisp language, tongue-in-cheek attitude, and real-world examples engage and instruct.&quot;&lt;/blockquote&gt;&lt;p&gt;—John Barry, author of &lt;i&gt;Sunburst&lt;/i&gt;, &lt;i&gt;Technobabble&lt;/i&gt;, and other books &lt;/p&gt;&lt;br /&gt;&lt;br /&gt; &lt;h5&gt;Table of Contents:&lt;/h5&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Preface&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Acknowledgments&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Introduction&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;1&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;C Through the Mists of Time&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;2&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;It&#39;s Not a Bug, It&#39;s a Language Feature&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;31&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;3&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Unscrambling Declarations in C&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;63&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;4&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;The Shocking Truth: C Arrays and Pointers Are NOT the Same!&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;95&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;5&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Thinking of Linking&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;109&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;6&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Poetry in Motion: Runtime Data Structures&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;137&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;7&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Thanks for the Memory&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;165&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;8&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Why Programmers Can&#39;t Tell Halloween from Christmas Day&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;201&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;9&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;More about Arrays&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;239&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;10&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;More About Pointers&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;263&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;11&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;You Know C, So C++ is Easy!&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;293&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Appendix: Secrets of Programmer Job Interviews&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;333&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width=&quot;20%&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td width=&quot;70%&quot;&gt;Index&lt;/td&gt;&lt;td align=&quot;right&quot; width=&quot;10%&quot;&gt;349&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description><link>http://cppsite.blogspot.com/2009/02/essentials-of-system-analysis-and.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-6738099553648384844</guid><pubDate>Tue, 10 Feb 2009 11:09:00 +0000</pubDate><atom:updated>2009-02-10T03:11:14.333-08:00</atom:updated><title>Nested Classes</title><description>Classes can be defined inside other classes. Classes that are defined inside other classes are called &lt;a name=&quot;an2201&quot;&gt;&lt;/a&gt;  &lt;em&gt;nested classes&lt;/em&gt;. Nested classes are used in situations where the nested class has a close conceptual relationship to its surrounding class. For example, with the class &lt;code&gt;string&lt;/code&gt; a type &lt;a name=&quot;an2202&quot;&gt;&lt;/a&gt; &lt;code&gt;string::iterator&lt;/code&gt; is available which will provide all characters that are stored in the &lt;code&gt;string&lt;/code&gt;. This &lt;code&gt;string::iterator&lt;/code&gt; type could be defined as an object &lt;a name=&quot;an2203&quot;&gt;&lt;/a&gt; &lt;code&gt;iterator&lt;/code&gt;, defined as nested class in the class &lt;code&gt;string&lt;/code&gt;. &lt;p&gt; A class can be nested in every part of the surrounding class: in the &lt;code&gt;public, protected&lt;/code&gt; or &lt;code&gt;private&lt;/code&gt; section. Such a nested class can be considered a member &lt;a name=&quot;an2204&quot;&gt;&lt;/a&gt;  of the surrounding class. The normal access and  rules in classes apply to nested classes. If a class is nested in the &lt;code&gt;public&lt;/code&gt; section of a class, it is     &lt;a name=&quot;an2205&quot;&gt;&lt;/a&gt;  visible outside the surrounding class. If it is nested in the &lt;code&gt;protected&lt;/code&gt; section it is visible in subclasses, derived from the surrounding class , if it is nested in the &lt;code&gt;private&lt;/code&gt; section, it is only visible for the members of the surrounding class. &lt;/p&gt;&lt;p&gt; The surrounding class has no special privileges with respect to the nested class. So, the nested class still has full control over the accessibility of its members by the surrounding class. For example, consider the following class definition:         &lt;/p&gt;&lt;pre&gt;    class Surround&lt;br /&gt;   {&lt;br /&gt;       public:&lt;br /&gt;           class FirstWithin&lt;br /&gt;           {&lt;br /&gt;               int d_variable;&lt;br /&gt;&lt;br /&gt;               public:&lt;br /&gt;                   FirstWithin();&lt;br /&gt;                   int var() const;&lt;br /&gt;           };&lt;br /&gt;       private:&lt;br /&gt;           class SecondWithin&lt;br /&gt;           {&lt;br /&gt;               int d_variable;&lt;br /&gt;&lt;br /&gt;               public:&lt;br /&gt;                   SecondWithin();&lt;br /&gt;                   int var() const;&lt;br /&gt;           };&lt;br /&gt;   };&lt;br /&gt;   inline int Surround::FirstWithin::var() const&lt;br /&gt;   {&lt;br /&gt;       return d_variable;&lt;br /&gt;   }&lt;br /&gt;   inline int Surround::SecondWithin::var() const&lt;br /&gt;   {&lt;br /&gt;       return d_variable;&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;     In this definition access to the members is defined as follows:         &lt;ul&gt;&lt;li&gt; The class &lt;code&gt;FirstWithin&lt;/code&gt; is visible both outside and inside &lt;code&gt;Surround&lt;/code&gt;. The class &lt;code&gt;FirstWithin&lt;/code&gt; therefore has  global scope.         &lt;/li&gt;&lt;li&gt; The constructor &lt;code&gt;FirstWithin()&lt;/code&gt; and the member function &lt;code&gt;var()&lt;/code&gt; of the class &lt;code&gt;FirstWithin&lt;/code&gt; are also globally visible.         &lt;/li&gt;&lt;li&gt; The &lt;code&gt;int d_variable&lt;/code&gt; datamember is only visible to the members of the class &lt;code&gt;FirstWithin&lt;/code&gt;. Neither the members of &lt;code&gt;Surround&lt;/code&gt; nor the members of &lt;code&gt;SecondWithin&lt;/code&gt; can access &lt;code&gt;d_variable&lt;/code&gt; of the class &lt;code&gt;FirstWithin&lt;/code&gt; directly.         &lt;/li&gt;&lt;li&gt; The class &lt;code&gt;SecondWithin&lt;/code&gt; is only visible inside &lt;code&gt;Surround&lt;/code&gt;. The public members of the class &lt;code&gt;SecondWithin&lt;/code&gt; can also be used by the members of the class &lt;code&gt;FirstWithin&lt;/code&gt;, as nested classes can be considered members of their surrounding class.         &lt;/li&gt;&lt;li&gt; The constructor &lt;code&gt;SecondWithin()&lt;/code&gt; and the member function &lt;code&gt;var()&lt;/code&gt; of the class &lt;code&gt;SecondWithin&lt;/code&gt; can also only be reached by the members of &lt;code&gt;Surround&lt;/code&gt; (and by the members of its nested classes).         &lt;/li&gt;&lt;li&gt; The &lt;code&gt;int d_variable&lt;/code&gt; datamember of the class &lt;code&gt;SecondWithin&lt;/code&gt; is only visible to the members of the class &lt;code&gt;SecondWithin&lt;/code&gt;. Neither the members of &lt;code&gt;Surround&lt;/code&gt; nor the members of &lt;code&gt;FirstWithin&lt;/code&gt; can access &lt;code&gt;d_variable&lt;/code&gt; of the class &lt;code&gt;SecondWithin&lt;/code&gt; directly.         &lt;/li&gt;&lt;li&gt; As always, an object of the class type is required before its members can be called. This also holds true for nested classes.         &lt;/li&gt;&lt;/ul&gt;     If the surrounding class should have access rights to the private members of its nested classes or if nested classes should have access rights to the private members of the surrounding class, the classes can be defined as &lt;code&gt;friend&lt;/code&gt; classes. &lt;p&gt; The nested classes can be considered members of the surrounding class, but the &lt;a name=&quot;an2206&quot;&gt;&lt;/a&gt; members of nested classes are &lt;em&gt;not&lt;/em&gt; members of the surrounding class. So, a member of the class &lt;code&gt;Surround&lt;/code&gt; may not access &lt;code&gt;FirstWithin::var()&lt;/code&gt; directly. This is understandable considering the fact that a &lt;code&gt;Surround&lt;/code&gt; object is not also a &lt;code&gt;FirstWithin&lt;/code&gt; or &lt;code&gt;SecondWithin&lt;/code&gt; object. In fact, nested classes are just typenames. It is not implied that objects of such classes automatically exist in the surrounding class. If a member of the surrounding class should use a (non-static) member of a nested class then the surrounding class must define a nested class object, which can thereupon be used by the members of the surrounding class to use members of the nested class. &lt;/p&gt;&lt;p&gt; For example, in the following class definition there is a surrounding class &lt;code&gt;Outer&lt;/code&gt; and a nested class &lt;code&gt;Inner&lt;/code&gt;. The class &lt;code&gt;Outer&lt;/code&gt; contains a member function &lt;code&gt;caller()&lt;/code&gt; which uses the &lt;code&gt;inner&lt;/code&gt; object that is composed in &lt;code&gt;Outer&lt;/code&gt; to call the &lt;code&gt;infunction()&lt;/code&gt; member function of &lt;code&gt;Inner&lt;/code&gt;:         &lt;/p&gt;&lt;pre&gt;    class Outer&lt;br /&gt;   {&lt;br /&gt;       public:&lt;br /&gt;           void caller();&lt;br /&gt;&lt;br /&gt;       private:&lt;br /&gt;           class Inner&lt;br /&gt;           {&lt;br /&gt;               public:&lt;br /&gt;                   void infunction();&lt;br /&gt;           };&lt;br /&gt;           Inner d_inner;      // class Inner must be known&lt;br /&gt;   };&lt;br /&gt;   void Outer::caller()&lt;br /&gt;   {&lt;br /&gt;       d_inner.infunction();&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;     The mentioned function &lt;code&gt;Inner::infunction()&lt;/code&gt; can be called as part of the inline definition of &lt;code&gt;Outer::caller()&lt;/code&gt;, even though the definition of the class &lt;code&gt;Inner&lt;/code&gt; is yet to be seen by the compiler.  On the other hand, the compiler must have seen the definition of the class &lt;code&gt;Inner&lt;/code&gt; before a data member of that class can be defined.</description><link>http://cppsite.blogspot.com/2009/02/nested-classes.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4346942143696106772</guid><pubDate>Sun, 08 Feb 2009 04:39:00 +0000</pubDate><atom:updated>2009-02-07T21:12:26.939-08:00</atom:updated><title>Intel releases updated concurrent C/C++ tools</title><description>&lt;a href=&quot;http://www.eetindia.co.in/ART_8800389721_1800004_NT_1a5fb6de.HTM&quot; title=&quot;Xilinx says low-cost PCI Express solution compliant&quot;&gt;Intel&lt;/a&gt; Corp. has announced version 0.3.0 of its Concurrent Collections for C/C++, downloadable from &lt;a href=&quot;http://software.intel.com/en-us/articles/pre-release-license-agreement-for-intel-concurrent-collections-for-cc-accept-end-user-license-agreement-and-download/&quot; target=&quot;_blank&quot;&gt;the company&#39;s site&lt;/a&gt;.&lt;p&gt;Among the new and updated features Release 0.3.0 includes are: steps that can now be specified with different priorities to improve performance; support for garbage collection by ref-counting to reduce memory usage.&lt;/p&gt;&lt;p&gt;The collections provide the tools for constructing &lt;a href=&quot;http://www.eetindia.co.in/SEARCH/ART/C%26%2343%3B%26%2343%3B+programs.HTM&quot;&gt;&lt;span class=&quot;maintext&quot;&gt;C++ programs&lt;/span&gt;&lt;/a&gt; that execute in parallel while allowing application developers to ignore issues of &lt;a href=&quot;http://www.eetindia.co.in/SEARCH/ART/parallelism.HTM&quot;&gt;&lt;span class=&quot;maintext&quot;&gt;parallelism&lt;/span&gt;&lt;/a&gt; such as low-level threading constructs or the scheduling and distribution of computations.&lt;/p&gt;&lt;p&gt;The model lets programmers specify high-level computational steps including inputs and outputs without imposing unnecessary ordering on their execution. Code within the computational steps is written using standard serial constructs of the &lt;a href=&quot;http://www.eetindia.co.in/SEARCH/ART/C%26%2343%3B%26%2343%3B+language.HTM&quot;&gt;&lt;span class=&quot;maintext&quot;&gt;C++ language&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Data is either local to a computational step or it is explicitly produced and consumed by them. An application in this programming model supports multiple styles of parallelism (e.g., data, task, pipeline parallel).&lt;/p&gt;&lt;p&gt;While the interface between the computational steps and the runtime system remains unchanged, a wide range of runtime systems may target different architectures (e.g., shared memory, distributed) or support different scheduling methodologies (e.g., static or dynamic).&lt;/p&gt;&lt;p&gt;Intel is also providing a runtime system for shared memory systems that supports parallel execution although it is not yet highly optimised.&lt;/p&gt;</description><link>http://cppsite.blogspot.com/2009/02/intel-releases-updated-concurrent-cc.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-7807695565704433820</guid><pubDate>Sat, 07 Feb 2009 13:09:00 +0000</pubDate><atom:updated>2009-02-07T05:09:59.739-08:00</atom:updated><title>The birth of C++</title><description>&lt;p&gt;In the coming two years after C was released, the code written in C began to suffer from a problem. As the programmers harnessed the power of Personal Computer, the software grew more complex, and the C programs have gotten longer and longer. This meant that these programs were also harder to maintain. Some functions needed to communicate with other functions, but there was no standard way of thinking about data organization. In long programs, the code was practically infested with functions in the global scope. One of the few ways to separate data was by using separate C header (.h) and C source (.c) files. Other than that, the language&#39;s features that would help organize the code to ensure readability of large programs were simply missing.&lt;/p&gt;&lt;p&gt;There had to be a solution. In 1983, Bjarne Stroustrup developed such a solution, and named it the C++ programming language. He drew inspiration from some of the previous languages that allowed object-oriented programming. One of those languages was Simula67. It can be said that one of the greatest addition to C&#39;s supset, the C++ language, was that C++ introduced programmers to the concept of an &lt;i&gt;object&lt;/i&gt;. An object is an abstraction of a problem. Not only does the use of the concept of objects in a programming language reduce the size of the program, it also makes the program much more readable and compact. The notion of objects gave the origin to the concept of Object-Oriented programming which I discuss in my &lt;a href=&quot;http://www.authenticsociety.com/blog/ObjectOrientedProgramming&quot;&gt;Introduction to Object-Oriented Programming&lt;/a&gt;.&lt;/p&gt;</description><link>http://cppsite.blogspot.com/2009/02/birth-of-c.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-1161051678240953962</guid><pubDate>Sat, 07 Feb 2009 12:54:00 +0000</pubDate><atom:updated>2009-02-07T05:00:52.359-08:00</atom:updated><title>Why was it named C++?</title><description>&lt;p&gt;Initially the C++ language was named &quot;C with Classes&quot; because you could still write C-like programs in C++ but you could also use what we call &lt;i&gt;classes&lt;/i&gt;. I will be sure to talk about classes and other object-oriented C++ concepts in the following tutorials. You might ask, why a successor to C is called C++ and what is this &lt;i&gt;double plus sign&lt;/i&gt; business all about? If you were writing a program in C and you wanted to add two numbers together such as &lt;i style=&quot;color: rgb(0, 0, 255);&quot;&gt;1 + 1&lt;/i&gt;, in the listing (or &lt;i&gt;source code&lt;/i&gt;) of your program, you would write it just that way as: &lt;i style=&quot;color: rgb(0, 0, 255);&quot;&gt;1 + 1&lt;/i&gt;. Notice that in computer programming, the plus sign is called an &lt;i&gt;operator&lt;/i&gt;, it directs the compiler to add two values together. Plenty of times programmers need to add the digit 1 to another digit stored in what is called a &lt;i&gt;variable&lt;/i&gt;. Because of the frequent use of these simple arithmetics in general computer programs, a new operator called &lt;i&gt;postfix increment&lt;/i&gt; (represented by a double plus sequence: ++) may be used to simply add 1 to the existing value, thus avoiding the redundancy of typing: 1 + 1. It just makes the program code look cleaner and easier to read. Therefore, you can see that the expression C++ actually has a meaning. It is C incremented by 1, or in other words, a version of C programming language that contains additional features. You may also think of C++ as an augmented version of C.&lt;/p&gt;&lt;p&gt;On the other hand, the reason the C programming language is called C, is that it is a successor to the language called B. Simple isn&#39;t it? Strange as it is, there was never a programming language called A. Or as far as we know, if there was a language called A, it never saw the light of success or popularity. If you are interested in learning about how C++ falls into the history of computer programming languages, you may want to look at the &lt;a href=&quot;http://authenticsociety.com/Images/ComputerLanguages/EvolutionOfComputerlanguages.png&quot; target=&quot;_blank&quot;&gt;chart of evolution of computer languages&lt;/a&gt;. This hierarchy shows where each computer language came from. As you can see the Assembly Language (which is a low-level programming language) on this chart is located in the upper left corner because it is one of the first languages ever designed and can be considered the grand-father of all computer programming languages. Additionally you may be able to gain an insight from the &lt;a href=&quot;http://www.scriptol.org/chronology-programming-languages.html&quot;&gt;list of programming languages by date&lt;/a&gt; that lists languages in chronological order.&lt;/p&gt;</description><link>http://cppsite.blogspot.com/2009/02/why-was-it-named-c.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4733334961682172122</guid><pubDate>Sat, 31 Jan 2009 10:18:00 +0000</pubDate><atom:updated>2009-01-31T02:18:19.906-08:00</atom:updated><title>Standard C Date &amp; Time</title><description>&lt;table class=&quot;inline&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;row0&quot;&gt;&lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/asctime&quot; class=&quot;wikilink1&quot; title=&quot;c:date:asctime&quot;&gt;asctime&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;a textual version of the time&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row1&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/clock&quot; class=&quot;wikilink1&quot; title=&quot;c:date:clock&quot;&gt;clock&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the amount of time that the program has been running&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row2&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/ctime&quot; class=&quot;wikilink1&quot; title=&quot;c:date:ctime&quot;&gt;ctime&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns a specifically formatted version of the time&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row3&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/difftime&quot; class=&quot;wikilink1&quot; title=&quot;c:date:difftime&quot;&gt;difftime&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;the difference between two times&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row4&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/gmtime&quot; class=&quot;wikilink1&quot; title=&quot;c:date:gmtime&quot;&gt;gmtime&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns a pointer to the current Greenwich Mean Time&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row5&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/localtime&quot; class=&quot;wikilink1&quot; title=&quot;c:date:localtime&quot;&gt;localtime&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns a pointer to the current time&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row6&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/mktime&quot; class=&quot;wikilink1&quot; title=&quot;c:date:mktime&quot;&gt;mktime&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the calendar version of a given time&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row7&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/setlocale&quot; class=&quot;wikilink1&quot; title=&quot;c:date:setlocale&quot;&gt;setlocale&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;sets the current locale&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row8&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/strftime&quot; class=&quot;wikilink1&quot; title=&quot;c:date:strftime&quot;&gt;strftime&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns individual elements of the date and time&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row9&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/date/time&quot; class=&quot;wikilink1&quot; title=&quot;c:date:time&quot;&gt;time&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the current calendar time of the system&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description><link>http://cppsite.blogspot.com/2009/01/standard-c-date-time.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-3598114258990842002</guid><pubDate>Sat, 31 Jan 2009 10:13:00 +0000</pubDate><atom:updated>2009-01-31T02:15:10.822-08:00</atom:updated><title>Standard C Math</title><description>&lt;table class=&quot;inline&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;row0&quot;&gt;&lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/abs&quot; class=&quot;wikilink1&quot; title=&quot;c:math:abs&quot;&gt;abs&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;absolute value&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row1&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/acos&quot; class=&quot;wikilink1&quot; title=&quot;c:math:acos&quot;&gt;acos&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;arc cosine&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row2&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/asin&quot; class=&quot;wikilink1&quot; title=&quot;c:math:asin&quot;&gt;asin&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;arc sine&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row3&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/atan&quot; class=&quot;wikilink1&quot; title=&quot;c:math:atan&quot;&gt;atan&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;arc tangent&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row4&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/atan2&quot; class=&quot;wikilink1&quot; title=&quot;c:math:atan2&quot;&gt;atan2&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;arc tangent, using signs to determine quadrants&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row5&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/ceil&quot; class=&quot;wikilink1&quot; title=&quot;c:math:ceil&quot;&gt;ceil&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;the smallest integer not less than a certain value&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row6&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/cos&quot; class=&quot;wikilink1&quot; title=&quot;c:math:cos&quot;&gt;cos&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;cosine&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row7&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/cosh&quot; class=&quot;wikilink1&quot; title=&quot;c:math:cosh&quot;&gt;cosh&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;hyperbolic cosine&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row8&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/div&quot; class=&quot;wikilink1&quot; title=&quot;c:math:div&quot;&gt;div&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the quotient and remainder of a division&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row9&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/exp&quot; class=&quot;wikilink1&quot; title=&quot;c:math:exp&quot;&gt;exp&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns “e” raised to a given power&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row10&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/fabs&quot; class=&quot;wikilink1&quot; title=&quot;c:math:fabs&quot;&gt;fabs&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;absolute value for floating-point numbers&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row11&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/floor&quot; class=&quot;wikilink1&quot; title=&quot;c:math:floor&quot;&gt;floor&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the largest integer not greater than a given value&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row12&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/fmod&quot; class=&quot;wikilink1&quot; title=&quot;c:math:fmod&quot;&gt;fmod&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the remainder of a division&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row13&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/frexp&quot; class=&quot;wikilink1&quot; title=&quot;c:math:frexp&quot;&gt;frexp&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;decomposes a number into scientific notation&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row14&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/labs&quot; class=&quot;wikilink1&quot; title=&quot;c:math:labs&quot;&gt;labs&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;absolute value for long integers&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row15&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/ldexp&quot; class=&quot;wikilink1&quot; title=&quot;c:math:ldexp&quot;&gt;ldexp&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;computes a number in scientific notation&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row16&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/ldiv&quot; class=&quot;wikilink1&quot; title=&quot;c:math:ldiv&quot;&gt;ldiv&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the quotient and remainder of a division, in long integer form&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row17&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/log&quot; class=&quot;wikilink1&quot; title=&quot;c:math:log&quot;&gt;log&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;natural logarithm (to base e)&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row18&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/log10&quot; class=&quot;wikilink1&quot; title=&quot;c:math:log10&quot;&gt;log10&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;common logarithm (to base 10)&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row19&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/modf&quot; class=&quot;wikilink1&quot; title=&quot;c:math:modf&quot;&gt;modf&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;decomposes a number into integer and fractional parts&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row20&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/pow&quot; class=&quot;wikilink1&quot; title=&quot;c:math:pow&quot;&gt;pow&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns a given number raised to another number&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row21&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/sin&quot; class=&quot;wikilink1&quot; title=&quot;c:math:sin&quot;&gt;sin&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;sine&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row22&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/sinh&quot; class=&quot;wikilink1&quot; title=&quot;c:math:sinh&quot;&gt;sinh&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;hyperbolic sine&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row23&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/sqrt&quot; class=&quot;wikilink1&quot; title=&quot;c:math:sqrt&quot;&gt;sqrt&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;square root&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row24&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/tan&quot; class=&quot;wikilink1&quot; title=&quot;c:math:tan&quot;&gt;tan&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;tangent&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row25&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/c/math/tanh&quot; class=&quot;wikilink1&quot; title=&quot;c:math:tanh&quot;&gt;tanh&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;hyperbolic tangent&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description><link>http://cppsite.blogspot.com/2009/01/standard-c-math.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-1259138883832395459</guid><pubDate>Mon, 26 Jan 2009 10:05:00 +0000</pubDate><atom:updated>2009-01-26T02:07:00.190-08:00</atom:updated><title>Keywords in C++</title><description>&lt;strong&gt;C++&lt;/strong&gt;&#39;s  &lt;a name=&quot;an222&quot;&gt;&lt;/a&gt; keywords are a superset of &lt;strong&gt;C&lt;/strong&gt;&#39;s  keywords. Here is a list of all keywords of the language:&lt;br /&gt;&lt;pre&gt; and        const     float         operator static_cast    using&lt;br /&gt;and_eq   const_cast       for               or      struct  virtual&lt;br /&gt;  asm     continue    friend            or_eq      switch     void&lt;br /&gt; auto      default      goto          private    template volatile&lt;br /&gt;bitand       delete        if        protected        this  wchar_t&lt;br /&gt;bitor           do    inline           public       throw    while&lt;br /&gt; bool       double       int         register        true      xor&lt;br /&gt;break dynamic_cast      long reinterpret_cast         try   xor_eq&lt;br /&gt; case         else   mutable           return     typedef&lt;br /&gt;catch         enum namespace            short      typeid&lt;br /&gt; char     explicit       new           signed    typename&lt;br /&gt;class       extern       not           sizeof       union&lt;br /&gt;compl        false    not_eq           static    unsigned&lt;br /&gt;&lt;br /&gt;Note the &lt;a name=&quot;an223&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;em&gt;operator keywords&lt;/em&gt;: &lt;code&gt;and, and_eq, bitand, bitor, compl,&lt;br /&gt;not, not_eq, or, or_eq, xor&lt;/code&gt; and &lt;code&gt;xor_eq&lt;/code&gt; are symbolic alternatives for,&lt;br /&gt;respectively, &lt;code&gt;&amp;amp;&amp;amp;, &amp;amp;=, &amp;amp;, |, ~, !, !=, ||, |=, ^&lt;/code&gt; and &lt;code&gt;^=&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;</description><link>http://cppsite.blogspot.com/2009/01/keywords-in-c.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4478745916063651523</guid><pubDate>Fri, 23 Jan 2009 12:29:00 +0000</pubDate><atom:updated>2009-01-23T04:30:13.745-08:00</atom:updated><title>Type Casting</title><description>onverting an expression of a given type into another type is known as &lt;i&gt;type-casting&lt;/i&gt;. We have already seen some ways to type cast:&lt;h3&gt;Implicit conversion&lt;/h3&gt; Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;short&lt;/span&gt; a=2000;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b;&lt;br /&gt;b=a;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Here, the value of &lt;tt&gt;a&lt;/tt&gt; has been promoted from &lt;tt&gt;short&lt;/tt&gt; to &lt;tt&gt;int&lt;/tt&gt; and we have not had to specify any type-casting operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow conversions such as the conversions between numerical types (&lt;tt&gt;short&lt;/tt&gt; to &lt;tt&gt;int&lt;/tt&gt;, &lt;tt&gt;int&lt;/tt&gt; to &lt;tt&gt;float&lt;/tt&gt;, &lt;tt&gt;double&lt;/tt&gt; to &lt;tt&gt;int&lt;/tt&gt;...), to or from &lt;tt&gt;bool&lt;/tt&gt;, and some pointer conversions. Some of these conversions may imply a loss of precision, which the compiler can signal with a warning. This can be avoided with an explicit conversion.&lt;/p&gt;&lt;p&gt;Implicit conversions also include constructor or operator conversions, which affect classes that include specific constructors or operator functions to perform conversions. For example:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; A {};&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; B { &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;: B (A a) {} };&lt;br /&gt;&lt;br /&gt;A a;&lt;br /&gt;B b=a;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Here, a implicit conversion happened between objects of &lt;tt&gt;class A&lt;/tt&gt; and &lt;tt&gt;class B&lt;/tt&gt;, because &lt;tt&gt;B&lt;/tt&gt; has a constructor that takes an object of class &lt;tt&gt;A&lt;/tt&gt; as parameter. Therefore implicit conversions from &lt;tt&gt;A&lt;/tt&gt; to &lt;tt&gt;B&lt;/tt&gt; are allowed.&lt;/p&gt;&lt;h3&gt;Explicit conversion&lt;/h3&gt; C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion. We have already seen two notations for explicit type conversion: functional and c-like casting:&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;short&lt;/span&gt; a=2000;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b;&lt;br /&gt;b = (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt;) a;    &lt;span class=&quot;comm&quot;&gt;// c-like cast notation&lt;/span&gt;&lt;br /&gt;b = &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; (a);    &lt;span class=&quot;comm&quot;&gt;// functional notation &lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;The functionality of these explicit conversion operators is enough for most needs with fundamental data types. 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: &lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// class type-casting&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CDummy {&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;float&lt;/span&gt; i,j;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CAddition {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; x,y;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt; CAddition (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b) { x=a; y=b; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; result() { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; x+y;}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; CDummy d;&lt;br /&gt; CAddition * padd;&lt;br /&gt; padd = (CAddition*) &amp;d;&lt;br /&gt; cout &lt;&lt;&gt;result();&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt; &lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;The program declares a pointer to &lt;tt&gt;CAddition&lt;/tt&gt;, but then it assigns to it a reference to an object of another incompatible type using explicit type-casting:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;padd = (CAddition*) &amp;d;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Traditional explicit type-casting allows to convert any pointer into any other pointer type, independently of the types they point to. The subsequent call to member &lt;tt&gt;result&lt;/tt&gt; will produce either a run-time error or a unexpected result.&lt;/p&gt;&lt;p&gt;In order to control these types of conversions between classes, we have four specific casting operators: &lt;tt&gt;dynamic_cast&lt;/tt&gt;, &lt;tt&gt;reinterpret_cast&lt;/tt&gt;, &lt;tt&gt;static_cast&lt;/tt&gt; and &lt;tt&gt;const_cast&lt;/tt&gt;. Their format is to follow the new type enclosed between angle-brackets (&lt;tt&gt;&lt;&gt;&lt;/tt&gt;) and immediately after, the expression to be converted between parentheses.&lt;/p&gt;&lt;p&gt;&lt;tt&gt; dynamic_cast &lt;new_type&gt; (expression)&lt;br /&gt;reinterpret_cast &lt;new_type&gt; (expression)&lt;br /&gt;static_cast &lt;new_type&gt; (expression)&lt;br /&gt;const_cast &lt;new_type&gt; (expression)&lt;br /&gt;&lt;/tt&gt;&lt;/p&gt;&lt;p&gt;The traditional type-casting equivalents to these expressions would be:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt; (new_type) expression&lt;br /&gt;new_type (expression)&lt;br /&gt;&lt;/tt&gt;&lt;/p&gt;&lt;p&gt;but each one with its own special characteristics:&lt;/p&gt;&lt;h3&gt;dynamic_cast&lt;/h3&gt;&lt;p&gt;&lt;tt&gt;dynamic_cast&lt;/tt&gt; 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.&lt;/p&gt;&lt;p&gt;Therefore, &lt;tt&gt;dynamic_cast&lt;/tt&gt; is always successful when we cast a class to one of its base classes:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CBase { };&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CDerived: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; 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 = &lt;span class=&quot;kw&quot;&gt;dynamic_cast&lt;/span&gt;&lt;cbase*&gt;(&amp;amp;d);     &lt;span class=&quot;comm&quot;&gt;// ok: derived-to-base&lt;/span&gt;&lt;br /&gt;pd = &lt;span class=&quot;kw&quot;&gt;dynamic_cast&lt;/span&gt;&lt;cderived*&gt;(&amp;amp;b);  &lt;span class=&quot;comm&quot;&gt;// wrong: base-to-derived &lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;The second conversion in this piece of code would produce a compilation error since base-to-derived conversions are not allowed with &lt;tt&gt;dynamic_cast&lt;/tt&gt; unless the base class is polymorphic.&lt;/p&gt;&lt;p&gt;When a class is polymorphic, &lt;tt&gt;dynamic_cast&lt;/tt&gt; performs a special checking during runtime to ensure that the expression yields a valid complete object of the requested class:&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// dynamic_cast&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;exception&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CBase { &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; dummy() {} };&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CDerived: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CBase { &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt; {&lt;br /&gt;   CBase * pba = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CDerived;&lt;br /&gt;   CBase * pbb = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CBase;&lt;br /&gt;   CDerived * pd;&lt;br /&gt;&lt;br /&gt;   pd = &lt;span class=&quot;kw&quot;&gt;dynamic_cast&lt;/span&gt;&lt;cderived*&gt;(pba);&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; (pd==0) cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;Null pointer on first type-cast&quot;&lt;/span&gt; &lt;&lt; endl;&lt;br /&gt;&lt;br /&gt;   pd = &lt;span class=&quot;kw&quot;&gt;dynamic_cast&lt;/span&gt;&lt;cderived*&gt;(pbb);&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; (pd==0) cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;Null pointer on second type-cast&quot;&lt;/span&gt; &lt;&lt; endl;&lt;br /&gt;&lt;br /&gt; } &lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (exception&amp;amp; e) {cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;Exception: &quot;&lt;/span&gt; &lt;&lt; e.what();}&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;Null pointer on second type-cast&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;table class=&quot;boxed&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt; &lt;b&gt;Compatibility note:&lt;/b&gt; &lt;tt&gt;dynamic_cast&lt;/tt&gt; 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 &lt;tt&gt;dynamic_cast&lt;/tt&gt; to work properly.&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;The code tries to perform two dynamic casts from pointer objects of type &lt;tt&gt;CBase*&lt;/tt&gt; (&lt;tt&gt;pba&lt;/tt&gt; and &lt;tt&gt;pbb&lt;/tt&gt;) to a pointer object of type &lt;tt&gt;CDerived*&lt;/tt&gt;, but only the first one is successful. Notice their respective initializations:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;CBase * pba = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CDerived;&lt;br /&gt;CBase * pbb = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CBase;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Even though both are pointers of type &lt;tt&gt;CBase*&lt;/tt&gt;, &lt;tt&gt;pba&lt;/tt&gt; points to an object of type &lt;tt&gt;CDerived&lt;/tt&gt;, while &lt;tt&gt;pbb&lt;/tt&gt; points to an object of type &lt;tt&gt;CBase&lt;/tt&gt;. Thus, when their respective type-castings are performed using &lt;tt&gt;dynamic_cast&lt;/tt&gt;, &lt;tt&gt;pba&lt;/tt&gt; is pointing to a full object of class &lt;tt&gt;CDerived&lt;/tt&gt;, whereas &lt;tt&gt;pbb&lt;/tt&gt; is pointing to an object of class &lt;tt&gt;CBase&lt;/tt&gt;, which is an incomplete object of class &lt;tt&gt;CDerived&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;When &lt;tt&gt;dynamic_cast&lt;/tt&gt; cannot cast a pointer because it is not a complete object of the required class -as in the second conversion in the previous example- it returns a null pointer to indicate the failure. If &lt;tt&gt;dynamic_cast&lt;/tt&gt; is used to convert to a reference type and the conversion is not possible, an exception of type &lt;tt&gt;bad_cast&lt;/tt&gt; is thrown instead.&lt;/p&gt;&lt;p&gt;&lt;tt&gt;dynamic_cast&lt;/tt&gt; can also cast null pointers even between pointers to unrelated classes, and can also cast pointers of any type to void pointers (&lt;tt&gt;void*&lt;/tt&gt;).&lt;/p&gt;&lt;h3&gt;static_cast&lt;/h3&gt; &lt;tt&gt;static_cast&lt;/tt&gt; can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived. This ensures that at least the classes are compatible if the proper object is converted, but no safety check is performed during runtime to check if the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, the overhead of the type-safety checks of &lt;tt&gt;dynamic_cast&lt;/tt&gt; is avoided.&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CBase {};&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CDerived: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CBase {};&lt;br /&gt;CBase * a = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CBase;&lt;br /&gt;CDerived * b = &lt;span class=&quot;kw&quot;&gt;static_cast&lt;/span&gt;&lt;cderived*&gt;(a);&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;This would be valid, although &lt;tt&gt;b&lt;/tt&gt; would point to an incomplete object of the class and could lead to runtime errors if dereferenced.&lt;/p&gt;&lt;p&gt;&lt;tt&gt;static_cast&lt;/tt&gt; can also be used to perform any other non-pointer conversion that could also be performed implicitly, like for example standard conversion between fundamental types:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;double&lt;/span&gt; d=3.14159265;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; i = &lt;span class=&quot;kw&quot;&gt;static_cast&lt;/span&gt;&lt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt;&gt;(d); &lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Or any conversion between classes with explicit constructors or operator functions as described in &quot;implicit conversions&quot; above.&lt;/p&gt;&lt;h3&gt;reinterpret_cast&lt;/h3&gt; &lt;tt&gt;reinterpret_cast&lt;/tt&gt; converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.&lt;p&gt;It can also cast pointers to or from integer types. The format in which this integer value represents a pointer is platform-specific. The only guarantee is that a pointer cast to an integer type large enough to fully contain it, is granted to be able to be cast back to a valid pointer.&lt;/p&gt;&lt;p&gt;The conversions that can be performed by &lt;tt&gt;reinterpret_cast&lt;/tt&gt; but not by &lt;tt&gt;static_cast&lt;/tt&gt; have no specific uses in C++ are low-level operations, whose interpretation results in code which is generally system-specific, and thus non-portable. For example:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; A {};&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; B {};&lt;br /&gt;A * a = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; A;&lt;br /&gt;B * b = &lt;span class=&quot;kw&quot;&gt;reinterpret_cast&lt;/span&gt;&lt;b*&gt;(a);&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;This is valid C++ code, although it does not make much sense, since now we have a pointer that points to an object of an incompatible class, and thus dereferencing it is unsafe.&lt;/p&gt;&lt;h3&gt;const_cast&lt;/h3&gt; This type of casting manipulates the constness of an object, either to be set or to be removed. For example, in order to pass a const argument to a function that expects a non-constant parameter:&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// const_cast&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; print (&lt;span class=&quot;kw&quot;&gt;char&lt;/span&gt; * str)&lt;br /&gt;{&lt;br /&gt; cout &lt;&lt; str &lt;&lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;char&lt;/span&gt; * c = &lt;span class=&quot;str&quot;&gt;&quot;sample text&quot;&lt;/span&gt;;&lt;br /&gt; print ( &lt;span class=&quot;kw&quot;&gt;const_cast&lt;/span&gt;&lt;&lt;span class=&quot;kw&quot;&gt;char&lt;/span&gt; *&gt; (c) );&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;sample text&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;typeid&lt;/h3&gt; &lt;tt&gt;typeid&lt;/tt&gt; allows to check the type of an expression: &lt;p&gt;&lt;tt&gt; typeid (expression)&lt;br /&gt;&lt;/tt&gt;&lt;/p&gt;&lt;p&gt;This operator returns a reference to a constant object of type &lt;tt&gt;type_info&lt;/tt&gt; that is defined in the standard header file &lt;tt&gt;&lt;typeinfo&gt;&lt;/tt&gt;. This returned value can be compared with another one using operators &lt;tt&gt;==&lt;/tt&gt; and &lt;tt&gt;!=&lt;/tt&gt; or can serve to obtain a null-terminated character sequence representing the data type or class name by using its &lt;tt&gt;name()&lt;/tt&gt; member.&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// typeid&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;typeinfo&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; * a,b;&lt;br /&gt; a=0; b=0;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(a) != &lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(b))&lt;br /&gt; {&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;a and b are of different types:\n&quot;&lt;/span&gt;;&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;a is: &quot;&lt;/span&gt; &lt;&lt; &lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(a).name() &lt;&lt; &lt;span class=&quot;str&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;b is: &quot;&lt;/span&gt; &lt;&lt; &lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(b).name() &lt;&lt; &lt;span class=&quot;str&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;a and b are of different types:&lt;br /&gt;a is: int *&lt;br /&gt;b is: int  &lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;When &lt;tt&gt;typeid&lt;/tt&gt; is applied to classes &lt;tt&gt;typeid&lt;/tt&gt; 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:&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// typeid, polymorphic class&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;typeinfo&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;exception&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CBase { &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; f(){} };&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CDerived : &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CBase {};&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt; {&lt;br /&gt;   CBase* a = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CBase;&lt;br /&gt;   CBase* b = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CDerived;&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;a is: &quot;&lt;/span&gt; &lt;&lt; &lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(a).name() &lt;&lt; &lt;span class=&quot;str&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;b is: &quot;&lt;/span&gt; &lt;&lt; &lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(b).name() &lt;&lt; &lt;span class=&quot;str&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;*a is: &quot;&lt;/span&gt; &lt;&lt; &lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(*a).name() &lt;&lt; &lt;span class=&quot;str&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;*b is: &quot;&lt;/span&gt; &lt;&lt; &lt;span class=&quot;kw&quot;&gt;typeid&lt;/span&gt;(*b).name() &lt;&lt; &lt;span class=&quot;str&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;br /&gt; } &lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (exception&amp;amp; e) { cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;Exception: &quot;&lt;/span&gt; &lt;&lt; e.what() &lt;&lt; endl; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&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;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Notice how the type that &lt;tt&gt;typeid&lt;/tt&gt; considers for pointers is the pointer type itself (both &lt;tt&gt;a&lt;/tt&gt; and &lt;tt&gt;b&lt;/tt&gt; are of type &lt;tt&gt;class CBase *&lt;/tt&gt;). However, when &lt;tt&gt;typeid&lt;/tt&gt; is applied to objects (like &lt;tt&gt;*a&lt;/tt&gt; and &lt;tt&gt;*b&lt;/tt&gt;) &lt;tt&gt;typeid&lt;/tt&gt; yields their dynamic type (i.e. the type of their most derived complete object).&lt;/p&gt;If the type &lt;tt&gt;typeid&lt;/tt&gt; evaluates is a pointer preceded by the dereference operator (&lt;tt&gt;*&lt;/tt&gt;), and this pointer has a null value, &lt;tt&gt;typeid&lt;/tt&gt; throws a &lt;tt&gt;bad_typeid&lt;/tt&gt; exception.</description><link>http://cppsite.blogspot.com/2009/01/type-casting.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-646092062602661690</guid><pubDate>Fri, 23 Jan 2009 12:19:00 +0000</pubDate><atom:updated>2009-01-23T04:20:31.041-08:00</atom:updated><title>Exceptions</title><description>Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called &lt;i&gt;handlers&lt;/i&gt;.&lt;p&gt;To catch exceptions we must place a portion of code under exception inspection. This is done by enclosing that portion of code in a &lt;i&gt;try block&lt;/i&gt;. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.&lt;/p&gt;&lt;p&gt;A exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword &lt;tt&gt;catch&lt;/tt&gt;, which must be placed immediately after the try block:&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// exceptions&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;throw&lt;/span&gt; 20;&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; e)&lt;br /&gt; {&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;An exception occurred. Exception Nr. &quot;&lt;/span&gt; &lt;&lt; e &lt;&lt; endl;&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;An exception occurred. Exception Nr. 20&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;The code under exception handling is enclosed in a &lt;tt&gt;try&lt;/tt&gt; block. In this example this code simply throws an exception:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;throw&lt;/span&gt; 20;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;A throw expression accepts one parameter (in this case the integer value &lt;tt&gt;20&lt;/tt&gt;), which is passed as an argument to the exception handler.&lt;/p&gt;&lt;p&gt;The exception handler is declared with the &lt;tt&gt;catch&lt;/tt&gt; keyword. As you can see, it follows immediately the closing brace of the &lt;tt&gt;try&lt;/tt&gt; block. The catch format is similar to a regular function that always has at least one parameter. The type of this parameter is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught.&lt;/p&gt;&lt;p&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;/p&gt;&lt;p&gt;If we use an ellipsis (&lt;tt&gt;...&lt;/tt&gt;) as the parameter of &lt;tt&gt;catch&lt;/tt&gt;, that handler will catch any exception no matter what the type of the &lt;tt&gt;throw&lt;/tt&gt; exception is. This can be used as a default handler that catches all exceptions not caught by other handlers if it is specified at last:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt; {&lt;br /&gt; &lt;span class=&quot;comm&quot;&gt;// code here&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; param) { cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;int exception&quot;&lt;/span&gt;; }&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;char&lt;/span&gt; param) { cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;char exception&quot;&lt;/span&gt;; }&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (...) { cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;default exception&quot;&lt;/span&gt;; }&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;In this case the last handler would catch any exception thrown with any parameter that is neither an &lt;tt&gt;int&lt;/tt&gt; nor a &lt;tt&gt;char&lt;/tt&gt;.&lt;/p&gt;&lt;p&gt;After an exception has been handled the program execution resumes after the &lt;tt&gt;try-catch&lt;/tt&gt; block, not after the &lt;tt&gt;throw&lt;/tt&gt; statement!.&lt;/p&gt;&lt;p&gt;It is also possible to nest &lt;tt&gt;try-catch&lt;/tt&gt; blocks within more external &lt;tt&gt;try&lt;/tt&gt; blocks. In these cases, we have the possibility that an internal &lt;tt&gt;catch&lt;/tt&gt; block forwards the exception to its external level. This is done with the expression &lt;tt&gt;throw;&lt;/tt&gt; with no arguments. For example: &lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt; {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt; {&lt;br /&gt;     &lt;span class=&quot;comm&quot;&gt;// code here&lt;/span&gt;&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; n) {&lt;br /&gt;     &lt;span class=&quot;kw&quot;&gt;throw&lt;/span&gt;;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (...) {&lt;br /&gt; cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;Exception occurred&quot;&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;Exception specifications&lt;/h3&gt;&lt;p&gt;When declaring a function we can limit the exception type it might directly or indirectly throw by appending a &lt;tt&gt;throw&lt;/tt&gt; suffix to the function declaration:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;float&lt;/span&gt; myfunction (&lt;span class=&quot;kw&quot;&gt;char&lt;/span&gt; param) &lt;span class=&quot;kw&quot;&gt;throw&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;This declares a function called &lt;tt&gt;myfunction&lt;/tt&gt; which takes one agument of type &lt;tt&gt;char&lt;/tt&gt; and returns an element of type &lt;tt&gt;float&lt;/tt&gt;. The only exception that this function might throw is an exception of type &lt;tt&gt;int&lt;/tt&gt;. If it throws an exception with a different type, either directly or indirectly, it cannot be caught by a regular &lt;tt&gt;int&lt;/tt&gt;-type handler.&lt;/p&gt;&lt;p&gt;If this &lt;tt&gt;throw&lt;/tt&gt; specifier is left empty with no type, this means the function is not allowed to throw exceptions. Functions with no &lt;tt&gt;throw&lt;/tt&gt; specifier (regular functions) are allowed to throw exceptions with any type:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; myfunction (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; param) &lt;span class=&quot;kw&quot;&gt;throw&lt;/span&gt;(); &lt;span class=&quot;comm&quot;&gt;// no exceptions allowed&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; myfunction (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; param);         &lt;span class=&quot;comm&quot;&gt;// all exceptions allowed &lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;Standard exceptions&lt;/h3&gt; The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called &lt;tt&gt;exception&lt;/tt&gt; and is defined in the &lt;tt&gt;&lt;exception&gt;&lt;/tt&gt; header file under the &lt;tt&gt;namespace std&lt;/tt&gt;. This class has the usual default and copy constructors, operators and destructors, plus an additional virtual member function called &lt;tt&gt;what&lt;/tt&gt; that returns a null-terminated character sequence (&lt;tt&gt;char *&lt;/tt&gt;) and that can be overwritten in derived classes to contain some sort of description of the exception.&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// standard exceptions&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;exception&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; myexception: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; exception&lt;br /&gt;{&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;char&lt;/span&gt;* what() &lt;span class=&quot;kw&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;throw&lt;/span&gt;()&lt;br /&gt; {&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;str&quot;&gt;&quot;My exception happened&quot;&lt;/span&gt;;&lt;br /&gt; }&lt;br /&gt;} myex;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;throw&lt;/span&gt; myex;&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (exception&amp;amp; e)&lt;br /&gt; {&lt;br /&gt;   cout &lt;&lt; e.what() &lt;&lt; endl;&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;My exception happened.&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;We have placed a handler that catches exception objects by reference (notice the ampersand &lt;tt&gt;&amp;amp;&lt;/tt&gt; after the type), therefore this catches also classes derived from &lt;tt&gt;exception&lt;/tt&gt;, like our &lt;tt&gt;myex&lt;/tt&gt; object of class &lt;tt&gt;myexception&lt;/tt&gt;.&lt;/p&gt;&lt;p&gt;All exceptions thrown by components of the C++ Standard library throw exceptions derived from this &lt;tt&gt;std::exception&lt;/tt&gt; class. These are:&lt;/p&gt;&lt;table class=&quot;boxed&quot;&gt; &lt;tbody&gt;&lt;tr&gt;&lt;th&gt;exception&lt;/th&gt;&lt;th&gt;description&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;bad_alloc&lt;/td&gt;&lt;td&gt;thrown by new on allocation failure&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;bad_cast&lt;/td&gt;&lt;td&gt;thrown by dynamic_cast when fails with a referenced type&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;bad_exception&lt;/td&gt;&lt;td&gt;thrown when an exception type doesn&#39;t match any catch&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;bad_typeid&lt;/td&gt;&lt;td&gt;thrown by typeid&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;ios_base::failure&lt;/td&gt;&lt;td&gt;thrown by functions in the iostream library&lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;For example, if we use the operator &lt;tt&gt;new&lt;/tt&gt; and the memory cannot be allocated, an exception of type &lt;tt&gt;bad_alloc&lt;/tt&gt; is thrown:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; * myarray= &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt;[1000];&lt;br /&gt;}&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (bad_alloc&amp;amp;)&lt;br /&gt;{&lt;br /&gt; cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;Error allocating memory.&quot;&lt;/span&gt; &lt;&lt; endl;&lt;br /&gt;}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&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 &lt;tt&gt;bad_alloc&lt;/tt&gt; exception to see it in action, you can try to allocate a huge array; On my system, trying to allocate 1 billion &lt;tt&gt;int&lt;/tt&gt;s threw a &lt;tt&gt;bad_alloc&lt;/tt&gt; exception.&lt;/p&gt;&lt;p&gt;Because &lt;tt&gt;bad_alloc&lt;/tt&gt; is derived from the standard base class &lt;tt&gt;exception&lt;/tt&gt;, we can handle that same exception by catching references to the &lt;tt&gt;exception&lt;/tt&gt; class:&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// bad_alloc standard exception&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;exception&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;try&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt;* myarray= &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt;[1000];&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;catch&lt;/span&gt; (exception&amp;amp; e)&lt;br /&gt; {&lt;br /&gt;   cout &lt;&lt; &lt;span class=&quot;str&quot;&gt;&quot;Standard exception: &quot;&lt;/span&gt; &lt;&lt; e.what() &lt;&lt; endl;&lt;br /&gt; }&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt; &lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description><link>http://cppsite.blogspot.com/2009/01/exceptions.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-8119683173252805711</guid><pubDate>Thu, 22 Jan 2009 08:03:00 +0000</pubDate><atom:updated>2009-01-22T00:03:40.479-08:00</atom:updated><title>C and C++ are CRAZY!</title><description>&lt;p&gt;I’ve learned a lot of crazy things about C and C++ in the last few years, as far as parsing goes. The one that’s always blown me away the most is that parsing C++ is undecidable: &lt;a href=&quot;http://yosefk.com/c++fqa/web-vs-c++.html#misfeature-3&quot;&gt;you can’t properly parse C++ without doing template instantiation&lt;/a&gt;, but &lt;a href=&quot;http://ubiety.uwaterloo.ca/%7Etveldhui/papers/2003/turing.pdf&quot;&gt;template instantiation is turing-complete&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;But every time I think I’ve gotten to the point where C and C++ can’t faze me anymore, I find something new.  Like this, from &lt;a href=&quot;http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B&quot;&gt;the Wikipedia article about operator precedence in C and C++&lt;/a&gt;:&lt;/p&gt; &lt;blockquote&gt;&lt;p&gt;The binding of operators in C and C++ is specified (in the corresponding Standards) by a factored language grammar, rather than a precedence table. This creates some subtle conflicts. For example, in C, the syntax for a conditional expression is:&lt;/p&gt; &lt;pre&gt;logical-OR-expression ? expression :&lt;br /&gt;conditional-expression&lt;/pre&gt; &lt;p&gt;while in C++ it is:&lt;/p&gt; &lt;pre&gt;logical-or-expression ? expression :&lt;br /&gt;assignment-expression&lt;/pre&gt; &lt;p&gt;Hence, the expression:&lt;/p&gt; &lt;pre&gt;e = a ? b : c = d&lt;/pre&gt; &lt;p&gt;is parsed differently in the two languages. In C, this expression is parsed as:&lt;/p&gt; &lt;pre&gt;e = ((a ? b : c) = d)&lt;/pre&gt; &lt;p&gt;which is a semantic error, since the result of a conditional-expression is not an lvalue. In C++, it is parsed as:&lt;/p&gt; &lt;pre&gt;e = (a ? b : (c = d))&lt;/pre&gt; &lt;p&gt;which is a valid expression.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;WHAT?? I had to try this out and determine whether it was true or some crazy Wikipedia anonymous editor who was trying to drive me mad. So I wrote the simple test program:&lt;/p&gt; &lt;pre&gt;int main() {&lt;br /&gt; int e = 1, a = 2, b = 3, c = 4, d = 5;&lt;br /&gt; e = a ? b : c = d;&lt;br /&gt; return e;&lt;br /&gt;} &lt;/pre&gt; &lt;p&gt;…and attempted to compile it first with gcc as a .c, then with g++ as a .cc, and sure enough:&lt;/p&gt; &lt;pre&gt;$ gcc -o /tmp/test /tmp/test.c&lt;br /&gt;/tmp/test.c: In function &#39;main&#39;:&lt;br /&gt;/tmp/test.c:4: error: lvalue required as left operand of assignment&lt;br /&gt;/tmp/test.c:4: error: expected &#39;;&#39; before &#39;return&#39;&lt;br /&gt;$ g++ -o /tmp/test /tmp/test.cc&lt;br /&gt;$&lt;/pre&gt; &lt;p&gt;The world is insane. Luckily Gazelle will be the sword in your right hand, your weapon against a world that conspires against your sanity.&lt;/p&gt; &lt;p&gt;Incidentally, it’s more and more clear that C++ is not a superset of C.  Though it’s true enough that most people &lt;i&gt;using&lt;/i&gt; the language don’t have to care, for language implementors the differences are significant.&lt;/p&gt;</description><link>http://cppsite.blogspot.com/2009/01/c-and-c-are-crazy.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-357245470192673500</guid><pubDate>Mon, 19 Jan 2009 07:28:00 +0000</pubDate><atom:updated>2009-01-18T23:36:14.317-08:00</atom:updated><title>Rounding Algorithms</title><description>There are a zillion different ways to round floating point values to integers. C and C++ provide a couple basic ones in &lt;math.h&gt; or &lt;cmath&gt;.&lt;br /&gt;There are two general categories of rounding algorithm: those that are symmetric about zero and those that are biased in some way.&lt;br /&gt;&lt;br /&gt;Biased Rounding&lt;br /&gt;A bias is a mathematical notion used in statistics to indicate that samples are not exactly representative of their true values; they are skewed in one way or another.&lt;br /&gt;&lt;br /&gt;For example, the &lt;cmath&gt; floor() function is biased towards negative infinity, because it always chooses the lower integer number --that is, it always chooses the number closer to negative infinity.&lt;br /&gt;&lt;br /&gt;floor( 7.5 ) --&gt; 7&lt;br /&gt;floor( -7.5 ) --&gt; -8&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Suppose your local big city and wanted to know how fast people are driving on a particular freeway. The first step is to gather the exact speeds that individual drivers are going, and the second would be to convert all the individual values into a single value that would represent the normal rate of speed. For simplicity here, we will just use the average value.&lt;br /&gt;&lt;br /&gt;Let us say that the equipment which samples a motorist&#39;s speed is far more accurate than the computer&#39;s ability to store it. (This is actually not uncommon.) Again, for simplicity, we will say that the computer stores the speed as integers.&lt;br /&gt;&lt;br /&gt;With a sampling of twelve motorists, we get the following speeds (in miles per hour)&lt;br /&gt;&lt;br /&gt;49.087 57.901 28.500 46.738 51.270 53.096&lt;br /&gt;44.795 47.218 46.347 45.989 47.582 50.563&lt;br /&gt;&lt;br /&gt;A quick calculation shows that the average speed is 47.424 mph.&lt;br /&gt;&lt;br /&gt;If the city were to simply use the floor() function to convert these to integers, it would get&lt;br /&gt;&lt;br /&gt;49 57 28 46 51 53&lt;br /&gt;44 47 46 45 47 50&lt;br /&gt;&lt;br /&gt;which averages to 46.916 --&gt; 46 mph (remember, integer arithmetic!)&lt;br /&gt;&lt;br /&gt;Either way, the sampling is off by about a whole mile per hour. I don&#39;t think that the city would actually care about a single mile per hour, but this does illustrate the bias, or tendancy of the floor() function, to make the numbers closer to negative infinity, thuse skewing the data to an inaccurate number.&lt;br /&gt;&lt;br /&gt;This was just a simple example that came off the top of my head, but in many sciences and statistical surveys, that difference can mean quite a lot. Suppose that the Apollo missed the moon by 1%? Suppose a pharmaceutical company put too much iron in a daily vitamin pill by 1%? Suppose a construction firm miscalculated the stresses a bridge can take by 1%? In all these scenarios the results would prove deadly. One percent is a lot.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Symmetric Rounding&lt;br /&gt;A special case of bias is centered about zero. Let us fix the floor() function to tend towards zero.&lt;br /&gt;1&lt;br /&gt;2&lt;br /&gt;3&lt;br /&gt;4&lt;br /&gt;5&lt;br /&gt;6&lt;br /&gt;7&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;double floor0( double value )&lt;br /&gt;  {&lt;br /&gt;  if (value &lt; 0.0)&lt;br /&gt;    return ceil( value );&lt;br /&gt;  else&lt;br /&gt;    return floor( value );&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, the absolute value of the result will always be the same:&lt;br /&gt;&lt;br /&gt;floor0( -7.7 ) --&gt; -7 floor0( 7.7 ) --&gt; 7&lt;br /&gt;floor0( -7.5 ) --&gt; -7 floor0( 7.5 ) --&gt; 7&lt;br /&gt;floor0( -7.3 ) --&gt; -7 floor0( 7.3 ) --&gt; 7&lt;br /&gt;&lt;br /&gt;Enough about that.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Unbiased Rounding&lt;br /&gt;So, how do we handle these biases? By adding some rule that accounts for it.&lt;br /&gt;&lt;br /&gt;Let us apply knowledge we all learned in gradeschool: In arithmetic rounding we round up if the next digit is 5 or more, and if it less than 5 we round down. We write ourselves a little function to do just that:&lt;br /&gt;1&lt;br /&gt;2&lt;br /&gt;3&lt;br /&gt;4&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;double round( double value )&lt;br /&gt;  {&lt;br /&gt;  return floor( value + 0.5 );&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The problem is that this is still biased. We have actually reversed the bias of floor() from negative infinity to positive infinity, because we always choose to round up when exactly halfway between two values:&lt;br /&gt;&lt;br /&gt;round( 10.3 ) --&gt; 10&lt;br /&gt;round( 10.5 ) --&gt; 11&lt;br /&gt;round( 10.7 ) --&gt; 11&lt;br /&gt;&lt;br /&gt;You can actually see the bias in the above table: the result tends towards 11 and away from 10.&lt;br /&gt;&lt;br /&gt;This brings us to the trick: which way do we round when exactly halfway between two values?&lt;br /&gt;&lt;br /&gt;One very popular method is variously called &quot;bankers rounding&quot;, &quot;round to even&quot;, &quot;convergent rounding&quot;, and even &quot;unbiased rounding&quot;, to name a few. It works by skewing the bias itself.&lt;br /&gt;&lt;br /&gt;Given a number exactly halfway between two values, round to the even value (zero is considered even here).&lt;br /&gt;&lt;br /&gt;round( 1.7 ) --&gt; 2 round( 2.7 ) --&gt; 3&lt;br /&gt;round( 1.5 ) --&gt; 2 round( 2.5 ) --&gt; 2&lt;br /&gt;round( 1.3 ) --&gt; 1 round( 2.3 ) --&gt; 2&lt;br /&gt;&lt;br /&gt;For random data this is very convenient. Bankers like it because money deposited and withdrawn is random. (There are trends, mind you, but you cannot predict exactly how much will be deposited and withdrawn.) The important point is that the bankers rounding is still biased if the data is biased. It is only unbiased with random data.&lt;br /&gt;&lt;br /&gt;One solution is called &quot;alternate rounding&quot;. It works by simply choosing to bias up or down every other time.&lt;br /&gt;&lt;br /&gt;round( 1.5 ) --&gt; 2&lt;br /&gt;round( 1.5 ) --&gt; 1&lt;br /&gt;round( 1.5 ) --&gt; 2&lt;br /&gt;round( 1.5 ) --&gt; 1&lt;br /&gt;etc&lt;br /&gt;&lt;br /&gt;This is not always useful though.&lt;br /&gt;&lt;br /&gt;The only way to eliminate all bias is to use a random bias... This, of course, is impossible to generate in your typical PC, but it still goes toward solving the problem quite nicely.&lt;br /&gt;&lt;br /&gt;If the sample is exactly halfway between two integers, it chooses one or the other randomly.&lt;br /&gt;&lt;br /&gt;Of course, the Achilles heel of this method is the random number generator you use. The default pseudorandom generator for C and C++ is not that great. The Mersenne Twister is by far the most popular high-quality pseudorandom number generator, but it is non-trivial to implement so I will not include it below.&lt;br /&gt;&lt;br /&gt;Anyway, what follows is a convenient, simple library you are free to use. I&#39;ll even permit you to cannabalize it at will (since the algorithms are so obvious...)&lt;br /&gt;&lt;br /&gt;I may update it sometime in the future if I can figure out a way around the default epsilon issue. Feel free to make suggestions for improvements.&lt;br /&gt;&lt;br /&gt;:-)</description><link>http://cppsite.blogspot.com/2009/01/rounding-algorithms.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4962142052768649420</guid><pubDate>Sat, 17 Jan 2009 06:40:00 +0000</pubDate><atom:updated>2009-01-16T22:45:02.127-08:00</atom:updated><title>Review of Mastering C++</title><description>&lt;div id=&quot;body&quot; style=&quot;text-align: justify;&quot;&gt; &lt;p&gt;Mastering C++ is a book written by K. R. Venugopal, T Ravishankar and Rajkumar. The book teaches the computer language C++ from the scratch to the professional level in a very friendly approach to the learning of the otherwise very difficult language.&lt;/p&gt; &lt;p&gt;First published in 1997, the book is recommended by most of the programmers. Basically written with C programmers in mind, this book caters to novices as well.&lt;/p&gt; &lt;p&gt;My brother advised me to read this book as I was desperately trying to learn the language but was unable to do so. At the first sight of the book, I thought,” This book can’t help anyone. This is just another book for engineers and is full of technical jargon.”&lt;/p&gt; &lt;p&gt;Almost two years later I learned ‘C’ language from the book “Let Us C” by Yashwant Kanetkar.&lt;br /&gt;That book helped me clear the basic concepts of programming and get some technical jargon of computers, giving me the break I needed.&lt;/p&gt; &lt;p&gt;I learned the concepts of OOPs from the reference books of NIIT. After that, I was ready to learn C++ in detail. I started from chapter-one which helped me get acquainted with thinking in terms of OOPs. And the following chapters till chapter nine taught me the basics of C++ (non-OOPs) and from 10 to 19 the OOPs side of the language. Chapter 20 was based on the management of software projects.&lt;/p&gt; &lt;p&gt;So after learning it, I decided to write some programs and got disappointed because I could write only console based programs, as standard C++ does not specify the platform independent GUI programming.&lt;/p&gt; &lt;p&gt;Though Mastering C++ is good for learning, to gain expertise in language one must not rely on this book alone. Specialized books in the field of data structures, network programming, games development etc should also be read and kept as reference.&lt;/p&gt; &lt;p&gt;But as far as the question of the language is concerned, this book can be trusted as it teaches the language and its constructs in details. To become expert in any field the base should be firm. And this book does the job.&lt;/p&gt; &lt;p&gt;Best of luck for your adventures in C++.&lt;/p&gt; &lt;/div&gt;</description><link>http://cppsite.blogspot.com/2009/01/review-of-mastering-c.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-3193908711764542159</guid><pubDate>Thu, 15 Jan 2009 12:12:00 +0000</pubDate><atom:updated>2009-01-15T04:16:25.357-08:00</atom:updated><title>Two Reasons Why I Prefer C to Java</title><description>There are two specific reasons why I prefer C++ to Java: operator overloading and pointers.  I strongly believe that these are related to the fact that I started learning C++ first, but I still find them critical to my preference for C++.&lt;br /&gt;&lt;br /&gt;Operator overloading, for me, is a critical feature in C++ for providing intuitive code.  When creating a mathematical class, such as BigInt, I want to be able to work with variables of that type the same way I work with variables of type int.  If + is a meaningful operation on a class, I want to be able to use it.  Moreover, I don&#39;t want to have to wrap my built-in type int in a class with .add(), .minus(), etc methods so that I can create a template for building rational numbers.  I realize that this is a side-effect of my math background, but I believe that I should be able to write math with math symbols wherever possible.&lt;br /&gt;&lt;br /&gt;The other irritation I have with Java is that it weakened pointers as found in languages like C++ and Pascal to what it calls a &quot;reference&quot;.  Anyone who is familiar with pointers in other languages will recognize that Java is using the pointer concept, but weakening it to make the language safer.  I realize that this is a deliberate design decision, but it is a decision that irritates me in its implementation.  It is both less function than C++ pointers, and more powerful in that it handles garbage collection.  For me, losing pointer arithmetic for garbage collection is a bad trade&lt;br /&gt;.</description><link>http://cppsite.blogspot.com/2009/01/two-reasons-why-i-prefer-c-to-java.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-2071387192043059306</guid><pubDate>Thu, 15 Jan 2009 11:39:00 +0000</pubDate><atom:updated>2009-01-15T03:46:35.105-08:00</atom:updated><title>C++ Sets</title><description>&lt;p&gt;  The C++ Set is an associative STL container that contains a sorted set of unique objects.  &lt;/p&gt;   &lt;table class=&quot;inline&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;row0&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/set_constructors&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:set_constructors&quot;&gt;Constructors&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;default methods to allocate, copy, and deallocate sets&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row1&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/set_operators&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:set_operators&quot;&gt;Operators&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;assign and compare sets&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row2&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/begin&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:begin&quot;&gt;begin&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns an iterator to the beginning of the set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row3&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/clear&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:clear&quot;&gt;clear&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;removes all elements from the set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row4&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/count&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:count&quot;&gt;count&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the number of elements matching a certain key&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row5&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/empty&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:empty&quot;&gt;empty&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;true if the set has no elements&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row6&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/end&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:end&quot;&gt;end&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns an iterator just past the last element of a set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row7&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/equal_range&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:equal_range&quot;&gt;equal_range&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns iterators to the first and just past the last elements matching a specific key&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row8&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/erase&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:erase&quot;&gt;erase&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;removes elements from a set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row9&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/find&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:find&quot;&gt;find&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns an iterator to specific elements&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row10&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/insert&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:insert&quot;&gt;insert&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;insert items into a set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row11&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/key_comp&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:key_comp&quot;&gt;key_comp&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the function that compares keys&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row12&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/lower_bound&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:lower_bound&quot;&gt;lower_bound&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns an iterator to the first element greater than or equal to a certain value&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row13&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/max_size&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:max_size&quot;&gt;max_size&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the maximum number of elements that the set can hold&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row14&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/rbegin&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:rbegin&quot;&gt;rbegin&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns a reverse_iterator to the end of the set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row15&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/rend&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:rend&quot;&gt;rend&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns a reverse_iterator to the beginning of the set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row16&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/size&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:size&quot;&gt;size&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the number of items in the set&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row17&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/swap&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:swap&quot;&gt;swap&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;swap the contents of this set with another&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row18&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/upper_bound&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:upper_bound&quot;&gt;upper_bound&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns an iterator to the first element greater than a certain value&lt;/td&gt;  &lt;/tr&gt;  &lt;tr class=&quot;row19&quot;&gt;   &lt;td class=&quot;col0&quot;&gt;&lt;a href=&quot;http://www.cppreference.com/wiki/stl/set/value_comp&quot; class=&quot;wikilink1&quot; title=&quot;stl:set:value_comp&quot;&gt;value_comp&lt;/a&gt;&lt;/td&gt;&lt;td class=&quot;col1&quot;&gt;returns the function that compares values&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description><link>http://cppsite.blogspot.com/2009/01/c-sets.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4884838640247142604</guid><pubDate>Tue, 13 Jan 2009 11:13:00 +0000</pubDate><atom:updated>2009-01-13T03:14:23.228-08:00</atom:updated><title>Insertion Sort Implementation in C++ [Sorting Algorithms]</title><description>&lt;p&gt;Insertion Sort is sorting algorithm similar to &lt;a href=&quot;http://talkbinary.com/2008/12/bubble-sort-c/&quot;&gt;Bubble Sort&lt;/a&gt; simply because they are basic sorting algorithms. The difference, is that Insertion Sort iterates through the data structure selects an element and inserts it into its correct location until no elements are left to sort.&lt;/p&gt; &lt;blockquote&gt; &lt;h3&gt;Insertion Sort Visual Example&lt;/h3&gt; &lt;table padding=&quot;0&quot; border=&quot;1&quot; bordercolor=&quot;black&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;26&lt;/td&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;-6&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;First Iteration&lt;/b&gt;&lt;br /&gt;The element 5  in position 1 is inserted into its appropriate location (position 0). &lt;/p&gt; &lt;table padding=&quot;0&quot; border=&quot;1&quot; bordercolor=&quot;black&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;-6&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;26 vs 5 equals SWAP&lt;/p&gt; &lt;p&gt;&lt;b&gt;Second Iteration&lt;/b&gt;&lt;br /&gt;The element 26 in position 2 is inserted into its appropriate location (position 1).&lt;/p&gt; &lt;table padding=&quot;0&quot; border=&quot;1&quot; bordercolor=&quot;black&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;td&gt;-6&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;26 &gt; 12 equals SWAP&lt;/p&gt; &lt;p&gt;&lt;b&gt;Third Iteration&lt;/b&gt;&lt;br /&gt;The element 26 in position 3 is in its appropriate position since it is greater than all the elements before it.&lt;/p&gt; &lt;table padding=&quot;0&quot; border=&quot;1&quot; bordercolor=&quot;black&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;td&gt;-6&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;12 &lt;&gt; &lt;p&gt;&lt;b&gt;Fourth Iteration&lt;/b&gt;&lt;br /&gt;The element -6 in position 3 is inserted into its appropriate position (position 0).&lt;/p&gt; &lt;table padding=&quot;0&quot; border=&quot;1&quot; bordercolor=&quot;black&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;-6&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;26 &gt; -6 SWAP&lt;br /&gt;&lt;span id=&quot;more-1463&quot;&gt;&lt;/span&gt;&lt;/p&gt; &lt;table padding=&quot;0&quot; border=&quot;1&quot; bordercolor=&quot;black&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;-6&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;12 &gt; -6 SWAP&lt;/p&gt; &lt;table padding=&quot;0&quot; border=&quot;1&quot; bordercolor=&quot;black&quot;&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td&gt;-6&lt;/td&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;5 &gt; -6 SWAP &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/p&gt; &lt;h3&gt;Insertion Sort Implementation in C++&lt;/h3&gt; &lt;p&gt;Below is the implementation of Insertion Sort in C++. The algorithm merely consists of inserting an element into its appropriate position by swapping it with the previous elements in the vector until it no longer is smaller than the element before it. Once inserted into its appropriate position, we look at the following element. &lt;/p&gt;      &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; temp; &lt;p&gt;    &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; i = &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;; i &lt; class=&quot;me1&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;)&lt;/span&gt;; i++ &lt;span class=&quot;br0&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;br0&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        temp = vec&lt;span class=&quot;br0&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;br0&quot;&gt;]&lt;/span&gt;;&lt;/p&gt; &lt;p&gt;        &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; j = i - &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;; j &gt;= &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;; j– &lt;span class=&quot;br0&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;br0&quot;&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;(&lt;/span&gt; temp &lt; class=&quot;br0&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;br0&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;)&lt;/span&gt;&lt;br /&gt;                swap&lt;span class=&quot;br0&quot;&gt;(&lt;/span&gt;vec&lt;span class=&quot;br0&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;nu0&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;]&lt;/span&gt;, vec&lt;span class=&quot;br0&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;br0&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;)&lt;/span&gt;;&lt;br /&gt;            &lt;span class=&quot;kw1&quot;&gt;else&lt;/span&gt;&lt;br /&gt;                &lt;span class=&quot;kw2&quot;&gt;break&lt;/span&gt;;&lt;br /&gt;        &lt;span class=&quot;br0&quot;&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p&gt;    &lt;span class=&quot;br0&quot;&gt;}&lt;/span&gt;&lt;/p&gt;</description><link>http://cppsite.blogspot.com/2009/01/insertion-sort-implementation-in-c.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-9065929193879932967</guid><pubDate>Sun, 11 Jan 2009 01:15:00 +0000</pubDate><atom:updated>2009-01-10T17:19:44.698-08:00</atom:updated><title>Polymorphism</title><description>Before getting into this section, it is recommended that you have a proper understanding of pointers and class inheritance. If any of the following statements seem strange to you, you should review the indicated sections:&lt;table class=&quot;boxed&quot;&gt; &lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Statement:&lt;/th&gt;&lt;td style=&quot;vertical-align: top;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;th&gt;Explained in:&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;int a::b(c) {};&lt;/td&gt;&lt;td style=&quot;vertical-align: top;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Classes&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;a-&gt;b&lt;/td&gt;&lt;td style=&quot;vertical-align: top;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Data Structures&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;class a: public b;&lt;/td&gt;&lt;td style=&quot;vertical-align: top;&quot;&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Friendship and inheritance&lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;Pointers to base class&lt;/h3&gt; One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that brings Object Oriented Methodologies to its full potential.&lt;p&gt;We are going to start by rewriting our program about the rectangle and the triangle of the previous section taking into consideration this pointer compatibility property:&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// pointers to base class&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;protected&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; width, height;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; set_values (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b)&lt;br /&gt;     { width=a; height=b; }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CRectangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area ()&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CTriangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area ()&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height / 2); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; CRectangle rect;&lt;br /&gt; CTriangle trgl;&lt;br /&gt; CPolygon * ppoly1 = &amp;rect;&lt;br /&gt; CPolygon * ppoly2 = &amp;trgl;&lt;br /&gt; ppoly1-&gt;set_values (4,5);&lt;br /&gt; ppoly2-&gt;set_values (4,5);&lt;br /&gt; cout &lt;&lt; rect.area() &lt;&lt; endl;&lt;br /&gt; cout &lt;&lt; trgl.area() &lt;&lt; endl;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;20&lt;br /&gt;10&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;In function &lt;tt&gt;main&lt;/tt&gt;, we create two pointers that point to objects of class &lt;tt&gt;CPolygon&lt;/tt&gt; (&lt;tt&gt;ppoly1&lt;/tt&gt; and &lt;tt&gt;ppoly2&lt;/tt&gt;). Then we assign references to &lt;tt&gt;rect&lt;/tt&gt; and &lt;tt&gt;trgl&lt;/tt&gt; to these pointers, and because both are objects of classes derived from &lt;tt&gt;CPolygon&lt;/tt&gt;, both are valid assignations.&lt;/p&gt;&lt;p&gt;The only limitation in using &lt;tt&gt;*ppoly1&lt;/tt&gt; and &lt;tt&gt;*ppoly2&lt;/tt&gt; instead of &lt;tt&gt;rect&lt;/tt&gt; and &lt;tt&gt;trgl&lt;/tt&gt; is that both &lt;tt&gt;*ppoly1&lt;/tt&gt; and &lt;tt&gt;*ppoly2&lt;/tt&gt; are of type &lt;tt&gt;CPolygon*&lt;/tt&gt; and therefore we can only use these pointers to refer to the members that &lt;tt&gt;CRectangle&lt;/tt&gt; and &lt;tt&gt;CTriangle&lt;/tt&gt; inherit from &lt;tt&gt;CPolygon&lt;/tt&gt;. For that reason when we call the &lt;tt&gt;area()&lt;/tt&gt; members at the end of the program we have had to use directly the objects &lt;tt&gt;rect&lt;/tt&gt; and &lt;tt&gt;trgl&lt;/tt&gt; instead of the pointers &lt;tt&gt;*ppoly1&lt;/tt&gt; and &lt;tt&gt;*ppoly2&lt;/tt&gt;.&lt;/p&gt;&lt;p&gt;In order to use &lt;tt&gt;area()&lt;/tt&gt; with the pointers to class &lt;tt&gt;CPolygon&lt;/tt&gt;, this member should also have been declared in the class &lt;tt&gt;CPolygon&lt;/tt&gt;, and not only in its derived classes, but the problem is that &lt;tt&gt;CRectangle&lt;/tt&gt; and &lt;tt&gt;CTriangle&lt;/tt&gt; implement different versions of &lt;tt&gt;area&lt;/tt&gt;, therefore we cannot implement it in the base class. This is when virtual members become handy:&lt;/p&gt;&lt;p&gt;&lt;a name=&quot;virtual&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;h3&gt;Virtual members&lt;/h3&gt; A member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare a member of a class as virtual, we must precede its declaration with the keyword &lt;tt&gt;virtual&lt;/tt&gt;:&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// virtual members&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;protected&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; width, height;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; set_values (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b)&lt;br /&gt;     { width=a; height=b; }&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area ()&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (0); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CRectangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area ()&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CTriangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area ()&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height / 2); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; CRectangle rect;&lt;br /&gt; CTriangle trgl;&lt;br /&gt; CPolygon poly;&lt;br /&gt; CPolygon * ppoly1 = &amp;rect;&lt;br /&gt; CPolygon * ppoly2 = &amp;trgl;&lt;br /&gt; CPolygon * ppoly3 = &amp;poly;&lt;br /&gt; ppoly1-&gt;set_values (4,5);&lt;br /&gt; ppoly2-&gt;set_values (4,5);&lt;br /&gt; ppoly3-&gt;set_values (4,5);&lt;br /&gt; cout &lt;&lt;&gt;area() &lt;&lt; endl;&lt;br /&gt; cout &lt;&lt;&gt;area() &lt;&lt; endl;&lt;br /&gt; cout &lt;&lt;&gt;area() &lt;&lt; endl;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;20&lt;br /&gt;10&lt;br /&gt;0&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Now the three classes (&lt;tt&gt;CPolygon&lt;/tt&gt;, &lt;tt&gt;CRectangle&lt;/tt&gt; and &lt;tt&gt;CTriangle&lt;/tt&gt;) have all the same members: &lt;tt&gt;width&lt;/tt&gt;, &lt;tt&gt;height&lt;/tt&gt;, &lt;tt&gt;set_values()&lt;/tt&gt; and &lt;tt&gt;area()&lt;/tt&gt;.&lt;/p&gt;&lt;p&gt;The member function &lt;tt&gt;area()&lt;/tt&gt; has been declared as virtual in the base class because it is later redefined in each derived class. You can verify if you want that if you remove this &lt;tt&gt;virtual&lt;/tt&gt; keyword from the declaration of &lt;tt&gt;area()&lt;/tt&gt; within &lt;tt&gt;CPolygon&lt;/tt&gt;, and then you run the program the result will be &lt;tt&gt;0&lt;/tt&gt; for the three polygons instead of &lt;tt&gt;20&lt;/tt&gt;, &lt;tt&gt;10&lt;/tt&gt; and &lt;tt&gt;0&lt;/tt&gt;. That is because instead of calling the corresponding &lt;tt&gt;area()&lt;/tt&gt; function for each object (&lt;tt&gt;CRectangle::area()&lt;/tt&gt;, &lt;tt&gt;CTriangle::area()&lt;/tt&gt; and &lt;tt&gt;CPolygon::area()&lt;/tt&gt;, respectively), &lt;tt&gt;CPolygon::area()&lt;/tt&gt; will be called in all cases since the calls are via a pointer whose type is &lt;tt&gt;CPolygon*&lt;/tt&gt;.&lt;/p&gt;&lt;p&gt;Therefore, what the &lt;tt&gt;virtual&lt;/tt&gt; keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class, as in the above example.&lt;/p&gt;&lt;p&gt;A class that declares or inherits a virtual function is called a &lt;i&gt;polymorphic class&lt;/i&gt;.&lt;/p&gt;&lt;p&gt;Note that despite of its virtuality, we have also been able to declare an object of type &lt;tt&gt;CPolygon&lt;/tt&gt; and to call its own &lt;tt&gt;area()&lt;/tt&gt; function, which always returns 0.&lt;/p&gt;&lt;p&gt;&lt;a name=&quot;abstract&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;h3&gt;Abstract base classes&lt;/h3&gt; Abstract base classes are something very similar to our &lt;tt&gt;CPolygon&lt;/tt&gt; class of our previous example. The only difference is that in our previous example we have defined a valid &lt;tt&gt;area()&lt;/tt&gt; function with a minimal functionality for objects that were of class &lt;tt&gt;CPolygon&lt;/tt&gt; (like the object &lt;tt&gt;poly&lt;/tt&gt;), whereas in an abstract base classes we could leave that &lt;tt&gt;area()&lt;/tt&gt; member function without implementation at all. This is done by appending &lt;tt&gt;=0&lt;/tt&gt; (equal to zero) to the function declaration. &lt;p&gt;An abstract base CPolygon class could look like this:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// abstract class CPolygon&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;protected&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; width, height;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; set_values (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b)&lt;br /&gt;     { width=a; height=b; }&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area () =0;&lt;br /&gt;};&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Notice how we appended &lt;tt&gt;=0&lt;/tt&gt; to &lt;tt&gt;virtual int area ()&lt;/tt&gt; instead of specifying an implementation for the function. This type of function is called a &lt;i&gt;pure virtual function&lt;/i&gt;, and all classes that contain at least one pure virtual function are &lt;i&gt;abstract base classes&lt;/i&gt;.&lt;/p&gt;&lt;p&gt;The main difference between an abstract base class and a regular polymorphic class is that because in abstract base classes at least one of its members lacks implementation we cannot create instances (objects) of it.&lt;/p&gt;&lt;p&gt;But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take advantage of all its polymorphic abilities. Therefore a declaration like: &lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;CPolygon poly;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;would not be valid for the abstract base class we have just declared, because tries to instantiate an object. Nevertheless, the following pointers:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;CPolygon * ppoly1;&lt;br /&gt;CPolygon * ppoly2;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;would be perfectly valid.&lt;/p&gt;&lt;p&gt;This is so for as long as &lt;tt&gt;CPolygon&lt;/tt&gt; includes a pure virtual function and therefore it&#39;s an abstract base class. However, pointers to this abstract base class can be used to point to objects of derived classes.&lt;/p&gt;&lt;p&gt;Here you have the complete example: &lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// abstract base class&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;protected&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; width, height;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; set_values (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b)&lt;br /&gt;     { width=a; height=b; }&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;) =0;&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CRectangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CTriangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height / 2); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; CRectangle rect;&lt;br /&gt; CTriangle trgl;&lt;br /&gt; CPolygon * ppoly1 = &amp;rect;&lt;br /&gt; CPolygon * ppoly2 = &amp;trgl;&lt;br /&gt; ppoly1-&gt;set_values (4,5);&lt;br /&gt; ppoly2-&gt;set_values (4,5);&lt;br /&gt; cout &lt;&lt;&gt;area() &lt;&lt; endl;&lt;br /&gt; cout &lt;&lt;&gt;area() &lt;&lt; endl;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;20&lt;br /&gt;10&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;If you review the program you will notice that we refer to objects of different but related classes using a unique type of pointer (&lt;tt&gt;CPolygon*&lt;/tt&gt;). This can be tremendously useful. For example, now we can create a function member of the abstract base class &lt;tt&gt;CPolygon&lt;/tt&gt; that is able to print on screen the result of the &lt;tt&gt;area()&lt;/tt&gt; function even though &lt;tt&gt;CPolygon&lt;/tt&gt; itself has no implementation for this function:&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// pure virtual members can be called&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;comm&quot;&gt;// from the abstract base class&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;protected&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; width, height;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; set_values (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b)&lt;br /&gt;     { width=a; height=b; }&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;) =0;&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; printarea (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { cout &lt;&lt; &lt;span class=&quot;kw&quot;&gt;this&lt;/span&gt;-&gt;area() &lt;&lt; endl; }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CRectangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CTriangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height / 2); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; CRectangle rect;&lt;br /&gt; CTriangle trgl;&lt;br /&gt; CPolygon * ppoly1 = &amp;rect;&lt;br /&gt; CPolygon * ppoly2 = &amp;trgl;&lt;br /&gt; ppoly1-&gt;set_values (4,5);&lt;br /&gt; ppoly2-&gt;set_values (4,5);&lt;br /&gt; ppoly1-&gt;printarea();&lt;br /&gt; ppoly2-&gt;printarea();&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;20&lt;br /&gt;10&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Virtual members and abstract classes grant C++ the polymorphic characteristics that make object-oriented programming such a useful instrument in big projects. Of course, we have seen very simple uses of these features, but these features can be applied to arrays of objects or dynamically allocated objects.&lt;/p&gt;&lt;p&gt;Let&#39;s end with the same example again, but this time with objects that are dynamically allocated:&lt;/p&gt;&lt;table class=&quot;codebox&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comm&quot;&gt;// dynamic allocation and polymorphism&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;prep&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;namespace&lt;/span&gt; std;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;protected&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; width, height;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; set_values (&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; b)&lt;br /&gt;     { width=a; height=b; }&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;) =0;&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt; printarea (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { cout &lt;&lt; &lt;span class=&quot;kw&quot;&gt;this&lt;/span&gt;-&gt;area() &lt;&lt; endl; }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CRectangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; CTriangle: &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt; CPolygon {&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;public&lt;/span&gt;:&lt;br /&gt;   &lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; area (&lt;span class=&quot;kw&quot;&gt;void&lt;/span&gt;)&lt;br /&gt;     { &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; (width * height / 2); }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kw&quot;&gt;int&lt;/span&gt; main () {&lt;br /&gt; CPolygon * ppoly1 = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CRectangle;&lt;br /&gt; CPolygon * ppoly2 = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CTriangle;&lt;br /&gt; ppoly1-&gt;set_values (4,5);&lt;br /&gt; ppoly2-&gt;set_values (4,5);&lt;br /&gt; ppoly1-&gt;printarea();&lt;br /&gt; ppoly2-&gt;printarea();&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;delete&lt;/span&gt; ppoly1;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;delete&lt;/span&gt; ppoly2;&lt;br /&gt; &lt;span class=&quot;kw&quot;&gt;return&lt;/span&gt; 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;result&quot;&gt;&lt;pre&gt;20&lt;br /&gt;10&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Notice that the &lt;tt&gt;ppoly&lt;/tt&gt; pointers:&lt;/p&gt;&lt;table class=&quot;snippet&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;CPolygon * ppoly1 = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CRectangle;&lt;br /&gt;CPolygon * ppoly2 = &lt;span class=&quot;kw&quot;&gt;new&lt;/span&gt; CTriangle;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;are declared being of type pointer to &lt;tt&gt;CPolygon&lt;/tt&gt; but the objects dynamically allocated have been declared having the derived class type directly.</description><link>http://cppsite.blogspot.com/2009/01/polymorphism.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4324673867277758489</guid><pubDate>Fri, 09 Jan 2009 07:33:00 +0000</pubDate><atom:updated>2009-01-08T23:34:12.387-08:00</atom:updated><title>Handling a Common strcmp() Case</title><description>In this section of the newsletter we will present some practical performance tips for improving code speed and reducing memory usage. Some of these tips will be useful only for C++ code and some will be more general and applicable to C or other languages.&lt;br /&gt;&lt;br /&gt;As a first example, consider an application using C-style strings and functions such as strcmp(). A recent experience with this sort of application involved a function that does word stemming, that is, takes words such as &quot;motoring&quot; and reduces them to their root stem, in this case &quot;motor&quot;.&lt;br /&gt;&lt;br /&gt;In profiling this function, it was observed that much of the overall time was being spent in the strcmp() function. For the C++ compiler in question (Borland 3.1), this function is written in assembly language and is quite fast, and attempts to speed it up by unrolling the equivalent code locally at the point of function call will typically result in slowing things down.&lt;br /&gt;&lt;br /&gt;But it&#39;s still the case that calling a function, even one implemented in assembly language, has some overhead, which comes from saving registers, manipulating stack frames, actual transfer of control, and so on. So it might be worth trying to exploit a common case -- the case where you can determine the relationship of the strings by looking only at the first character.&lt;br /&gt;&lt;br /&gt;So we might use an inline function in C++ to encapsulate this logic:&lt;br /&gt;&lt;br /&gt;        inline int local_strcmp(const char* s, const char* t)&lt;br /&gt;        {&lt;br /&gt;                return (*s != *t ? *s - *t : strcmp(s, t));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;If the first characters of each string do not match, there&#39;s no need to go further by calling strcmp(); we already know the answer.&lt;br /&gt;&lt;br /&gt;Another way to implement the same idea is via a C macro:&lt;br /&gt;&lt;br /&gt;        #define local_strcmp(s, t) ((s)[0] != (t)[0] ? (s)[0] - (t)[0] : \&lt;br /&gt;                strcmp((s), (t)))&lt;br /&gt;&lt;br /&gt;This approach has a couple of disadvantages, however. Macros are hard to get right because of the need to parenthesize arguments so as to avoid subtly wrong semantics. Writing local_strcmp() as a real function is more natural.&lt;br /&gt;&lt;br /&gt;And macros are less likely to be understood by development tools such as browsers or debuggers. Inline functions are also a source of problems for such tools, but they at least are part of the C++ language proper, and many C++ compilers have a way of disabling inlining to help address this problem.&lt;br /&gt;&lt;br /&gt;How much speedup is this approach good for? In the word stemming program, for input of about 65000 words, the times in seconds were:&lt;br /&gt;&lt;br /&gt;        strcmp()                9.7&lt;br /&gt;&lt;br /&gt;        inline local_strcmp()   7.5&lt;br /&gt;&lt;br /&gt;        #define local_strcmp()  7.5&lt;br /&gt;&lt;br /&gt;or a savings of about 23%. Obviously, this figure will vary with the compiler and the application.&lt;br /&gt;&lt;br /&gt;This particular speedup is achieved by exploiting a common case -- the case where the first letters of two strings are different. For applications involving English words, this is often a good assumption. For some other types of strings, it may not be.</description><link>http://cppsite.blogspot.com/2009/01/handling-common-strcmp-case.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-4940195164106966944</guid><pubDate>Fri, 09 Jan 2009 07:23:00 +0000</pubDate><atom:updated>2009-01-08T23:28:11.903-08:00</atom:updated><title>Character Types and Arrays</title><description>There are a couple of differences in the way that ANSI C and C++ treat character constants and arrays of characters. One of these has to do with the type of a character constant. For example:&lt;br /&gt;&lt;br /&gt;        #include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;        int main()&lt;br /&gt;        {&lt;br /&gt;                printf(&quot;%d\n&quot;, sizeof(&#39;x&#39;));&lt;br /&gt;&lt;br /&gt;                return 0;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;If this program is compiled as ANSI C, then the value printed will be sizeof(int), typically 2 on PCs and 4 on workstations. If the program is treated as C++, then the printed value will be sizeof(char), defined by the draft ANSI/ISO standard to be 1. So the type of a char constant in C is int, whereas the type in C++ is char. Note that it&#39;s possible to have sizeof(char) == sizeof(int) for a given machine architecture, though not very likely.&lt;br /&gt;&lt;br /&gt;Another difference is illustrated by this example:&lt;br /&gt;&lt;br /&gt;        #include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;        char buf[5] = &quot;abcde&quot;;&lt;br /&gt;&lt;br /&gt;        int main()&lt;br /&gt;        {&lt;br /&gt;                printf(&quot;%s\n&quot;, buf);&lt;br /&gt;&lt;br /&gt;                return 0;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;This is legal C, but invalid C++. The string literal requires a trailing \0 terminator, and there is not enough room in the character array for it. This is valid C, but you access the resulting array at your own risk. Without the terminating null character, a function like printf() may not work correctly, and the program may not even terminate.</description><link>http://cppsite.blogspot.com/2009/01/character-types-and-arrays.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4347663708493617035.post-3518039518567204943</guid><pubDate>Thu, 08 Jan 2009 10:51:00 +0000</pubDate><atom:updated>2009-01-08T02:51:46.366-08:00</atom:updated><title>QUEUES</title><description>There&#39;s a huge crowd at your local grocery store. There are too many people&lt;br /&gt;trying to buy their respective items and the Shopkeeper doesnt know from where&lt;br /&gt;to start. Everyone wants their job done quickly and the shopkeeper needs an&lt;br /&gt;efficient method to solve this problem. What does he do? He introduces a Queue&lt;br /&gt;System based on the First Come, First Serve System. The Last Person trying to&lt;br /&gt;buy an item stands behind the last person at the END of the queue. The&lt;br /&gt;Shopkeeper however is present at the FRONT end of the queue. He gives the item&lt;br /&gt;to the person in FRONT of the queue and after the transaction is done, the&lt;br /&gt;person in FRONT of the Queue Leaves. Then the person second in queue becomes the&lt;br /&gt;First person in the Queue.&lt;br /&gt;&lt;br /&gt;Do you get the point here? A Queue is similar to a stack except that addition of&lt;br /&gt;data is done in the BACK end and deletion of data is done in the FRONT.&lt;br /&gt;Writing a queue is a lot harder than writing a stack. We maintain 2 Integers in&lt;br /&gt;our Queue Data Structure, one signifying the FRONT end of the Queue and the&lt;br /&gt;other referring to the BACK end of the Queue.&lt;br /&gt;&lt;br /&gt;Let us use the same coding style as we used for the Stack. We first initialise&lt;br /&gt;both the ends to -1 to indicate an empty queue. When Data is added to the queue&lt;br /&gt;both ends get respective postive values. When New Data is added, the back End is&lt;br /&gt;incremented and when data is deleted the front end is decremented. This works&lt;br /&gt;fine but it has a major drawback. What if the Maximum capacity of the Queue is 5&lt;br /&gt;Items. Suppose the User has added 4 items, deleted 3 items and adds 2 again. The&lt;br /&gt;Stack wont permit him to add the last half of the data as it will report that&lt;br /&gt;the stack is full. The Reason is that we are blindly incrementing/decrementing&lt;br /&gt;each end depending on addition/deletion not realising that both the ends are&lt;br /&gt;related to each other. I leave this as an excercise for you to answer. Why will&lt;br /&gt;our proposed Stack report the Stack as Full even though it&#39;s actually vacant?&lt;br /&gt;So we need another approach.In this method we focus more on the data than on the&lt;br /&gt;addition end and the deletion end.&lt;br /&gt;&lt;br /&gt;What we now use is the grocery store example again. Suppose there are 5 items in&lt;br /&gt;a queue and we want to delete them one by one. We first delete the first data&lt;br /&gt;item pointed by the deletion end. Then we shift all data one step ahead so that&lt;br /&gt;the second item becomes the first, third item becomes second and so on...&lt;br /&gt;Another method would be to maintain the difference between the two ends which is&lt;br /&gt;not practical. Hence we stick to our previous method. It might be slow in Big&lt;br /&gt;Queues, but it certainly works great. Here&#39;s the code.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;codetop&quot;&gt;cpp&lt;/div&gt;&lt;pre class=&quot;codemain&quot;&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;com&quot;&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;using&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;com&quot;&gt;#define MAX 5           // MAXIMUM CONTENTS IN QUEUE&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; queue&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;MAX&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;com&quot;&gt;// Addition End&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;com&quot;&gt;// Deletion End&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;  queue&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; tmp&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;==-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;         cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot;Queue is Empty&quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; j&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;=&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;++)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;=&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;                    tmp&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;                    t&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;]=&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;                    al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;--;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;==-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;                    dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;                    dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; add&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; item&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;==-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;==-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;++;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;++;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;++;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;MAX&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot;Queue is Full\n&quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;--;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;               &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     t&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;al&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;]=&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; display&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;dl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;!=-&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;=&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;al &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;++)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;          cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;]&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;     cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot;EMPTY&quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;};&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; queue a&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; data&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;]={&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;};&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt; cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot;Queue before adding Elements: &quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; a&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;++)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   a&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;]);&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot;Addition Number : &quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot; : &quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   a&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot;Queue after adding Elements: &quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; a&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;++)&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   a&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot;Deletion Number : &quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;(&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;)&lt;&lt;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&quot; : &quot;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   a&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;   cout&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class=&quot;kwd&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt; &lt;/span&gt;&lt;span class=&quot;lit&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;pun&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;pln&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;Queue before adding Elements: EMPTY&lt;br /&gt;&lt;br /&gt;Addition Number : 1 : 32&lt;br /&gt;Addition Number : 2 : 32 23&lt;br /&gt;Addition Number : 3 : 32 23 45&lt;br /&gt;Addition Number : 4 : 32 23 45 99&lt;br /&gt;Addition Number : 5 : 32 23 45 99 24&lt;br /&gt;&lt;br /&gt;Queue after adding Elements: 32 23 45 99 24&lt;br /&gt;&lt;br /&gt;Deletion Number : 1 : 23 45 99 24&lt;br /&gt;Deletion Number : 2 : 45 99 24&lt;br /&gt;Deletion Number : 3 : 99 24&lt;br /&gt;Deletion Number : 4 : 24&lt;br /&gt;Deletion Number : 5 : EMPTY&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As you can clearly see through the output of this program that addition is&lt;br /&gt;always done at the end of the queue while deletion is done from the front end of&lt;br /&gt;the queue. Once again the Maximum limit of Data will be extended later when we&lt;br /&gt;learn Linked Lists.</description><link>http://cppsite.blogspot.com/2009/01/queues.html</link><author>noreply@blogger.com (Rachifu)</author><thr:total>0</thr:total></item></channel></rss>