[doHTML]<BODY link="#000000"> <b><font size="6"><i>Beginner's Guide to C++<br> </i></font><hr size="2" color="#000000"> <BR> <i><font size="5">Introduction</font></i><BR> <font face="Verdana" size="2">This tutorial is a step by step walkthrough for C++. It takes a person with absolutely no knowledge or limited knowledge of C++ and will assist then in building their skills up to an intermediate level.</font><p> <font face="Verdana" size="2">Even if you have read some other C++ tutorials I would advise that you have a look through this one as it deals with things that are breaching on intermediate-advanced C++ skills. There are also some things not heavily if at all covered in other tutorials.&nbsp; Some of the things included in this tutorial are: Time Delay Loops, switch statement, nested statements and using arrays. </font></p> <p><font face="Verdana" size="2">If you have absolutely no idea about C++ and this is the first thing you have come across that's ok as well. This tutorial will start at the absolute basics and slowly work it's way up.</font></p> <p><font face="Verdana" size="2">This tutorial is free and took a lot of time and effort, please don't leave criticism about this tutorial and it's content unless you have something solid to ground it upon. All I ask from readers of this tutorial is a rating on <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=http://www.pscode.com">www.pscode.com</a>&nbsp; (rating system at the very bottom of the page). Please enjoy the tutorial and give me hints and suggestions as I will be continually updating&nbsp; and revising this tutorial (see version number at the top of the page).</font></p> <p><font size="5"><i>What you will need<br> </i></font><font size="2" face="Verdana">To do any of the coding examples in this tutorial you will need to get your hands on a C++ compiler. There are many great compilers out there including dev-C++ and Visual C++. If you are using a Linux or a Unix system you will find that you already have a C++ compiler installed (In Linux it is under the programming menu).</font></p> <p><font face="Verdana" size="2">If you are a Windows user and you don't want to have to fork out the money for a compiler then do a search for Dev-C++ (<a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=http://www.bloodshed.net">http://www.bloodshed.net</a> I think...) as this compiler is free and simple to use.</font></p> <p><font face="Verdana" size="2">You are also going to need a lot of patience. C++ is a complicated language but people are often scared off by others exaggerating the true difficulty of C++ to make others have a higher opinion of them. If you take the time and follow this tutorial step by step you will be writing workable C++ code in next to no time.</font></p> <p><font size="5"> <i>If a program exits without showing you the output it generated...<br> </i></font><font size="2" face="Verdana">This is something people seem to have a lot of trouble with on planet-source-code (<a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=http://www.pscode.com">www.pscode.com</a>). If you want the easy way out, get Visual C++ as a compiler as it will automatically pause the program for you when all processing is complete. If you aren't using VC++ however you will need to do the following to every program that you create...</font></p> <p><font face="Verdana" size="2">First you need to include the following line at the top of ever project that you create:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber36"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;stdlib.h&gt;</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">You will also need to add the following line in place of &quot;</font><font size="2" face="Courier">return 0;</font><font face="Verdana" size="2">&quot; and at the end of any project you create:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber37"> <tr> <td width="100%"><font face="Courier" size="2">system(&quot;pause&quot;);</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">&nbsp;Got that? Hopefully that simplifies everything and actually lets you see what you have created... If you are still having problems please don't hesitate to drop me an e-mail (address in the header of this tutorial).</font></p> <p><font face="Verdana"><font size="5"><b>Table of Contents<br> </b></font><b><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#THEBASICS">The Basics</a></b></font></p> <ul> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#HelloWorld">HelloWorld, your first C++ program</a></font></li> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#Commenting">Commenting and it's importance</a></font></li> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#Operators">Operators</a></font><br> &nbsp;&nbsp;&nbsp; <font face="Verdana" size="1">1. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#ArithmeticOperators">Arithmetic Operators</a><br> &nbsp;&nbsp;&nbsp; 2. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#Assignments">Increment and Decrement</a><br> &nbsp;&nbsp;&nbsp; 3. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#Assignments">Assignments</a><br> &nbsp;&nbsp;&nbsp; 4. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#RelationalOperators">Relational Operators</a><br> &nbsp;&nbsp;&nbsp; 5. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#LogicalOperators">Logical Operators</a></font></li> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#OrderofOperations">Order of operations</a></font></li> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#Themainevent">The &quot;main&quot; event</a></font></li> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#IntroducingVariable">All about &quot;cout&quot; and &quot;cin&quot;</a></font></li> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#IntroducingVariable">The &quot;rand&quot; function</a></font></li> <li><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#IntroducingVariable">Introducing variables</a></font><br> &nbsp;&nbsp;&nbsp; <font face="Verdana" size="1">1. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#GlobalVariables"> Local Variables</a></font><br> &nbsp;&nbsp;&nbsp; <font face="Verdana" size="1">2. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#GlobalVariables"> Global Variables</a></font></li> <li><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#SomevariableDataTypes"><font face="Verdana" size="2">V</font></a><font face="Verdana" size="2"><a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#SomevariableDataTypes">ariable data types</a></font><br> &nbsp;&nbsp;&nbsp; <font face="Verdana" size="1">1. <a href="http://brainmeta.com/redirect/redirlink2/redir.php?id=#GlobalVariables"> Basic Data Types</a></font></li> </ul> <p><font face="Verdana"><b>Further Along... </b></font></p> <ul> <li><font face="Verdana" size="2">All about #include</font></li> <li><font face="Verdana" size="2">The &quot;if&quot; statement</font></li> <li><font face="Verdana" size="2">&quot;if&quot;, &quot;else&quot; and &quot;else if&quot;</font></li> <li><font face="Verdana" size="2">What's my age program part #2</font></li> <li><font face="Verdana" size="2">The &quot;switch&quot; statement</font></li> <li><font face="Verdana" size="2">Math program part #2</font></li> </ul> <p><font face="Verdana"><b>Loops</b></font></p> <ul> <li><font face="Verdana" size="2">The &quot;for&quot; loop</font></li> <li><font face="Verdana" size="2">The infinite loop</font></li> <li><font face="Verdana" size="2">Time delay loops</font></li> <li><font face="Verdana" size="2">10 second count-down program</font></li> <li><font face="Verdana" size="2">The while loop</font></li> <li><font face="Verdana" size="2">The do-while loop</font></li> <li><font face="Verdana" size="2">Number guessing game</font></li> </ul> <p><b><font face="Verdana">Functions</font></b></p> <ul> <li><font face="Verdana" size="2">Using Functions</font></li> <li><font face="Verdana"><font size="2">Formal Parameters</font></font></li> </ul> <p><font face="Verdana"><b>Nested Statements</b></font></p> <ul> <li><font face="Verdana" size="2">What are nested statements?</font></li> <li><font face="Verdana" size="2">Nested &quot;if&quot; and &quot;switch&quot; statements</font></li> <li><font face="Verdana" size="2">Nested Loops<i><br> &nbsp;</i></font></li> </ul> <p><font size="5"><font face="Arial Black"><a name="THEBASICS">THE BASICS</a></font><i><br> <a name="HelloWorld">HelloWorld, your first C++ program</a><br> </i></font><font size="2" face="Verdana">Alright my figuring is your reading this tutorial to learn to program some C++ code right? Anyway I figure I should start off with some code but if you don't really understand it don't go worrying yourself as everything used in this simple program is covered in &quot;the basics&quot; section of this tutorial.</font></p> <div align="left"> <font face="Verdana" size="1"><b>HelloWorld.cpp</b></font><table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;iostream.h&gt; <font color="#008000">//adds a header file that someone else has created</font><br> <br> <font color="#0000FF">void</font> main()<br> {<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;HELLO WORLD!!!&quot; &lt;&lt; endl; <font color="#008000">//put this text to the screen</font><br> }</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">Now we will start to break down the code. For starters, take this line into account-</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber2"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;iostream.h&gt; <font color="#008000">//adds a header file that someone else has created</font></font></td> </tr> </table> </div> <p><font face="Verdana" size="2">What this does is tell the program to use a &quot;header file&quot; that someone else has created. It can also be written as:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber3"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;iostream&gt;</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">But for now lets avoid doing that and stick to the basics. You may also notice the text after the &quot;//&quot;. These are comments, basically little notes that a programmer leaves for himself and others to help them understand the code. These are an excellent practice and can be added at the end of any line. More on commenting later on in this tutorial.</font></p> <p><font face="Verdana" size="2">Now we will look at the following-</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber4"> <tr> <td width="100%"><font face="Courier" size="2"> <font color="#0000FF">void</font> main()</font></td> </tr> </table> </div> </BODY> </html><body link="#000000"> <p><font face="Verdana" size="2">If you have ever done any programming before you will have a fair idea about &quot;Procedures&quot;. In C++ &quot;main&quot; is always the first procedure executed in any program in C++ however we call these &quot;functions&quot;.</font></p> <p><font face="Verdana" size="2">The purpose of</font><font face="Courier" size="2"> &quot;<font color="#0000FF">void</font>&quot;</font><font face="Verdana" size="2"> is simply to tell the computer that main won't return a value. More on </font><font face="Courier" size="2"> &quot;<font color="#0000FF">void</font>&quot; </font> <font face="Verdana" size="2">and returning values later on.</font></p> <p><font face="Verdana" size="2">Next you will notice a &quot;{&quot; on the fourth line down. In C++ &quot;{&quot; and &quot;}&quot; group procedures and statements, you will understand this a bit more as you work your way through this tutorial. For now you may notice that all code is grouped inside of these. This is a very important thing to remember.</font></p> <p><font face="Verdana" size="2">Now lets look at the line that prints our text to the screen.</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber5"> <tr> <td width="100%"><font face="Courier" size="2">cout &lt;&lt; &quot;HELLO WORLD!!!&quot; &lt;&lt; endl; <font color="#008000">//put this text to the screen</font></font></td> </tr> </table> </div> <p><font face="Courier" size="2">&quot;cout&quot; </font> <font face="Verdana" size="2">pronounced &quot;see-out&quot; in C++ is a function that outputs text to the screen. It is covered along with it's partner &quot;cin&quot; further along in this tutorial. For now what you need to know is that it can print text onto the screen. The</font><font face="Courier" size="2"> &quot;endl&quot; </font> <font face="Verdana" size="2">text tells the computer to put any text after a</font><font face="Courier" size="2"> &quot;endl&quot; </font> <font face="Verdana" size="2">function onto a new line.</font></p> <p><font face="Verdana" size="2">Try this now, type in the following and run it-</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber6"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;iostream.h&gt; <font color="#008000">//adds a header file that someone else has created</font><br> <br> <font color="#0000FF">void</font> main() <font color="#008000">//the main procedure (also the first executed)</font><br> {<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot; One&quot; &lt;&lt; endl;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Two&quot; &lt;&lt; endl;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Three&quot; &lt;&lt; endl &lt;&lt; &quot;Line Four&quot; &lt;&lt; endl;<br> }</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">If you execute this you will notice that you get something like the following:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber7"> <tr> <td width="100%" bgcolor="#000000"> <font face="Courier New" color="#FFFFFF" size="2">Line One<br> Line Two<br> Line Three<br> Line Four</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">Also take notice that I finished the last line off with an &quot;endl&quot;. This is a good practice because it means that later on in the program if &quot;cout&quot; is used again you won't get a lot of code on one line. You will also need this is the console is being pause as the computer will automatically print the text needed for this on the next available line.</font></p> <p><font face="Verdana" size="2">There is also a substitute to the &quot;endl&quot; function. This is &quot;\n&quot; basically it is a parameter of &quot;cout&quot;. Try the following:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber8"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include </font>&lt;iostream.h&gt; <font color="#008000">//adds a header file that someone else has created<br> </font><br> <font color="#0000FF">void </font>main() <font color="#008000">//the main procedure (also the first executed)</font><br> {<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot; One\n&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Two\n&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Three\n&quot; &lt;&lt; &quot;Line Four\n&quot;;<br> }</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">You should see the same output as before except now we are using &quot;\n&quot; instead of &quot;endl&quot;. Both have the same purpose and assist you in your coding. Knowing both will greatly speed up your coding.</font></p> <p><font size="5"><i><a name="Commenting">Commenting and its importance</a></i><br> </font><font size="2" face="Verdana">If you look at the code that we have already covered you will notice that there is a lot of green text around the place. These are called &quot;comments&quot;. Basically they assist the programmer in browsing code. They are also a necessity if you plan to write code for others as comments greatly assist other programmers interpreting your code more clearly.</font></p> <p><font face="Verdana" size="2">Their are two types of comments, &quot;group comments&quot; and &quot;line comments&quot;. Group comments are comments that can have any number of lines in them. Usually you will see these int the header of a C++ file. Line comments can be added onto any line however, if a line comment precedes any code on a line the that line of code will be ignored completely by the compiler and won't be executed...</font></p> <p><font face="Verdana" size="2">In a majority of compilers comments show up as either a light or dark green. Comments are only seen in the coding process. In fact most compilers will strip out comments during the compiling process. Because of this don't worry about comments adding onto a programs size, use them as often as you can!</font></p> <p><font face="Verdana" size="2">Have a look at the following:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber9"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#008000">/* <br> Title: &quot;cout&quot; example project<br> Author: Michael Skelton<br> e-mail: skelmirc@hotmail.com<br> */</font><br> <font color="#0000FF">#include</font> &lt;iostream.h&gt;<font color="#008000"> //adds a header file that someone else has created<br> </font><br> <font color="#0000FF">void</font> main() <font color="#008000">//the main function (also the first executed)</font><br> {<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot; One\n&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Two\n&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Three\n&quot; &lt;&lt; &quot;Line Four\n&quot;;<br> <font color="#008000"><span style="background-color: #FFFFFF">//this comment has no purpose :)</span></font><br> }</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">Notice the first line opens up with &quot;</font><font color="#008000" face="Courier" size="2">/*</font><font size="2" face="Verdana">&quot; This is the opening for a multilane comment. Once this is added everything after it becomes a comment until the &quot;</font><font color="#008000" face="Courier" size="2">*/</font><font face="Verdana" size="2">&quot; is added. Hence this is why they are very handy for the beginning of a file as in a lot of cases you might want to add quite a bit of information including a description or something of the sort.</font></p> <p><font face="Verdana" size="2">Now lets look at one liner comments. As you have probably already figured out they start off with &quot;</font><span style="background-color: #FFFFFF"><font color="#008000"><font face="Courier" size="2">//</font></font><font face="Verdana" size="2">&quot; and don't have a closing operator. They can be added onto any line whether it is blank or not as long as they are added after any code on that line. If we don't do this then the code we have after the comment becomes part of the comment itself.</font></span></p> <p><span style="background-color: #FFFFFF"><font face="Verdana" size="2"> Comments can also prove very handy if there is some code that isn't working and for the time being don't want it included in the compilation process. For example lets assume in our previous &quot;cout example project&quot; their was an error on the &quot;</font><font size="2" face="Courier">cout &lt;&lt; &quot;Line&quot;;</font><font face="Verdana" size="2">&quot; that we just couldn't figure out at the moment. To compile the project successfully all we have to do is to comment it out. An example of this is below:</font></span></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber10"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#008000">/* <br> Title: &quot;cout&quot; example project<br> Author: Michael Skelton<br> e-mail: skelmirc@hotmail.com<br> */</font><br> <font color="#0000FF">#include</font> &lt;iostream.h&gt;<font color="#008000"> //adds a header file that someone else has created<br> </font><br> <font color="#0000FF">void</font> main() <font color="#008000">//the main procedure (also the first executed)</font><br> {<br> <font color="#008000">//cout &lt;&lt; &quot;Line&quot;;</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot; One\n&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Two\n&quot;;<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Line Three\n&quot; &lt;&lt; &quot;Line Four\n&quot;;<br> <font color="#008000"><span style="background-color: #FFFFFF">//this comment has no purpose :)</span></font><br> }</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">Notice that the line now becomes a comment itself and thus is ignored when we compile the project. Applying this practise can be very handy when trying to isolate errors in your coding.</font></p> <p><font size="5"><i><a name="Operators">Operators</a><br> </i></font><font size="2" face="Verdana">C++ is loaded with built in operators. An operator is a character that tells the compiler to perform specific mathematical or logical manipulations. C++ has three main categories of operators: arithmetic, relational and logical. Don't worry if you can't understand what an operator does from the table, they are simply their for future reference and their individual explanations are below the tables.</font></p> <div align="left" style="width: 1154; height: 227"> <font face="Verdana" size="4"><b><a name="ArithmeticOperators">Arithmetic Operators</a></b></font><table border="1" cellpadding="4" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="84%" id="AutoNumber11" height="170"> <tr> <td width="9%" bgcolor="#000000" height="16"> <font face="Verdana" size="2" color="#FFFFFF">Operator</font></td> <td width="91%" bgcolor="#000000" height="16"> <font face="Verdana" size="2" color="#FFFFFF">Action</font></td> </tr> <tr> <td width="9%" height="1"><b>-</b></td> <td width="91%" height="1"><font face="Verdana" size="2">Subtraction, otherwise known as the &quot;minus&quot; sign</font></td> </tr> <tr> <td width="9%" height="18"><font face="Verdana" size="2">+</font></td> <td width="91%" height="18"><font face="Verdana" size="2">Addition</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">*</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Multiplication</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">/</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Division</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">%</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Modulus</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">--</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Decrement</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">++</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Increment</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">+=</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Addition assignment</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">-=</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Subtraction assignment</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">*=</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Multiplication assignment</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">/=</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Division assignment</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">%=</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Modular assignment</font></td> </tr> </table> </div> <p><font face="Verdana" size="2"><br> The operators +,-,* and / all work the same way in C++ as they do in any other computing language or algebra. They can be applied to any built in data type allowed by C++. When &quot;/&quot; is applied to an integer or a character any remainder will be truncated; for example, 10/3 will equal 3 in integer division.</font></p> <p><font face="Verdana" size="2">The modulus operator (%) also works in C++ the way that it does in other languages. Remember that the modulus operation yields the remainder of an integer division. This means that the % cannot be used with <b>float</b> or <b>double</b> (more on this later). The following program shows its use.</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber12"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include </font>&lt;iostream.h&gt;</font><p><font face="Courier" size="2"> <font color="#0000FF">void</font> main() <font color="#008000">//notice no &quot;void&quot; this time, we don't always need it. More on this later</font><br> {<br> &nbsp;&nbsp;&nbsp; int x, y; <font color="#008000">//declare two integer variables (x and y) more on this later</font><br> &nbsp;&nbsp;&nbsp; x = 10; <font color="#008000">//set x to 10, more on this later</font><br> &nbsp;&nbsp;&nbsp; y = 3; <font color="#008000">//set y to 3, more on this later</font><br> &nbsp;&nbsp;&nbsp; cout &lt;&lt; x/y; <font color="#008000">//will display 3</font><br> &nbsp;&nbsp;&nbsp; cout &lt;&lt; &quot;\n&quot;; <font color="#008000">//put the following on a new line (remember this from earlier on?)</font><br> &nbsp;&nbsp;&nbsp; cout &lt;&lt; x%y; <font color="#008000">//displays the remainder of the division</font><br> &nbsp;&nbsp;&nbsp; cout &lt;&lt; &quot;\n&quot;; <font color="#008000">//put the following on a new line</font><br> &nbsp;&nbsp;&nbsp; x = 1; <font color="#008000">//set x to 1, more on this later</font><br> &nbsp;&nbsp;&nbsp; y = 2;<font color="#008000"> //set y to 2, more on this later</font><br> &nbsp;&nbsp;&nbsp; cout &lt;&lt; x/y &lt;&lt; &quot; &quot; &lt;&lt; x%y; <font color="#008000">//will display 0 1</font><br> }</font><font face="Verdana" size="2">&nbsp;&nbsp;&nbsp; </font></td> </tr> </table> </div> <p><font face="Verdana" size="2">The reason the last line prints a 0 and a 1 is because 1/2 in integer division is 0 with a remainder of 1. Therefore, 1%2 gives us the remainder of 1.</font></p> <p><font face="Verdana" size="4"><b><a name="IncrementDecrement">Increment and Decrement</a><br> </b></font><font face="Verdana" size="2">C++ has two operators not generally found in other computer languages. These are the increment and decrement operators, ++ and --. The ++ operator adds 1 to it's operand, and -- subtracts 1.</font></p> <p><font face="Verdana" size="2">For example, x = x+1; is the same as ++x; and x = x-1; is the same as --x; . If you are a bit confused at the moment read the &quot;variables&quot; section of this tutorial and come back to this section. Both the increment and the decrement operators can either precede (prefix) or follow (postfix) the operand. For example, x = x+1; can be written as ++x; (prefix form) or as: x++; (postfix form).</font></p> <p><font face="Verdana" size="2">In the foregoing example there is no difference whether increment is applied as a prefix or postfix. However, when an increment or decrement is used as part of a larger expression, there is an important difference. When an increment or decrement precedes its operand. C++ will perform the corresponding operation prior to obtaining the operand's value. If the operator follows it;s operand then C++ will obtain the operand's value before incrementing or decrementing it. Consider the following:</font></p> <p><font face="Verdana" size="2">x = 10;<br> y = ++x;</font></p> <p><font face="Verdana" size="2">In this case, <b>y</b> will be set to 11. However if the code is written as:</font></p> <p><font face="Verdana" size="2">x = 10;<br> y = x++;</font></p> <p><font face="Verdana" size="2">Then <b>y</b> will be set to 10. In both cases, <b>x</b> is still set to 11; the difference is when it happens. There are significant advantages in being able to control when the increment or decrement takes place.</font></p> <p><font face="Verdana" size="2">Most C++ compilers write very fast, efficient object code for increment and decrement operations that is better then the code generated when the corresponding assignment statement is used. Therefore, it is a good idea to use increment and decrement operators whenever you can.</font></p> <p><b><font face="Verdana" size="4"><a name="Assignments">Assignments</a></font></b><br> <font face="Verdana" size="2">Assignments in C++ like Increment and Decrement operators simplify our coding when working with variables. In simple terms say we had the following line in a program which we had created:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber27"> <tr> <td width="100%"><font face="Courier" size="2">x=x+1</font></td> </tr> </table> </div> <div align="left" style="width: 1154; height: 227"> <br> <font face="Verdana" size="2">We could using an assignment operator simplify this line down to the following:<br> &nbsp;</font><div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber28"> <tr> <td width="100%"><font face="Courier" size="2">x+=1</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">Simple huh? The same works with the other assignment operators. You cannot however use &quot;x=+1&quot; as your compiler will recognise this as &quot;x equals positive one&quot;. See the problem there?<br> Lets try another assignment operator, say we had the following:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber29"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;iostream.h&gt;<br> <br> <font color="#0000FF">void</font> main()<br> {<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>int x,y; <font color="#008000">//declare two integer variables, x and y</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>x=40; <font color="#008000">//set x to 40</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>y=60; <font color="#008000">//set y to 60</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>x=x+1; <font color="#008000">//add 1 to x</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>y=y/2; <font color="#008000">//divide y by 2</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;x equals: &quot; &lt;&lt; x &lt;&lt; &quot; and y equals: &quot; &lt;&lt; y &lt;&lt; endl;<br> }</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">This could easily be simplified down to:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber30"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;iostream.h&gt;<br> <br> <font color="#0000FF">void</font> main()<br> {<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>int x,y; <font color="#008000">//declare two integer variables, x and y</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>x=40; <font color="#008000">//set x to 40</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>y=60; <font color="#008000">//set y to 60</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>x+=1; <font color="#008000">//add 1 to x</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>y/=2; <font color="#008000">//divide y by 2</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;x equals: &quot; &lt;&lt; x &lt;&lt; &quot; and y equals: &quot; &lt;&lt; y &lt;&lt; endl;<br> }</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">Both do exactly the same thing except the second is clearer and makes your code more efficient. If you are planning to get fairly full on with C++ then this would defiantly be a good practise to take up as it saves valuable coding time and makes your code look a lot more professional.<br> </font><br> <a name="RelationalOperators"> <b><font face="Verdana" size="4">Relational Operators</font></b></a></p> <table border="1" cellpadding="4" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="84%" id="AutoNumber13" height="202"> <tr> <td width="9%" bgcolor="#000000" height="16"> <font face="Verdana" size="2" color="#FFFFFF">Operator</font></td> <td width="91%" bgcolor="#000000" height="16"> <font face="Verdana" size="2" color="#FFFFFF">Action</font></td> </tr> <tr> <td width="9%" height="19"><b>&gt;</b></td> <td width="91%" height="19"><font face="Verdana" size="2">Greater than</font></td> </tr> <tr> <td width="9%" height="18"><font face="Verdana" size="2">&gt;=</font></td> <td width="91%" height="18"><font face="Verdana" size="2">Greater than or equal to</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">&lt;</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Less than</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">&lt;=</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Less than or equal to</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">==</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Equal to</font></td> </tr> <tr> <td width="9%" height="18"><font face="Verdana" size="2">!=</font></td> <td width="91%" height="18"><font face="Verdana" size="2">Not equal to</font></td> </tr> </table> </div> <div align="left" style="width: 1154; height: 97"> <b><font face="Verdana" size="4"><a name="LogicalOperators">Logical Operators</a></font></b><table border="1" cellpadding="4" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="84%" id="AutoNumber14" height="1"> <tr> <td width="9%" bgcolor="#000000" height="16"> <font face="Verdana" size="2" color="#FFFFFF">Operator</font></td> <td width="91%" bgcolor="#000000" height="16"> <font face="Verdana" size="2" color="#FFFFFF">Action</font></td> </tr> <tr> <td width="9%" height="1"><b>&amp;&amp;</b></td> <td width="91%" height="1"><font face="Verdana" size="2">And</font></td> </tr> <tr> <td width="9%" height="1"><font face="Verdana" size="2">||</font></td> <td width="91%" height="1"><font face="Verdana" size="2">Or</font></td> </tr> <tr> <td width="9%" height="19"><font face="Verdana" size="2">!</font></td> <td width="91%" height="19"><font face="Verdana" size="2">Not</font></td> </tr> </table> </div> <p><font face="Verdana" size="2">In the terms <i>relational operator</i> and <i> logical operator</i>, <i>relational</i> refers to the relationships which values can have with one another, and <i>logical</i> refers to the ways these relationships can be connected together. Because these relational and logical operators often work together they will be discussed together below.</font></p> <p><font face="Verdana" size="2">The key to using the relational and logical operators is the idea of <i>true versus false</i>. In C++, true is any value other than 0. False is 0. Thus, only expressions that evaluate to 0 are false. Any other value is true. As you learn more about C++, you will see that this concept of true and false makes certain algorithms much easier to write.</font></p> <p><font face="Verdana" size="2">Although C++ does not contain built-in exclusive-OR (XOR) logical operator, it is easy to construct one.</font></p> <p><font face="Verdana" size="2">A lot of people seem to have problems remembering the difference between less than and greater than. The way I was taught to remember it is to look to the L in less than. Have a look at it again, do you see what looks like an L angling down? Just a handy trick if you can't tell the two of them apart. :)</font></p> <p><font size="5"><i><a name="OrderofOperations">Order of Operations</a><br> </i></font><font size="2" face="Verdana">Order of operations is the order in which the computer interprets you operators and mathematical code. Basically it works the same way as mathematics does.</font></p> <p><font face="Verdana" size="2">C++ first evaluates things that are grouped by parentheses (brackets like the things that surround this text). It then evaluates things that are being multiplied and divided, from left to right. Next, it evaluates additions and subtractions.</font></p> <p><font face="Verdana" size="2">For example:</font></p> <p><font face="Verdana" size="2">(5+3)/2 * 5 + 6 - 12 / 4</font></p> <p><font face="Verdana" size="2">The computer first does the stuff in parentheses, changing the expression to this:</font></p> <p><font face="Verdana" size="2">8/2 * 5 + 6 - 12/4</font></p> <p><font face="Verdana" size="2">Next, it does all multiplications and divisions from left to right, getting:</font></p> <p><font face="Verdana" size="2">4 * 5 + 6 - 12/4<br> 20 + 6 - 12/4<br> 20 + 6 - 3<br> <br> Finally, it does the additions and subtractions from left to right:</font></p> <p><font face="Verdana" size="2">20 + 6 - 3<br> 26 - 3</font></p> <p><font face="Verdana" size="2">And gets the final answer,</font></p> <p><font face="Verdana" size="2">23</font></p> <p><font face="Verdana" size="2">WALA! Hopefully that cleared thing up for you a bit. And if your still at school this also applies in algebra so it's well worth remembering.</font></p> <p><font face="Verdana" size="2"><i>***Please note that adding spaces in mathematics in C++ in most cases makes no difference, it just makes code easier to interpret so try not to get confused about the spacing system used aove.***</i></font></p> <p><font size="5"><i><a name="Themainevent">The &quot;main&quot; event</a><br> </i></font><font size="2" face="Verdana">By now you have seen quite a fair bit of C++ code, if you look at everything so far you will see &quot;main()&quot; around the place. In C++ &quot;main()&quot; is as we stated earlier the first function executed in any C++ application. </font></p> <p><font face="Verdana" size="2">Unlike some programming languages that always begin execution at the &quot;top&quot; of the program, C++ begins every program with a call to the &quot;main()&quot; function, no matter where that function is located in the program. (However it is a good practice to be the first function in your program so it can be easily found.)</font></p> <p><font face="Verdana" size="2">There can only be one &quot;main()&quot; in a program. If you try to include more than one, your program will not know where to begin execution. Most compilers would catch this error however and notify you.</font></p> <p><font size="5"><i><a name="Allaboutcoutandcin">All about &quot;cout&quot; and &quot;cin&quot;</a><br> </i></font><font size="2" face="Verdana">Already we have used &quot;cout&quot; (pronounced See-Out) in our example programs and looked at some ways to utilise it's use. There is however more to all of this. C++ has another function called &quot;cin&quot; (pronounced see-in).</font></p> <p><font face="Verdana" size="2">If you intend on using either of these you will have to tell the compiler to include iostream.h upon compilation, without this your program won't work. To do this simply add the line &quot;#include &lt;iostream.h&gt;&quot; at the head of your document, without the double quotes of course. You can see examples of this line used in the examples we did earlier.</font></p> <p><font face="Verdana" size="2">As we have already covered the main sections of &quot;cout&quot; we will now look at &quot;cin&quot; and it's uses. &quot;cin&quot; works alongside &quot;cout&quot; but instead of printing output onto the screen it takes input and puts it into a variable. To use this you first need to have a basic understanding of variables (further down in this tutorial). Once you have mastered &quot;cin&quot; and &quot;cout&quot; you will easily be able to create menus in the console, always make sure you remember to use &quot;endl&quot; though as without this you will encounter all sorts of problems...</font></p> <p><font face="Verdana" size="2">Below is an example of how &quot;cin&quot; is used to give you a clearer idea:</font></p> <div align="left"> <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber15"> <tr> <td width="100%"><font face="Courier" size="2"><font color="#0000FF"> #include</font> &lt;iostream.h&gt;<br> <br> <font color="#0000FF">void</font> main()<br> {<br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; int</font> x,y; <font color="#008000">//declare two variables x and y</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cout &lt;&lt; &quot;Enter a number to double: &quot;; <font color="#008000">//this is just the text before input</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>cin &gt;&gt; x; <font color="#008000">//this puts our input into variable x</font><br> <font color="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp; </font>y = x*2; <font color="#008000">//works out the answer and put it into variable y</font><br> <font color="#0000FF">&nbsp;&nbsp;&

C++ beginning is been stated from the C language means if people is been updated with the C language than he will be able to grap the C++ very fast.C++ is been the language said to be most efficient and beginning for every language the base for all language.

C++ beginning is been stated from the C language means if people is been updated with the C language than he will be able to grap the C++ very fast.C++ is been the language said to be most efficient and beginning for every language the base for all language.