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

<channel>
	<title>SDLTutorials.com &#187; Blue Dino Code</title>
	<atom:link href="http://www.sdltutorials.com/category/blue-dino-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sdltutorials.com</link>
	<description>SDL Tutorials - Game Tutorials - Programming Tutorials</description>
	<lastBuildDate>Tue, 20 Jul 2010 19:56:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>A Guide to Graphics with Sprig</title>
		<link>http://www.sdltutorials.com/a-guide-to-graphics-with-sprig/</link>
		<comments>http://www.sdltutorials.com/a-guide-to-graphics-with-sprig/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 16:38:55 +0000</pubDate>
		<dc:creator>Jonny D</dc:creator>
				<category><![CDATA[Blue Dino Code]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=207</guid>
		<description><![CDATA[The following tutorial was created by Blue Dino Code, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework [...]]]></description>
			<content:encoded><![CDATA[<p>The following tutorial was created by <a href="http://code.bluedinosaurs.com/">Blue Dino Code</a>, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. SDLTutorials.com does not endorse or reject any of the coding practices outlined in the tutorial, and is not responsible for any code or files belonging to this tutorial that harms your computer.</p>
<hr/><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">A Guide to Graphics with SPriG</span></div>
<p><span style="font-weight: bold;">Prerequisites</span>:<br />
Some knowledge of SDL (see <a href="http://code.bluedinosaurs.com/installSDL.html">Installing SDL</a>)</p>
<p>The SDL Primitive Generator, <a href="http://code.bluedinosaurs.com/SPriG.html">SPriG</a>, is a quick and simple interface to making graphics on SDL surfaces.  In this guide, I&#8217;ll be discussing the notable features of Sprig with usage examples.  Not all of the functions are described here.  There are plenty more to be found in the documentation once you get the hang of the stuff found here.  I will, however, be describing the entire dirty rect interface.</p>
<p><span id="more-207"></span></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Contents</span></div>
<table style="text-align: left; width: 100%;" border="0" cellspacing="2" cellpadding="2">
<tbody>
<tr>
<td style="width: 302px;"></td>
<td style="width: 316px;"><a href="#Introduction">Introduction</a><br />
<a href="#History">History</a><br />
<a href="#Installation">Installation</a><br />
<a href="#Surface">Surface Commands</a><br />
<a href="#Primitives">Graphics Primitives</a><br />
<a href="#Polygons">Polygons</a><br />
<a href="#Controls">Rendering Controls</a><br />
<a href="#Drawing">Image Drawing</a><br />
<a href="#Angles">Angles</a><br />
<a href="#Transformations">Surface Transformations</a><br />
<a href="#Dirty">Dirty Rects</a></td>
<td style="width: 302px;"></td>
</tr>
</tbody>
</table>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Introduction<a name="Introduction"></a></span></div>
<p><br/><br />
<img alt="Intro" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/intro.png" title="Intro" class="aligncenter" width="600" height="163" /><br />
Sprig does three main things:  It draws graphical primitives, it rotates and scales surfaces, and it simplifies access to SDL surfaces.  Once you&#8217;re off and using Sprig, I suggest that you download the documentation so you have easy reference material at hand.</p>
<p>Along the way, I&#8217;ll be leaving usage examples.  In these, I&#8217;ll be using <a href="http://code.bluedinosaurs.com/SDL.html">SDL_ColorDef</a> to define colors, so if you see something like this:</p>
<div class="dean_ch" style="white-space: wrap;">RGB_BLUE<span class="br0">&#40;</span>mySurface<span class="br0">&#41;</span></div>
<p>This is just a quick replacement for the usual format-independent call in SDL:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_MapRGB<span class="br0">&#40;</span>mySurface-&gt;format, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">255</span><span class="br0">&#41;</span></div>
<p>Isn&#8217;t that convenient?  So, instead of describing a color with red, green, and blue components, I&#8217;ll just use common names.<br/></p>
<div style="text-align: center;"><span style="color: #006600;"> </span><span style="text-decoration: underline;">History and Motivation<a name="History"></a></span></div>
<p><br/><br />
<img alt="history" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/history.png" title="history" class="aligncenter" width="400" height="300" /><br />
Sprig was originally an overhaul of SGE.  I called it minSGE back then because I was not satisfied with how many peripheral features are in SGE.  I cut away collision detection, fonts, C++ sprite classes, and text classes.  These features are handled far better by other libraries and would be quite the hassle to maintain and upgrade properly.  I assumed that most people used SGE for primitives and transforms and used other libraries for all the other stuff.  A major annoyance to me were the naming conventions, so I worked on that, too.  Well, that set the stage for designing Sprig.  In fact, I thought that fonts were important enough that I also started NFont as a successor to SFont for drawing bitmap fonts.  Sprig and NFont together are all that a simple game needs to get started.  These decisions also made it easy for Sprig to become a pure C library, so that users of either C or C++ can enjoy it.</p>
<p>I hope you&#8217;ll find that Sprig has a very comfortable feel in its naming scheme and range of convenient functions.  My main goal besides providing primitives and transforms is to supplement the built-in SDL functions to make development faster with less typing.<br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Installation<a name="Installation"></a></span></div>
<p><br/><br />
<img alt="installation" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/installation.png" title="installation" class="aligncenter" width="400" height="338" /><br />
Installation for Sprig is just like any other C library.  You can check out the SDL installation guide for generic details (<a href="http://code.bluedinosaurs.com/installSDL.html">Installing SDL</a>).  For Sprig, you can get the pre-built binaries, use &#8216;make&#8217;, or build it by hand.</p>
<p>First of all, <a href="http://code.bluedinosaurs.com/SPriG.html">download</a> the library, of course.  If you have the binaries for your system, unzip them.  Copy &#8216;sprig.h&#8217; and &#8216;sprig_inline.h&#8217; to your compiler&#8217;s /include or /include/SDL folder.  Then copy the appropriate library file (libsprig.a, libsprig.so, sprig.lib) to your compiler&#8217;s /lib folder.  Now you can write Sprig programs by using:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co2">#include &quot;sprig.h&quot;</span></div>
<p>and linking with sprig (-lsprig or sprig.lib).</p>
<p>When you distribute these programs, be sure to include the dynamic library appropriate to your system (sprig.dll, libsprig.so).</p>
<p>Using make (Linux):<br />
Download the Sprig source, open a terminal window, and move into the unzipped Sprig source directory.  Type:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">make</span> <span class="kw2">install</span></div>
<p>That should be it!</p>
<p>Building by hand:<br />
Download the Sprig source and unzip it.  Make a new dynamic library project in your favorite compiler that contains all of the source files (.c).  Add SDLmain and SDL to your linker options.  Compile it and install it as above.</p>
<p>If you&#8217;re willing to comply with the LGPL license on static linking, you can build a static link library or just add all of the Sprig source files to your project.  You will then not need to distribute the library with your executable.</p>
<p>If you have any problems or are not on Windows XP or Debian GNU/Linux, then please send me an email!<br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Lurking Beneath the Surface<a name="Surface"></a></span></div>
<p>Sprig&#8217;s surface functions make creation and control of SDL surfaces quite easy.  Sprig has functions to set up SDL, create surfaces, and copy surfaces.</p>
<table style="text-align: left; text-valign: top; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>SDL_Surface*</td>
<td>SPG_InitSDL(Uint16 w, Uint16 h, Uint8 bitsperpixel, Uint32 systemFlags, Uint32 screenFlags)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_CreateSurface32(Uint32 flags, Uint16 width, Uint16 height)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_CopySurface(SDL_Surface* surface)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_Free(SDL_Surface* surface)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_SetColorkey(SDL_Surface* surface, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_SetClip(SDL_Surface* surface, const SDL_Rect clipRect)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_RestoreClip(SDL_Surface* surface)</td>
</tr>
</tbody>
</table>
<p>SPG_Init is a wrapper for calls to SDL_Init and SDL_SetVideoMode.  It returns the display surface or NULL if there&#8217;s an error.  Just like with SDL_SetVideoMode, passing 0 for the surface depth will give you the best available depth.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* screen = SPG_InitSDL<span class="br0">&#40;</span><span class="nu0">800</span>, <span class="nu0">600</span>, <span class="nu0">0</span>, SDL_INIT_VIDEO, SDL_SWSURFACE | SDL_FULLSCREEN<span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>screen == <span class="kw2">NULL</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw3">exit</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;</div>
<p>SPG_CreateSurface32 is how you can create 32-bit RGBA surfaces with minimal work.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* mySurf = SPG_CreateSurface32<span class="br0">&#40;</span>SDL_SWSURFACE, <span class="nu0">100</span>, <span class="nu0">100</span><span class="br0">&#41;</span>;</div>
<p>SPG_CopySurface returns a duplicate of the given surface.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* copy = SPG_CopySurface<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span>;</div>
<p>SPG_Free is a wrapper for SDL_FreeSurface.  It&#8217;s just for less typing, nothing special.  When you&#8217;re done using a surface, you must free the memory using SPG_Free (or SDL_FreeSurface).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Free<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span>;</div>
<p>SPG_SetColorkey sets the transparent color for a given surface.  Colorkey transparency can only be used for RGB-&gt;RGB blits, RGB-&gt;RGBA blits, or RGBA-&gt;RGBA blits with SDL_SRCALPHA disabled.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_SetColorkey<span class="br0">&#40;</span>mySurf, RGB_BRIGHTPINK<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SDL_BlitSurface<span class="br0">&#40;</span>mySurf, <span class="kw2">NULL</span>, screen, <span class="kw2">NULL</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Only non-pink areas will be drawn.</span></div>
<p>SPG_SetClip sets the clipping rectangle for the given surface.  When stuff is drawn to this surface, only the parts inside the clipping rect will actually be drawn.  You can reset the clipping rect by using SPG_RestoreClip.  This is really useful for splitscreen effects.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect clipRect = <span class="br0">&#123;</span><span class="nu0">40</span>, <span class="nu0">40</span>, <span class="nu0">60</span>, <span class="nu0">100</span><span class="br0">&#125;</span>;<br />
SPG_SetClip<span class="br0">&#40;</span>mySurf, clipRect<span class="br0">&#41;</span>;<br />
SPG_RestoreClip<span class="br0">&#40;</span>mySurf<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Reset it</span></div>
<p><br/><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">As Primitive as Can Be<a name="Primitives"></a></span></div>
<p><br/><br />
<img alt="primitives" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/primitives.png" title="primitives" class="aligncenter" width="400" height="300" /><br />
Sprig is first and foremost a generator for graphics primitives (of course).  These are pixel shapes like lines, circles, and rectangles.  Sprig has functions to create:</p>
<p>Pixels<br />
Lines<br />
Rectangles<br />
Circles<br />
Ellipses<br />
Arcs<br />
Bezier curves<br />
Trigons (Triangles)<br />
Polygons</p>
<p>Each of these primitives can be drawn with anti-aliasing, different alpha-blending modes, and a specific thickness.  Rects, circles, ellipses, arcs, trigons, and polygons can all be drawn with filled interiors.  Check out the Controls section for more info on changing how primitives are drawn.</p>
<p>The naming convention that Sprig follows is like so:<br />
SPG_(Shape)(Filled)(Blend)</p>
<p>For example:<br />
SPG_RectFilled<br />
SPG_PixelBlend<br />
SPG_EllipseFilledBlend</p>
<p>The different words in each function name follow an intuitive hierarchical naming scheme, based on the magnitude of the change they make to the shape.  This is especially useful when combined with many IDEs&#8217; autocompletion to save you from lots of typing.</p>
<p>Here are some sample drawing commands.  The signature of each function can be found in the documentation.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Rect<span class="br0">&#40;</span>screen, <span class="nu0">40</span>, <span class="nu0">60</span>, <span class="nu0">140</span>, <span class="nu0">100</span>, RGB_RED<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SPG_CircleFilledBlend<span class="br0">&#40;</span>screen, <span class="nu0">300</span>, <span class="nu0">100</span>, <span class="nu0">20</span>, RGB_PURPLE<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>, <span class="nu0">200</span><span class="br0">&#41;</span>;<br />
SPG_Line<span class="br0">&#40;</span>screen, <span class="nu0">0</span>, <span class="nu0">0</span>, screen-&gt;w<span class="nu0">-1</span>, screen-&gt;h<span class="nu0">-1</span>, RGB_GRAY<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SPG_ArcFilled<span class="br0">&#40;</span>screen, <span class="nu0">200</span>, <span class="nu0">500</span>, <span class="nu0">40</span>, <span class="nu0">-90</span>, <span class="nu0">45</span>, RGB_YELLOW<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
SPG_PixelBlend<span class="br0">&#40;</span>screen, <span class="nu0">60</span>, <span class="nu0">60</span>, RGB_WHITE<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>, <span class="nu0">100</span><span class="br0">&#41;</span>;</div>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Two Sides of the Same Polygon<a name="Polygons"></a></span></div>
<p><br/><br />
<img alt="polygons" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/polygons.png" title="polygons" class="aligncenter" width="400" height="300" /><br />
Polygons are great for many effects and can represent platforms or form characters in various video games.  With Sprig&#8217;s polygon rotations, you can easily make something like the opening screen for Zelda III&#8230;  Some functions use the SPG_Point coordinate structure to simplify their calls.  SPG_Point contains two floating point values, x and y.  Use SPG_MakePoint to define your own points for these functions.  You can also fill the struct when it&#8217;s created:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Point p = <span class="br0">&#123;</span><span class="nu0">50</span>, <span class="nu0">40</span><span class="br0">&#125;</span>;</div>
<p>The functions:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>void</td>
<td>SPG_Trigon(SDL_Surface* surface, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_QuadTexPoints(SDL_Surface* dest, SPG_Point* dest_points, SDL_Surface* source, SPG_Point* source_points)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_Polygon(SDL_Surface* surface, Uint16 n, SPG_Point* points, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_CopyPoints(Uint16 n, SPG_Point* points, SPG_Point* buffer)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_RotatePoints(Uint16 n, SPG_Point* points, float angle)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_ScalePoints(Uint16 n, SPG_Point* points, float xscale, float yscale)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_SkewPoints(Uint16 n, SPG_Point* points, float xskew, float yskew)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_TranslatePoints(Uint16 n, SPG_Point* points, float dx, float dy)</td>
</tr>
</tbody>
</table>
<p>SPG_Trigon draws a triangle from three points.  Just like other primitive shapes, it can be a line drawing or variable thickness, filled, alpha-blended, or anti-aliased.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Trigon<span class="br0">&#40;</span>screen, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">50</span>, <span class="nu0">10</span>, <span class="nu0">20</span>, <span class="nu0">20</span>, RGB_ORANGE<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>SPG_QuadTexPoints draws a four-sided polygon that is filled with an image.  A set of source points allows you to choose exactly what part of the source image is used.  This can be used for stretching, rotating, and distorting images.  There is another function, SPG_QuadTex, that uses (x,y) coordinates instead of SPG_Points.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_QuadTexPoints<span class="br0">&#40;</span>screen, myQuad, myImage, otherQuad<span class="br0">&#41;</span>;</div>
<p>SPG_Polygon and all of its variants draw a multi-sided shape using SPG_Point values.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Polygon<span class="br0">&#40;</span>screen, <span class="nu0">6</span>, myHexagon, RGB_TEAL<span class="br0">&#40;</span>screen<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>SPG_CopyPoints makes a static copy of your points into the given buffer.  The result can be messed with, drawn using SPG_Polygon, then deleted without ruining your original shape.  It uses a pre-existing buffer, so it&#8217;s nice and fast.  You can also use this to copy part of a polygon into another polygon with some pointer arithmetic.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Point buffer<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span>;<br />
SPG_CopyPoints<span class="br0">&#40;</span><span class="nu0">6</span>, myHexagon, buffer<span class="br0">&#41;</span>;</p>
<p>SPG_CopyPoints<span class="br0">&#40;</span><span class="nu0">3</span>, myTrigon, <span class="br0">&#40;</span>myHexagon + <span class="nu0">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// Copy points into myHexagon[3], [4], and [5]</span></div>
<p>SPG_RotatePoints rotates each SPG_Point about the origin by the specified angle.  To rotate about a different point, you can use SPG_RotatePointsXY (or translate it back and forth).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_RotatePoints<span class="br0">&#40;</span><span class="nu0">5</span>, myPentagon, <span class="nu0">45</span><span class="br0">&#41;</span>;</div>
<p>SPG_ScalePoints can make a polygon bigger or smaller.  It scales about the origin (i.e. assumes the center of the polygon is at (0,0)), but you can use SPG_ScalePointsXY (or translate it back and forth) to change that.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_ScalePoints<span class="br0">&#40;</span><span class="nu0">5</span>, myPentagon, <span class="nu0">2</span>.0f, <span class="nu0">2</span>.0f<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Double the size</span></div>
<p>SPG_SkewPoints applies a shear transform to the polygon (about the origin, as above &#8211; Try out SPG_SkewPointsXY too).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_SkewPoints<span class="br0">&#40;</span><span class="nu0">4</span>, leaningTower, <span class="nu0">2</span>.0f, <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;</div>
<p>SPG_TranslatePoints moves all the points in a polygon.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_TranslatePoints<span class="br0">&#40;</span><span class="nu0">4</span>, myBox, <span class="nu0">200</span>, <span class="nu0">400</span><span class="br0">&#41;</span>;</div>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Those Controls are Stacked!<a name="Controls"></a></span></div>
<p>Several options are controlled through a stack system.  When you change an option, you push a new value onto the stack.  When you&#8217;re done, you pop the stack, returning it to the old state.  In this way, you can write a block of code or a function that changes the state to achieve a particular effect, then returns it to the old state for the rest of the program to use.</p>
<p>There are three stacks held by Sprig.  The thickness stack controls the thickness of any line primitives that are drawn (default: 1).  The blending stack determines the special blending mode for alpha-blended primitives and SPG_Blit (default: SPG_DEST_ALPHA).  The anti-alias stack controls whether graphics primitives are anti-aliased (smoothed) or not (default: 0).  The surface alpha stack controls whether or not SPG_Blit combines the per-surface alpha with the per-pixel alpha (default: 0).</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>void</td>
<td>SPG_PushThickness(Uint16 state)</td>
</tr>
<tr>
<td>Uint16</td>
<td>SPG_PopThickness()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_PushBlend(Uint8 state)</td>
</tr>
<tr>
<td>Uint8</td>
<td>SPG_PopBlend()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_PushAA(SPG_bool state)</td>
</tr>
<tr>
<td>SPG_bool</td>
<td>SPG_PopAA()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_PushSurfaceAlpha(SPG_bool state)</td>
</tr>
<tr>
<td>SPG_bool</td>
<td>SPG_PopSurfaceAlpha()</td>
</tr>
</tbody>
</table>
<p>Here&#8217;s a table of the available blending modes:</p>
<table style="text-align: left; margin-right: auto; margin-left: auto; width: auto;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr style="text-align: center">
<td><span>Name</span></td>
<td><span>RGB</span></td>
<td><span>Alpha</span></td>
<td>Description</td>
</tr>
<tr>
<td><span>SPG_DEST_ALPHA</span></td>
<td><span>Blend</span></td>
<td><span>Dest</span></td>
<td>Blends colors normally and keeps the destination per-pixel alpha.  This is the same as SDL_BlitSurface&#8217;s blending.</td>
</tr>
<tr>
<td><span>SPG_SRC_ALPHA</span></td>
<td><span>Blend</span></td>
<td><span>Src</span></td>
<td>Blends colors and copies the source alpha.</td>
</tr>
<tr>
<td><span>SPG_COMBINE_ALPHA</span></td>
<td><span>Blend</span></td>
<td><span>Blend</span></td>
<td>Blends colors and blends the alpha.  This is &#8220;True&#8221; blending and lends well to some neat compositing effects.</td>
</tr>
<tr>
<td><span>SPG_COPY_NO_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Opaque</span></td>
<td>Copies the source color, sets the dest alpha to opaque.</td>
</tr>
<tr>
<td><span>SPG_COPY_DEST_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Dest</span></td>
<td>Copies the source color, keeps the dest alpha.</td>
</tr>
<tr>
<td><span>SPG_COPY_SRC_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Src</span></td>
<td>Copies the source color and alpha.</td>
</tr>
<tr>
<td><span>SPG_COPY_COMBINE_ALPHA</span></td>
<td><span>Src</span></td>
<td><span>Blend</span></td>
<td>Copies the source color and blends the alpha.</td>
</tr>
<tr>
<td><span>SPG_COPY_ALPHA_ONLY</span></td>
<td>Dest</td>
<td>Src</td>
<td>Keeps the dest color, but copies the source alpha.</td>
</tr>
<tr>
<td>SPG_COMBINE_ALPHA_ONLY</td>
<td>Dest</td>
<td>Blend</td>
<td>Keeps the dest color, but blends the alpha.</td>
</tr>
<tr>
<td>SPG_REPLACE_COLORKEY</td>
<td>Src</td>
<td>Src</td>
<td>If the destination surface has the SDL_SRCCOLORKEY flag, this replaces the dest colorkey color in the image with the source color and alpha.  This is similar to palette-swapping, but can be used with gradients and other images.</td>
</tr>
</tbody>
</table>
<p>Some examples:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_PushBlend<span class="br0">&#40;</span>SPG_COMBINE_ALPHA<span class="br0">&#41;</span>;<br />
SPG_PushAA<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;<br />
SPG_PushThickness<span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span>;</p>
<p>SPG_PopThickness<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
SPG_PopAA<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
SPG_PopBlend<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p><br/><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">All the Pi in the World&#8230;<a name="Angles"></a></span></div>
<p>Sprig can work with both degree angle measure and radians.  By default, all functions use the more common (in everyday life) units of degrees.  You can change this behavior with a call like:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_EnableRadians<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;</div>
<p>This avoids one floating-point multiplication in some of Sprig&#8217;s internal conversions (Sprig uses radians internally).  To make radian measure easier to use, there are several #defines involving pi:<br />
PI_8, PI_4, PI_2, PI3_4, PI, PI5_4, PI3_2, PI7_4, PI2</p>
<p>A number after &#8216;PI&#8217; indicates multiplication, and an underscore means division.  So PI3_2 is &#8220;Three halves times pi&#8221; or 1.5*pi.  An amount equal to pi radians is equivalent to 180 degrees.  Every quarter value of pi (45 degree increments) up to 2*pi (360 degrees) is provided.  Sprig also provides conversion factors for converting between degrees and radians:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw4">float</span> radians = degrees * RADPERDEG;</div>
<p>or</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw4">float</span> degrees = radians * DEGPERRAD;</div>
<p>These #defines are named after the unit conversion, which is a fraction.  The measure that you&#8217;re converting to will be in the numerator (top), with the old unit in the denominator (bottom).</p>
<div style="text-align: center;"><span style="text-decoration: underline;">More Than Meets the Eye<a name="Transformations"></a></span></div>
<p>Sprig transformation routines can scale, rotate, and flip SDL surfaces.</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>SDL_Surface*</td>
<td>SPG_Transform(SDL_Surface *src, Uint32 bcol, float angle, float xscale, float yscale, Uint8 flags)</td>
</tr>
<tr>
<td>SDL_Rect</td>
<td>SPG_TransformX(SDL_Surface *src, SDL_Surface *dst, float angle, float xscale, float yscale, Uint16 pivotX, Uint16 pivotY, Uint16 destX, Uint16 destY, Uint8 flags)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_Rotate(SDL_Surface *src, float angle, Uint32 bgColor)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_RotateAA(SDL_Surface *src, float angle, Uint32 bgColor)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_Scale(SDL_Surface* src, float xscale, float yscale)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_ScaleAA(SDL_Surface* src, float xscale, float yscale)</td>
</tr>
</tbody>
</table>
<p>Anti-aliasing is handled a little differently for the transform functions than for primitives.  This is because they operate on a different level: surfaces rather than shapes.  You don&#8217;t usually want your choices for one to affect your results for the other.  For this reason, SPG_Transform and SPG_TransformX use bit flags to control their behavior:<br />
SPG_NONE &#8211; Normal, same as 0.<br />
SPG_TAA &#8211; Anti-aliasing (no more jaggies)<br />
SPG_TSAFE &#8211; Makes incompatible surface depths work right<br />
SPG_TTMAP &#8211; Uses texture mapping (SPG_QuadTex) for a faster, but uglier result<br />
SPG_TSLOW &#8211; Uses a slow transformation, but is very accurate<br />
SPG_TCOLORKEY &#8211; Ignores the colorkey value on the destination surface<br />
SPG_TBLEND &#8211; Alpha-blend on the destination surface<br />
SPG_TSURFACE_ALPHA &#8211; Blend with the per-surface alpha</p>
<p>SPG_Rotate and SPG_Scale are convenience functions that call SPG_TransformX.  So, to simply rotate an image:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* rotated = SPG_Rotate<span class="br0">&#40;</span>myImage, <span class="nu0">60</span>, RGB_BLACK<span class="br0">&#40;</span>myImage<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>C++ users can omit the background color argument (defaults to 0&#215;000000).</p>
<p>To scale a surface:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* scaled = SPG_Scale<span class="br0">&#40;</span>myImage, <span class="nu0">0</span>.5f, <span class="nu0">0</span>.5f<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Quarter the size (half each dimension)</span></div>
<p>To flip a surface horizontally:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Surface* flipped = SPG_Scale<span class="br0">&#40;</span>myImage, <span class="nu0">-1</span>, <span class="nu0">1</span><span class="br0">&#41;</span>;</div>
<p>SPG_TransformX gives you the most control over your surfaces.  Let&#8217;s look at it closely:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody>
<tr>
<td>SDL_Rect SPG_TransformX(SDL_Surface *src, SDL_Surface *dst, float angle, float xscale, float yscale, Uint16 pivotX, Uint16 pivotY, Uint16 destX, Uint16 destY, Uint8 flags);</td>
</tr>
</tbody>
</table>
<p>This function takes a surface, transforms it, and draws it onto another surface.  The first two arguments are the source surface and the destination surface.  The third argument is a rotation angle.  The next two are scaling factors.  The sixth and seventh are pivot point coordinates.  These determine the point about which the surface is to be rotated.  The eighth and ninth arguments are the drawing destination coordinates.  The last is a bitwise OR &#8216;|&#8217; combination of transformation flags.</p>
<div style="text-align: center;"><span style="text-decoration: underline;">Draw Me Close&#8230;<a name="Drawing"></a></span></div>
<p>Sprig has plenty of handy drawing functions.  These give you quick control over SDL surfaces.  The ones you should take note of are:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>Uint32</td>
<td>SPG_GetPixel(SDL_Surface* surface, Sint16 x, Sint16 y)</td>
</tr>
<tr>
<td>Uint32</td>
<td>SPG_ConvertColor(SDL_PixelFormat* srcfmt, Uint32 srccolor, SDL_PixelFormat* destfmt)</td>
</tr>
<tr>
<td>SDL_Rect</td>
<td>SPG_MakeRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h)</td>
</tr>
<tr>
<td>int</td>
<td>SPG_Blit(SDL_Surface* Src, SDL_Rect* srcRect, SDL_Surface* Dest, SDL_Rect* destRect)</td>
</tr>
<tr>
<td>SDL_Surface*</td>
<td>SPG_ReplaceColor(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dest, SDL_Rect* destrect, Uint32 color)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_Draw(SDL_Surface* source, SDL_Surface* dest, Sint16 x, Sint16 y)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DrawCenter(SDL_Surface* source, SDL_Surface* dest, Sint16 x, Sint16 y)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_FloodFill(SDL_Surface* dst, Sint16 x, Sint16 y, Uint32 color)</td>
</tr>
</tbody>
</table>
<p>SPG_GetPixel returns the color of a target pixel, like so:</p>
<div class="dean_ch" style="white-space: wrap;">Uint32 color = SPG_GetPixel<span class="br0">&#40;</span>mySurface, x, y<span class="br0">&#41;</span>;</div>
<p>Use SPG_ConvertColor to change a color from one format to another:</p>
<div class="dean_ch" style="white-space: wrap;">Uint32 screenBrown = SPG_ConvertColor<span class="br0">&#40;</span>mySurf-&gt;format, myBrown, screen-&gt;format<span class="br0">&#41;</span>;</div>
<p>In many functions, like SDL_BlitSurface, you use an SDL_Rect to define the source pixels or to indicate the drawing location.  SPG_MakeRect can be used to make rects for such use:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect rect = SPG_MakeRect<span class="br0">&#40;</span><span class="nu0">40</span>, <span class="nu0">30</span>, <span class="nu0">60</span>, <span class="nu0">60</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// A 60&#215;60 rect at the location (40,30)</span></div>
<p>SPG_Blit draws an image using Sprig&#8217;s custom blitter.   It&#8217;s pretty slow, but allows you to use several alpha-blending modes.</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect location = <span class="br0">&#123;</span><span class="nu0">50</span>, <span class="nu0">50</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#125;</span>;<br />
SPG_Blit<span class="br0">&#40;</span>mySource, <span class="kw2">NULL</span>, myDest, &amp;location<span class="br0">&#41;</span>;</div>
<p>SPG_ReplaceColor lets you blit an image only to specific colored areas on the destination surface.  This is an effect similar to the palette-swapping that tons of old 8-bit games used to make identical enemies of different colors.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_ReplaceColor<span class="br0">&#40;</span>mySource, <span class="kw2">NULL</span>, myDest, <span class="kw2">NULL</span>, color<span class="br0">&#41;</span>;</div>
<p>SPG_Draw is a quick wrapper call to SDL_BlitSurface.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_Draw<span class="br0">&#40;</span>mySurf, screen, x, y<span class="br0">&#41;</span>;</div>
<p>SPG_DrawCenter is just like SPG_Draw except that it uses the width and height of the source surface in order to draw it centered on the given coordinates.</p>
<p>SPG_FloodFill fills in the target area (all pixels of the same color) with the given color.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_FloodFill<span class="br0">&#40;</span>mySurf, x, y, color<span class="br0">&#41;</span>;</div>
<p><br/></p>
<div style="text-align: center;"><span style="text-decoration: underline;">Those Low-down Dirty Rects!<a name="Dirty"></a></span></div>
<p><br/><br />
<img alt="dirty rects" src="http://code.bluedinosaurs.com/SDL/SPriG/screenshots/dirty.png" title="dirty rects" class="aligncenter" width="400" height="300" /><br />
If a game is changing the entire screen every frame, it makes sense to update the entire screen.  But if you&#8217;re only moving a few sprites on a static background, then a huge time savings can be made by updating only the &#8220;dirty&#8221; parts.  Sprig includes a dirty rect system to make this easy to do.  David Olofson&#8217;s code from Fixed Rate Pig (<a href="http://olofson.net/">http://olofson.net/</a>) turned out to be a great and simple implementation, so he deserves all the credit for what ended up in Sprig.</p>
<p>The complete Dirty system consists of these calls:</p>
<table style="text-align: left; width: 100%;" border="1" cellspacing="2" cellpadding="2">
<tbody style="text-align: left;" valign="top">
<tr>
<td>void</td>
<td>SPG_EnableDirty(SPG_bool enable)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyInit(Uint16 maxsize)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyAdd(SDL_Rect* rect)</td>
</tr>
<tr>
<td>SPG_DirtyTable*</td>
<td>SPG_DirtyUpdate(SDL_Surface* screen)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtySwap()</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>SPG_bool</td>
<td>SPG_DirtyEnabled()</td>
</tr>
<tr>
<td>SPG_DirtyTable*</td>
<td>SPG_DirtyMake(Uint16 maxsize)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyAddTo(SDL_Rect* rect, SPG_DirtyTable* table)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyFree(SPG_DirtyTable* table)</td>
</tr>
<tr>
<td>SPG_DirtyTable*</td>
<td>SPG_DirtyGet()</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyClear(SPG_DirtyTable* table)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyLevel(Uint16 optimizationLevel)</td>
</tr>
<tr>
<td>void</td>
<td>SPG_DirtyClip(SDL_Surface* screen, SDL_Rect* rect)</td>
</tr>
</tbody>
</table>
<p>The first group up there are the important ones.  To start, call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_EnableDirty<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;</div>
<p>This tells all of the primitives to output dirty rects.  These rects are collected by the &#8216;front&#8217; dirty table.  There are two of these tables, the &#8216;front&#8217; and the &#8216;back&#8217;.  Everything happens on the front table and the back table is used to save old dirty rects for an extra frame.  This is necessary since you must update the new position of a sprite to show where it is, but also the old position in order to show empty space where it once was.  The definition of the tables looks like this:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw4">typedef</span> <span class="kw4">struct</span> SPG_DirtyTable<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Uint16 size; &nbsp;<span class="coMULTI">/* Table size */</span><br />
&nbsp; &nbsp; SDL_Rect *rects; &nbsp;<span class="coMULTI">/* Table of rects */</span><br />
&nbsp; &nbsp; Uint16 count; <span class="coMULTI">/* # of rects currently used */</span><br />
&nbsp; &nbsp; Uint16 best; &nbsp;<span class="coMULTI">/* Merge testing starts here! */</span><br />
<span class="br0">&#125;</span> SPG_DirtyTable;</div>
<p>You will use the count to loop over the rects when redrawing your background.</p>
<p>Next, call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyInit<span class="br0">&#40;</span><span class="nu0">64</span><span class="br0">&#41;</span>;</div>
<p>This initializes the front and back tables to contain a maximum of 64 dirty rects.  When it reaches that limit, some rects will be combined as more are added.  Now you&#8217;re ready to enter your main drawing loop.  Whenever you draw something to the screen, you need to collect its dirty rect.  If you use SDL_BlitSurface(), then this is pretty easy:</p>
<div class="dean_ch" style="white-space: wrap;">SDL_Rect dirty;<br />
dirty.<span class="me1">x</span> = myX;<br />
dirty.<span class="me1">y</span> = myY;<br />
SDL_BlitSurface<span class="br0">&#40;</span>myPic, <span class="kw2">NULL</span>, screen, &amp;dirty<span class="br0">&#41;</span>;<br />
SPG_DirtyAdd<span class="br0">&#40;</span>&amp;dirty<span class="br0">&#41;</span>;</div>
<p>SDL_BlitSurface will write the actual dirty rect into &#8216;dirty&#8217;.  SPG_DirtyAdd() puts the given rect into the front table and does any necessary merging.  You must be sure that the rects that you add to the front table are properly clipped to the screen.  If not, the internal call to SDL_UpdateRects() will crash.  If you&#8217;re not sure about it, call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyClip<span class="br0">&#40;</span>screen, &amp;dirty<span class="br0">&#41;</span>;</div>
<p>This will make sure that your rect is safe to add.  Any calls to Sprig primitives and blits will automatically do all of this.</p>
<p>When you&#8217;re all done adding dirty rects (you&#8217;re done drawing to the screen), make this call:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyTable* table = SPG_DirtyUpdate<span class="br0">&#40;</span>screen<span class="br0">&#41;</span>;</div>
<p>Now you&#8217;ve updated the screen, just as if you had called SDL_Flip(screen), and you receive a pointer to the front table.  This has all of the newly updated rects in it for you to redraw the screen&#8217;s background:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">// This is for a full-screen background image</span><br />
<span class="kw4">int</span> i;<br />
SDL_Rect dest;<br />
<span class="kw1">for</span><span class="br0">&#40;</span>i = <span class="nu0">0</span>; i &lt; table-&gt;count; i++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; dest = table-&gt;rects<span class="br0">&#91;</span>i<span class="br0">&#93;</span>; &nbsp;<span class="co1">// SDL_BlitSurface is destructive to your rects</span><br />
&nbsp; &nbsp; SDL_BlitSurface<span class="br0">&#40;</span>myBackground, &amp;<span class="br0">&#40;</span>table-&gt;rects<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>, screen, &amp;dest<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>The screen is all set for more drawing.  Now we have to swap the tables:</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtySwap<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>This clears and switches the tables so that they are ready for the next frame.</p>
<p>The rest of the functions are for if you want to do fancy things with the dirty rects.</p>
<p>You can make your own table with SPG_DirtyMake.  You can add rects to it with SPG_DirtyAddTo and free it with SPG_DirtyFree when you&#8217;re done.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyTable* table = SPG_DirtyMake<span class="br0">&#40;</span><span class="nu0">128</span><span class="br0">&#41;</span>;<br />
SDL_Rect rect = <span class="br0">&#123;</span><span class="nu0">30</span>, <span class="nu0">40</span>, <span class="nu0">10</span>, <span class="nu0">10</span><span class="br0">&#125;</span>;<br />
SPG_DirtyAddTo<span class="br0">&#40;</span>&amp;rect, table<span class="br0">&#41;</span>;<br />
SPG_DirtyFree<span class="br0">&#40;</span>table<span class="br0">&#41;</span>;</div>
<p>SPG_DirtyEnabled tells you if dirty rects will be automatically generated.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_bool gettingDirty = SPG_DirtyEnabled<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
<p>SPG_DirtyGet returns a pointer to the front table (just be aware that the tables are switched each time SPG_DirtySwap is called).</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyTable* front = SPG_DirtyGet<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// Add stuff to it or whatever</span></div>
<p>SPG_DirtyClear will reset a given table, and you can adjust the constants in the dirty algorithm with SPG_DirtyLevel.  These constants determine the thresholds for merging rects.</p>
<div class="dean_ch" style="white-space: wrap;">SPG_DirtyClear<span class="br0">&#40;</span>table<span class="br0">&#41;</span>;<br />
SPG_DirtyLevel<span class="br0">&#40;</span><span class="nu0">200</span><span class="br0">&#41;</span>;</div>
<p><br/><br/><br />
There you go.  By now you should have a feeling of what Sprig can do for you and what kinds of awesome games and applications you can write using it.  If you have any questions, suggestions, or contributions, drop me an email.</p>
<p>Jonny D</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/a-guide-to-graphics-with-sprig/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MixBox Quick Guide</title>
		<link>http://www.sdltutorials.com/mixbox-quick-guide/</link>
		<comments>http://www.sdltutorials.com/mixbox-quick-guide/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 23:00:41 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Blue Dino Code]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=205</guid>
		<description><![CDATA[The following tutorial was created by Blue Dino Code, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework [...]]]></description>
			<content:encoded><![CDATA[<p>The following tutorial was created by <a href="http://code.bluedinosaurs.com/">Blue Dino Code</a>, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. SDLTutorials.com does not endorse or reject any of the coding practices outlined in the tutorial, and is not responsible for any code or files belonging to this tutorial that harms your computer.</p>
<hr/>
<p><b>Prerequisites:</b>  C++ classes and a small bit of SDL</p>
<p>MixBox is a set of C++ classes that makes it easy to add sound and music to your games.  The MixBox class is the main one.  It holds all the sound and music data and has functions that access just about everything you&#8217;d want.  The other classes, SoundID, MusicID, and ChannelID, are all subclasses of MixID.  Each of these classes has functions that roughly duplicate what the MixBox class can do for that type of object, but having them lets you choose how best to work MixBox into your game.</p>
<p><span id="more-205"></span></p>
<p>Typical usage of MixBox follows these steps:  Open the mixer, load a sound, close the mixer, then free the sound.<br />
Here&#8217;s a quick example of the simplest usage of MixBox.  You can grab the full example source and sample sounds here.  Link this program with SDLmain, SDL, and SDL_mixer:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &quot;SDL.h&quot;</span><br />
<span class="co2">#include &quot;MixBox.h&quot;</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">char</span>* argv<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">using</span> <span class="kw2">namespace</span> MB; &nbsp;<span class="co1">// MixBox&#8217;s namespace</span><br />
&nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Get the MixBox for future use</span><br />
&nbsp; &nbsp; MixBox&amp; mixer = MixBox::<span class="me2">instance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Open the mixer</span><br />
&nbsp; &nbsp; mixer.<span class="me1">openMixer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span class="co1">// Load a sound</span><br />
&nbsp; &nbsp; SoundID hit = mixer.<span class="me1">loadSound</span><span class="br0">&#40;</span><span class="st0">&quot;hit.wav&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>hit.<span class="me1">isBad</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;Error loading sound file.<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Play the sound</span><br />
&nbsp; &nbsp; hit.<span class="me1">play</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>mixer.<span class="me1">isPlaying</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SDL_Delay<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">// Close the mixer</span><br />
&nbsp; &nbsp; mixer.<span class="me1">closeMixer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Delete the sound</span><br />
&nbsp; &nbsp; mixer.<span class="me1">freeSounds</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; <span class="co1">// All done</span><br />
&nbsp; &nbsp; SDL_Quit<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Really straightforward, right?  Before you can do anything with this example, though, you need to have sound files to play.  There are some in the above link, but you&#8217;ll need more, of course.  If you need somewhere to start, I recommend DrPetter&#8217;s sfxr.  It&#8217;s a great tool for quickly making retro and placeholder sounds.</p>
<p>The examples just have the basics of MixBox.  Here&#8217;s a brief overview of some other functions MixBox has.  Since playing sounds and music is not too complex, I&#8217;ll just explain the odd ones.  There&#8217;s more than this in MixBox.  All the functions can easily be found in MixBox.h, where there are better descriptions of what they do.  Be sure to check it out!</p>
<p>MixBox setup:  Initialize and close MixBox.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">static</span> MixBox&amp; instance<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Returns the singleton MixBox.<br />
<span class="kw4">void</span> setAudioFormat<span class="br0">&#40;</span>Uint16 format<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> setAudioRate<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> rate<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> setNumOutputs<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> num<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Sets the number of output <span class="br0">&#40;</span>not mixing<span class="br0">&#41;</span> channels <span class="br0">&#40;</span>e.<span class="me1">g</span>. <span class="nu0">1</span> <span class="kw1">for</span> mono, <span class="nu0">2</span> <span class="kw1">for</span> stereo<span class="br0">&#41;</span><br />
<span class="kw4">void</span> setBufferSize<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> size<span class="br0">&#41;</span>;<br />
<span class="kw4">unsigned</span> <span class="kw4">int</span> setNumChannels<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> numChannels<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Sets the number of mixing channels<br />
<span class="kw4">bool</span> openMixer<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> closeMixer<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>MixBox loading:  Load and delete sounds and music.</p>
<div class="dean_ch" style="white-space: wrap;">
SoundID loadSound<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span>* filename<span class="br0">&#41;</span>;<br />
MusicID loadMusic<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span>* filename<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> freeSounds<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> <span class="kw3">free</span><span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> freeMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> <span class="kw3">free</span><span class="br0">&#40;</span><span class="kw4">const</span> MusicID&amp; id<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>MixBox sound controls:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> swapStereo<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
ChannelID play<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> stop<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
ChannelID playPan2D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="me1">ChannelID</span> playPan3D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> z, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y, z<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="kw4">bool</span> isPlaying<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> any channel is playing a sound. &nbsp;<span class="me1">There</span> are other functions that work with specific channels.<br />
&nbsp;</div>
<p>MixBox music controls:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">void</span> play<span class="br0">&#40;</span><span class="kw4">const</span> MusicID&amp; id, <span class="kw4">int</span> loops = <span class="nu0">-1</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> isMusicPlaying<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> stopMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> pauseMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> isMusicPaused<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> rewindMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> resumeMusic<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> skipMusic<span class="br0">&#40;</span><span class="kw4">double</span> position<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> fadeInMusic<span class="br0">&#40;</span><span class="kw4">const</span> MusicID&amp; id, <span class="kw4">unsigned</span> <span class="kw4">int</span> milliseconds, <span class="kw4">double</span> skipPosition = <span class="nu0">0</span>, <span class="kw4">int</span> loops = <span class="nu0">-1</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> fadeOutMusic<span class="br0">&#40;</span><span class="kw4">unsigned</span> <span class="kw4">int</span> milliseconds<span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>SoundID:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> isBad<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> the sound loaded incorrectly.<br />
<span class="kw4">void</span> setVolume<span class="br0">&#40;</span><span class="kw4">float</span> volume = <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;<br />
<span class="kw4">float</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
ChannelID play<span class="br0">&#40;</span><span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
ChannelID playPan2D<span class="br0">&#40;</span><span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="me1">ChannelID</span> playPan3D<span class="br0">&#40;</span><span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> z, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y, z<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
&nbsp;</div>
<p>MusicID:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> isBad<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> the music loaded incorrectly.<br />
<span class="kw4">void</span> setVolume<span class="br0">&#40;</span><span class="kw4">float</span> volume = <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;<br />
<span class="kw4">float</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> play<span class="br0">&#40;</span><span class="kw4">int</span> loops = <span class="nu0">-1</span><span class="br0">&#41;</span>;<br />
&nbsp;</div>
<p>ChannelID:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw4">bool</span> isBad<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
&nbsp; &nbsp; &#8211; Tells you <span class="kw1">if</span> the ID represents a viable channel.<br />
<span class="kw4">void</span> setVolume<span class="br0">&#40;</span><span class="kw4">float</span> volume = <span class="nu0">1</span>.0f<span class="br0">&#41;</span>;<br />
<span class="kw4">float</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">bool</span> isPlaying<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> play<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; sound, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> stop<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> pause<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> isPaused<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span>;<br />
<span class="kw4">void</span> resume<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> playPan2D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
<span class="kw4">void</span> playPan3D<span class="br0">&#40;</span><span class="kw4">const</span> SoundID&amp; id, <span class="kw4">float</span> x, <span class="kw4">float</span> y, <span class="kw4">float</span> z, <span class="kw4">float</span> maxRange, <span class="kw4">int</span> loops = <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &#8211; Plays a sound with stereo panning and volume that depend on how far <span class="br0">&#40;</span>x, y, z<span class="br0">&#41;</span> is from <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>.<br />
&nbsp;</div>
<p>And that&#8217;s how easy it is to add sound and music to your games!  Remember that there&#8217;s more in the header file, MixBox.h.  There&#8217;s also a demo program included with the MixBox source.  If you have any questions, suggestions, or contributions, drop me an email.</p>
<p>Files:<br />
<a href="/tutorials/bluedinocode/mixbox-quick-guide.zip">mixbox-quick-guide.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/mixbox-quick-guide/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Jonny D&#8217;s C++ Tutorial</title>
		<link>http://www.sdltutorials.com/jonny-ds-c-tutorial/</link>
		<comments>http://www.sdltutorials.com/jonny-ds-c-tutorial/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 22:54:38 +0000</pubDate>
		<dc:creator>Tim Jones</dc:creator>
				<category><![CDATA[Blue Dino Code]]></category>

		<guid isPermaLink="false">http://www.sdltutorials.com/?p=197</guid>
		<description><![CDATA[The following tutorial was created by Blue Dino Code, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework [...]]]></description>
			<content:encoded><![CDATA[<p>The following tutorial was created by <a href="http://code.bluedinosaurs.com/">Blue Dino Code</a>, for the purpose of helping people like you learn SDL. This tutorial, though not purposely a part of the SDLTutorials.com or created for the series, may be a branch or addition to the series. Please read notes by the author for any additional code and/or framework used by the author. SDLTutorials.com does not endorse or reject any of the coding practices outlined in the tutorial, and is not responsible for any code or files belonging to this tutorial that harms your computer.</p>
<hr/>
<div class="dean_ch" style="white-space: wrap;">
<span class="coMULTI">/*</p>
<p>//////////////////////// &nbsp;C++ From Start to Classes &nbsp;\\\\\\\\\\\\\\\\\\\\\\\\\\<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
| Hi, this is Jonny D and I&#8217;ll be whirling you through the basics and some &nbsp; &nbsp;|<br />
| intricacies of C++. &nbsp;This tutorial is for ABSOLUTE BEGINNERS that are &nbsp; &nbsp; &nbsp; |<br />
| computer literate. &nbsp;I&#8217;ll try my best to keep it plain, but if anything &nbsp; &nbsp; &nbsp;|<br />
| isn&#8217;t clear, feel free to drop me a line at GrimFang4@hotmail.com . &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| Stick with me and you&#8217;ll be programming in no time. &nbsp;I&#8217;m currently using &nbsp; &nbsp;|<br />
| the Code::Blocks IDE (Integrated Development Environment) to write and &nbsp; &nbsp; &nbsp;|<br />
| compile my code under Windows XP and I use the SDL (Simple DirectMedia &nbsp; &nbsp; &nbsp;|<br />
| Layer) graphics library for my games. &nbsp;I highly recommend SDL for when you &nbsp;|<br />
| feel ready to make video games. &nbsp;I might leave little bits of information &nbsp; |<br />
| about SDL along the way towards our learning goal. &nbsp;To keep it simple, I &nbsp; &nbsp;|<br />
| won&#8217;t be referencing any specific programming language other than C and C++ |<br />
| and I won&#8217;t be giving any history lessons (yay!). &nbsp;From here on out, it&#8217;s &nbsp; |<br />
| pure C++ learning time. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| Well, let&#8217;s get started. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
*/</span><br />
&nbsp;</div>
<p><span id="more-197"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<span class="coMULTI">/*<br />
First of all, you&#8217;ll need a compiler to convert your code into an executable.<br />
Until Code::Blocks releases a new and bug-free version of their IDE, I <br />
suggest Dev-C++ for beginning C++ programming. &nbsp;It can be found and downloaded<br />
for free at www.bloodshed.net</p>
<p>Strictly speaking, Dev-C++ is not a compiler, it is an IDE that contains a <br />
particular compiler (in this case, g++). &nbsp;Dev-C++ is mostly an editor that makes<br />
it easy for you to write your programs. &nbsp;Whatever you do, don&#8217;t use programs <br />
like Word to write your code. &nbsp;If you don&#8217;t have access to a C++ editor like <br />
Dev-C++, then use something simple like Notepad. &nbsp;Word will leave messy <br />
formatting stuff in your text which will prevent you from compiling your code.</p>
<p>Once you install Dev-C++, you can run it and start coding. &nbsp;You can open this <br />
file in there as well. &nbsp;You might need to make a new project and add these code <br />
files to it.<br />
*/</span></p>
<p><span class="coMULTI">/*<br />
So, what you&#8217;ve just seen is the beginning of a multi-line (or C-style)<br />
comment. &nbsp;This comment starts with a forward slash and an asterisk and is<br />
ended by the reverse, an asterisk and a forward slash. &nbsp;Whatever is <br />
within a comment is totally ignored by the compiler (the program that changes <br />
the code that you&#8217;ve written into a program that the computer can read). &nbsp;<br />
The easy way to make these comments is to use the numpad&#8217;s keys since they are <br />
right next to each other and don&#8217;t require a shift modifier.<br />
*/</span></p>
<p><span class="co1">// This is a single-line (or C++-style or new-style) comment. &nbsp;It starts with </span><br />
<span class="co1">// two forward slashes and ends at the end of a line. &nbsp;It&#8217;s really quick to </span><br />
<span class="co1">// use for leaving notes in your code, but can be annoying for large blocks of</span><br />
<span class="co1">// writing like I&#8217;ll be using for some of my explanations.</span></p>
<p>
<span class="coMULTI">/*<br />
C++ uses preprocessor directives. &nbsp;These are just special keywords that are <br />
checked before the actual code gets compiled. &nbsp;The most common is the #include<br />
(&quot;pound include&quot;) preprocessor directive. &nbsp;It is used to link other source code <br />
files (like this one) to the current source file so that they get compiled <br />
together. &nbsp;This means you can use the code in the other files just as if it <br />
were in your file. &nbsp;It is usually best to write your preprocessor directives<br />
at the beginning of your source code file.<br />
*/</span></p>
<p><span class="co2">#include &lt;cstdlib&gt; &nbsp;//This line includes a standard header file named &quot;cstdlib&quot;,</span><br />
<span class="co1">// the C Standard Library. &nbsp;This contains many features that help with most </span><br />
<span class="co1">// simple tasks. &nbsp;This and the next #include are not really necessary in this </span><br />
<span class="co1">// tutorial, but can be useful in some projects.</span></p>
<p><span class="co2">#include &lt;cstdio&gt; &nbsp;//&quot;cstdio&quot;, the C Standard Input and Output file, gives us </span><br />
<span class="co1">// access to C&#8217;s old way of controlling input and output. &nbsp;Hey, it&#8217;s old but </span><br />
<span class="co1">// can still come in handy!</span></p>
<p><span class="co2">#include &lt;iostream&gt; &nbsp;//This is C++&#8217;s standard input/output file. &nbsp;It makes </span><br />
<span class="co1">// many things easier and uses a different syntax (order of words/symbols)</span><br />
<span class="co1">// than the C way of things.</span></p>
<p><span class="co2">#include &lt;string&gt; //Here&#8217;s C++&#8217;s string class that replaces many uses of C&#8217;s </span><br />
<span class="co1">// outdated character arrays (which we&#8217;ll see later). &nbsp;Strings are just a </span><br />
<span class="co1">// bunch of sequential characters (letters/numbers/symbols). &nbsp;Strings are </span><br />
<span class="co1">// usually seen with double quotes around them, like &quot;This is a string.&quot; or </span><br />
<span class="co1">// &quot;1357&quot;.</span></p>
<p><span class="co2">#include &quot;ClassName.h&quot; &nbsp;//This is a user-defined header file that I made. &nbsp;</span><br />
<span class="co1">// Notice that it uses a string that contains the name of a file in the same </span><br />
<span class="co1">// directory as this source file. &nbsp;If this were in a separate folder, I could </span><br />
<span class="co1">// use relative path names like:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// &quot;folder/ClassName.h&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; &nbsp; &nbsp; &nbsp;or</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// &quot;../ClassName.h&quot;</span><br />
<span class="co1">// That last one looks for ClassName in the parent folder of the one </span><br />
<span class="co1">// that JDTutorial.cpp is in.</span></p>
<p><span class="co2">#define ONE 1 &nbsp;//&quot;Pound define&quot; is another preprocessor directive. &nbsp;If the first </span><br />
<span class="co1">// term after it, ONE, is found in your code, then it will be converted to the </span><br />
<span class="co1">// second term, 1, before any code is actually compiled.</span></p>
<p><span class="co2">#define MOOCOW &quot;milk&quot; &nbsp;// &quot;milk&quot; and 1 are called &quot;literals&quot; because they don&#8217;t </span><br />
<span class="co1">// evaluate (break down or change) to anything simpler than themselves. &nbsp;They </span><br />
<span class="co1">// are hard-written into the code.</span></p>
<p><span class="co2">#if 0 &nbsp;//The &quot;pound if&quot; is a preprocessor logic control. &nbsp;You should know that </span><br />
<span class="co1">// 0 means false and 1 means true. &nbsp;Remember that. &nbsp;So since 0 means false, </span><br />
<span class="co1">// the next part will not be compiled, thankfully. &nbsp;If I wanted it to compile, </span><br />
<span class="co1">// I could have written either 1 or ONE, since ONE is #defined as 1.</span><br />
<span class="co2">#include &quot;thisFileDoesNotExist.h&quot;</span><br />
<span class="co2">#else &nbsp;//Pound else handles the case that the term after the #if is false.</span><br />
<span class="co2">#define SAMURAI &quot;choppity-chop!&quot;</span><br />
<span class="co2">#endif &nbsp;//Every good if statement must come to an end&#8230;</span></p>
<p><span class="kw2">using</span> <span class="kw2">namespace</span> std; &nbsp;<span class="co1">//This statement is the use of the standard namespace. &nbsp;</span><br />
<span class="co1">// Namespaces are just protection from colliding definitions. &nbsp;C++ won&#8217;t like </span><br />
<span class="co1">// it if you use the same name for two functions (a function is a</span><br />
<span class="co1">// reusable organizational unit of code, similar to math functions), so </span><br />
<span class="co1">// namespaces will free you from possible collisions, say, with #included code.</span><br />
<span class="co1">// The standard namespace is what many important files (like &lt;iostream&gt;) use </span><br />
<span class="co1">// to store their commands in. &nbsp;You&#8217;ll need to be using this namespace in order </span><br />
<span class="co1">// to use these commands. &nbsp;Typically, namespaces other than std are only used </span><br />
<span class="co1">// in gigantic projects like operating systems and such.</span></p>
<p><span class="co1">// Unlike English, where statements are ended with a period, C++ is a language </span><br />
<span class="co1">// where statements are ended with semicolons. &nbsp;All of your statements must end </span><br />
<span class="co1">// with a semicolon because &quot;whitespace&quot; doesn&#8217;t matter to C++. &nbsp;Tabs, spaces, </span><br />
<span class="co1">// and even carriage returns (end of line, beginning of new line) are ignored </span><br />
<span class="co1">// by the compiler (except for when they separate words) and serve only to make </span><br />
<span class="co1">// your code look neat and tidy. &nbsp;It is important to have neat code so that </span><br />
<span class="co1">// you or the next person in line (like a coworker or college professor) can </span><br />
<span class="co1">// read it and tell what it does. &nbsp;Of course you probably won&#8217;t ever have to </span><br />
<span class="co1">// write as many comments as I have in this file.</span></p>
<p><span class="kw4">int</span> counter; <span class="coMULTI">/* This is a variable (just like math) declaration. &nbsp;In this case, <br />
I specified that it is an integer (positive or negative whole number). &nbsp;It&#8217;s<br />
like saying &quot;Let there exist an integer named &#8216;counter&#8217;&quot;. &nbsp;There are a few <br />
options for variable types, such as, but not restricted to:<br />
&nbsp; &nbsp; integer =&gt; int<br />
&nbsp; &nbsp; long integer (for BIG numbers) =&gt; long<br />
&nbsp; &nbsp; character =&gt; char<br />
&nbsp; &nbsp; floating point (number with a decimal) =&gt; float<br />
&nbsp; &nbsp; double-precision floating point (more significant figures) =&gt; double<br />
&nbsp; &nbsp; boolean (either 1, true, or 0, false) =&gt; bool<br />
Every variable must be declared with a type. &nbsp;That&#8217;s why C++ is called a <br />
&quot;strongly typed&quot; language. &nbsp;Also, variable and function names must start with a <br />
letter but can have letters, numbers, or _underscores_ afterwards. &nbsp;C++ is <br />
case-sensitive, so capitalization matters every time. &nbsp;It is a useful <br />
convention to capitalize the beginning of each new word in a name, as below. */</span></p>
<p><span class="kw4">char</span> aLetter = <span class="st0">&#8216;j&#8217;</span>; &nbsp;<span class="coMULTI">/* Variables can be given values when they are declared.<br />
The assignment operator for C++ is the equals sign. &nbsp;The assignment operator <br />
always takes two things, the left part and the right part, and assigns the <br />
value of the right part to the variable on the left (right to left evaluation). <br />
Character values are denoted by having single-quotes around a single <br />
letter, number, or symbol. &nbsp;Characters are different than strings. */</span></p>
<p><span class="coMULTI">/* counter and aLetter are both &quot;global&quot; variables because they were declared <br />
outside of any functions. &nbsp;This means that any part of the program can access <br />
them and change them. &nbsp;Global variables aren&#8217;t always a good idea, though, <br />
because they are hard to recognize in code that refers to them all <br />
of a sudden. &nbsp;Variables declared inside a function are called &quot;local&quot; <br />
variables. &nbsp;These will always be deleted when the function finishes unless<br />
you do something fancy (We&#8217;ll see some of this later).<br />
*/</span></p>
<p><span class="co1">// &nbsp;Here we will declare the functions we will be using in our program.</span><br />
<span class="co1">// These are just the declarations. &nbsp;They tell the compiler that the function </span><br />
<span class="co1">// exists. &nbsp;Later, we must define each function so that we can actually use it.</span></p>
<p><span class="coMULTI">/* I want just1() to always give me back the number 1. &nbsp;Pretty useless, yes, <br />
but it illustrates a few things about functions. &nbsp;The name of a function, <br />
like just1(), should always say something about what the function does. &nbsp;<br />
A function named q1342g() won&#8217;t help me at all. &nbsp;It&#8217;s also good to write a <br />
quick comment next to a function that tells what it does. */</span><br />
<span class="kw4">int</span> just1<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">//Function code goes here</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span>;<br />
<span class="br0">&#125;</span></p>
<p>
<span class="coMULTI">/* Functions are called (used) in this way:<br />
&nbsp; &nbsp; &nbsp;just1();<br />
&nbsp; <br />
&nbsp; But this doesn&#8217;t do much for us, really. &nbsp;The &quot;int&quot; specifies the return <br />
type. &nbsp;This means that when I call just1(), then an integer will pop right <br />
out when the function ends. &nbsp;That integer would just be discarded if we don&#8217;t <br />
assign it to a variable. &nbsp;The integer can be caught with the assignment <br />
operator like so: <br />
&nbsp; &nbsp; &nbsp; &nbsp;int result = just1();<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; After the return type comes the name followed by parentheses. &nbsp;Inside the <br />
parentheses is a list of whatever you want the function input to be <br />
(called function arguments). &nbsp;There are no arguments for just1() though.<br />
*/</span></p>
<p>
<span class="coMULTI">/* It is quite important (and good coding style) to write <br />
&quot;prototype declarations&quot; of functions. &nbsp;If a declaration isn&#8217;t found before <br />
you try to use a function, C++ won&#8217;t know what you&#8217;re talking about. &nbsp;It&#8217;s <br />
like referring to a joke that you haven&#8217;t told yet.<br />
*/</span></p>
<p>
<span class="co1">// This is a prototype declaration for add1()</span><br />
<span class="kw4">int</span> add1<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span>; <span class="coMULTI">/* I want this function to take a number I give to it, add <br />
one to it, and return the result. &nbsp;num is an integer that I will provide <br />
whenever I use add1(). &nbsp;When I do call add1() and give it an integer, such <br />
as&#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add1(4);<br />
&nbsp; &#8230;then the value 4 is bound to num which is then used within add1(). &nbsp;I could <br />
even write add1(ONE);, using the preprocessor to convert ONE to 1. */</span></p>
<p><span class="kw4">void</span> countDown<span class="br0">&#40;</span><span class="kw4">int</span> count<span class="br0">&#41;</span>; <span class="coMULTI">/* Here&#8217;s a very fancy function. &nbsp;I want it to <br />
print to the screen a list of numbers starting with one I give it and descending <br />
(decrementing) until it reaches zero. &nbsp;The return type, void, means that the <br />
function will not return a value at all. &nbsp;That means that the function must <br />
perform a useful side-effect like changing a variable or in this case, printing<br />
to the screen.<br />
*/</span></p>
<p><span class="coMULTI">/* &nbsp;You can ignore the following function declarations for now. &nbsp;They&#8217;ll be <br />
used later to explain the topics in this tutorial. &nbsp;You can use this list as <br />
an index if you wish.<br />
*/</span></p>
<p><span class="kw4">void</span> choice<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw4">void</span> operators<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// +, -, *, /, =, etc.</span><br />
<span class="kw4">void</span> stdPut<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// cout and cin</span><br />
<span class="kw4">void</span> logic<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// if (conditionals), &amp;&amp;, ||, ==, !, !=</span><br />
<span class="kw4">void</span> controls<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// loops with while, do while, and for</span><br />
<span class="kw4">void</span> arrays<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// arrays[] as lists of variables</span><br />
<span class="kw4">void</span> stringClass<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Explaining the string class</span><br />
<span class="kw4">void</span> randomStuff<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// rand() and random number generation</span><br />
<span class="kw4">void</span> structKeyword<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Structures and struct, the precursor to classes</span><br />
<span class="kw4">void</span> recursion<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Looping without loops, function self-calls</span><br />
<span class="kw4">void</span> pointers<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// -&gt; memory addresses</span><br />
<span class="kw4">void</span> references<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// An easier way to pass by reference</span><br />
<span class="kw4">void</span> functionOverloading<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// functions with nearly the same name</span><br />
<span class="kw4">void</span> scope<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// local vs. global, precedence rules</span><br />
<span class="kw4">void</span> dynamicMemory<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// new and delete memory management</span><br />
<span class="kw4">void</span> classes<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// what makes C++ better than C, classes and inheritance</span></p>
<p><span class="kw2">class</span> Junk<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw4">int</span> j;<br />
<span class="br0">&#125;</span>;</p>
<p><span class="kw4">int</span> fact<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> square<span class="br0">&#40;</span><span class="kw4">int</span> a<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> squarePtr<span class="br0">&#40;</span><span class="kw4">int</span>* x<span class="br0">&#41;</span>;<br />
<span class="kw4">void</span> squareRef<span class="br0">&#40;</span>int&amp; x<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b, <span class="kw4">int</span> c<span class="br0">&#41;</span>;<br />
<span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">char</span> a, <span class="kw4">char</span> b<span class="br0">&#41;</span>;</p>
<p><span class="coMULTI">/*<br />
&nbsp;Here&#8217;s where the fun begins. &nbsp;The main() function is the entry point of <br />
the program whereas everything up to this is just being held onto for use <br />
in this function. &nbsp;It&#8217;s a good idea to type it <br />
as &quot;int main(int argc, char *argv[])&quot; because then it will return an integer <br />
value to the operating system telling whether or not there was an error and <br />
the arguments afterward give you the ability to catch command-line arguments <br />
(like when you use &quot;Run&#8230; c:\test.exe fps toilet&quot; where fps and toilet are <br />
command-line arguments). &nbsp;You can sometimes put just void main(), but SDL, the <br />
graphics library, requires all this stuff. &nbsp;Like all function definitions, <br />
main() must have wiggly brackets {}, aka braces, after it. &nbsp;These braces <br />
contain the statements to be executed when the function is run.<br />
*/</span></p>
<p><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">char</span> *argv<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/***********<br />
&nbsp; &nbsp; Let&#8217;s go over exactly what code you need for your program to compile.<br />
&nbsp; &nbsp; First, you need to #include the files that have code that you&#8217;ll be using.<br />
&nbsp; &nbsp; Next, you will usually need the &#8216;using namespace std;&#8217; line. &nbsp;Then, you <br />
&nbsp; &nbsp; need a main() function with a code block ( code surrounded by {} curly <br />
&nbsp; &nbsp; braces ). &nbsp;Lastly, make sure your code is correct with semicolons ; after <br />
&nbsp; &nbsp; every necessary line and you return from main() cleanly.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;************/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// Check out GoodExample.cpp if you want to see an example of a </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// pretty basic program.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// This JDTutorial.cpp is a full program as well, but is very cluttered</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// with tutorial comments.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; My main() is pretty lame. &nbsp;A main() could be the whole program, <br />
&nbsp; &nbsp; but mine just organizes the rest of the program.<br />
&nbsp; &nbsp; */</span></p>
<p>&nbsp; &nbsp; <span class="kw3">srand</span><span class="br0">&#40;</span><span class="kw3">time</span><span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// This is explained in randomStuff()</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; choice<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">system</span><span class="br0">&#40;</span><span class="st0">&quot;PAUSE&quot;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// This is an inefficient but very simple way to pause </span><br />
&nbsp; &nbsp; <span class="co1">// the program. &nbsp;It&#8217;s a call to the operating system and is a little </span><br />
&nbsp; &nbsp; <span class="co1">// wasteful. &nbsp;It&#8217;s often used in main() to keep the program window open, but </span><br />
&nbsp; &nbsp; <span class="co1">// some compilers do not need this command to do that.</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>; &nbsp;<span class="co1">// No problems running, so return 0 to the operating system.</span><br />
&nbsp; &nbsp; <span class="co1">// This is the end of the program. &nbsp;Once main() returns, it&#8217;s all over.</span><br />
&nbsp; &nbsp; <span class="co1">// The return keyword is how you stop a function from continuing and how </span><br />
&nbsp; &nbsp; <span class="co1">// you can make it give you a value (unless it&#8217;s void). &nbsp;return also </span><br />
&nbsp; &nbsp; <span class="co1">// accepts variables. &nbsp;For void functions, just type &#8216;return;&#8217; to end it.</span><br />
&nbsp; &nbsp; <span class="co1">// Continue on below for the rest of the tutorial.</span><br />
&nbsp; &nbsp; <br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END int main(int argc, char *argv[]) &nbsp;&lt;- Though this may be </span><br />
&nbsp; &nbsp;<span class="co1">// unnecessary since main() is so short, it is a good idea to label the end </span><br />
&nbsp; &nbsp;<span class="co1">// of large blocks of code (like functions).</span></p>
<p>
<span class="kw4">void</span> operators<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Operators in C++ are those symbols that change or eventually give values,<br />
&nbsp; &nbsp; &nbsp;again, just like in math class.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> answer;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> a = <span class="nu0">6</span>; &nbsp;<span class="co1">// The assignment operator, as you&#8217;ve seen.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> b = <span class="nu0">4</span>; &nbsp;<span class="co1">// Never try to use an uninitialized variable. &nbsp;You&#8217;ll get </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// lots of errors.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!) &nbsp;&lt;- Look for this symbol later in the tutorial!</span><br />
&nbsp; &nbsp; &nbsp;answer = a + b; &nbsp; <span class="co1">// This will evaluate and set the value of answer to 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a &#8211; b; &nbsp; <span class="co1">// Answer equals 2</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a * b; &nbsp; <span class="co1">// Answer equals 24</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a / b; &nbsp; <span class="co1">// Equals&#8230; 1? &nbsp;Yep, this is integer division!</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;answer = a % b; &nbsp; <span class="co1">// This is &quot;modulo&quot;, which gives the remainder of </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//integer division.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// a % b = 2</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Modulo is a powerful operator. &nbsp;It can do lots of fun stuff.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You&#8217;ll see more in randomStuff(), but keep in mind that because </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// modulo is based on division, the result is ALWAYS less than the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// right side (in this case, &quot;b&quot;).</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Just below this comment, see if you can make answer equal 12 by </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// modding two integers. &nbsp;Then write one that equals zero.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// C++ has some handy shorthand operators as well.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// From up top a = 6, b = 4.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a = a + b; <span class="co1">// This is the same as&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a += b; &nbsp;<span class="co1">// This.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Notice now that a = (a + b) + b = 14</span><br />
&nbsp; &nbsp; &nbsp;a -= b; <span class="co1">// Now a = 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a *= b; <span class="co1">// a = 40</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a /= b; <span class="co1">// a = 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The increment and decrement operators are useful too.</span><br />
&nbsp; &nbsp; &nbsp;a++; <span class="co1">// a = 11</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;a&#8211;; <span class="co1">// a = 10</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// These are in the postfix position, but you can also write them in the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// prefix position like so:</span><br />
&nbsp; &nbsp; &nbsp;++a;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;&#8211;a;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The difference is in when they evaluate.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// a++, the postfix notation of the increment operator, will add one to </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the variable &#8216;a&#8217; after the whole line has been performed by the program.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// ++a, the prefix notation of the increment operator, will add one to </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the variable &#8216;a&#8217; immediately and thus before anything else on the line </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// tries to work with it.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Precedence</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Every operator has a preset level of precedence. &nbsp;This tells which operator<br />
&nbsp; &nbsp; &nbsp;&quot;precedes&quot; another particular operator.<br />
&nbsp; &nbsp; &nbsp;For instance, multiplication has a higher precedence than addition.<br />
&nbsp; &nbsp; &nbsp;Thus, a + b * c<br />
&nbsp; &nbsp; &nbsp; &nbsp;evaluates like a + (b * c).<br />
&nbsp; &nbsp; &nbsp; &nbsp;&#8217;b&#8217; and &#8216;c&#8217; get multiplied before &#8216;a&#8217; is added.<br />
&nbsp; &nbsp; &nbsp;Just as I did here, you can make the precedence of your statements clearer<br />
&nbsp; &nbsp; &nbsp;or cause the precedence to work the way you want it to by using parentheses.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;If you look at (a + b) * c,<br />
&nbsp; &nbsp; &nbsp; &nbsp;this evaluates (a + b) first, then multiplies that result by &#8216;c&#8217;.<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;The precedence of operators is not usually a big deal, but many new <br />
&nbsp; &nbsp; &nbsp;programmers will forget that multiplication and division have higher <br />
&nbsp; &nbsp; &nbsp;precedence than addition and subtraction.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;If you need to know any other precedences, you can just search online for <br />
&nbsp; &nbsp; &nbsp;a C++ precedence table. &nbsp;Otherwise, you can always use parentheses.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here&#8217;s a little test that you can skip if you want</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp;(or just run the program with the task done from the next section&#8230;)</span><br />
&nbsp; &nbsp; &nbsp;a = <span class="nu0">6</span>;<br />
&nbsp; &nbsp; &nbsp;b = <span class="nu0">4</span>;<br />
&nbsp; &nbsp; &nbsp;answer = <span class="nu0">3</span> * a++ + add1<span class="br0">&#40;</span><span class="nu0">6</span><span class="br0">&#41;</span> &#8211; ++b / <span class="nu0">5</span> + &#8211;b + b++; &nbsp;<span class="co1">// Ooh, nice and complicated.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// It helps to clarify with parentheses sometimes.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// answer = (3 * a++) + add1(6) &#8211; (++b / 5) + &#8211;b + b++;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Now what is the answer? &nbsp;a++ increments &#8216;a&#8217; _after_ the statement is </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// evaluated. &nbsp;++b increments &#8216;b&#8217; _before_ anything else happens.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// What will be the values of answer, a, and b?</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// (!)</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void operators()</span></p>
<p>
<span class="kw4">int</span> add1<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><span class="kw1">return</span> num+=<span class="nu0">1</span>;<span class="br0">&#125;</span></p>
<p>
<span class="kw4">void</span> stdPut<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// Stuff in this section requires #include &lt;iostream&gt;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Do you remember way up there when we #included &lt;iostream&gt;? &nbsp;That file <br />
&nbsp; &nbsp; includes two nice objects that we&#8217;ll use a lot, cin and cout, console <br />
&nbsp; &nbsp; input and output objects. &nbsp;We&#8217;ll be getting to the definition of objects <br />
&nbsp; &nbsp; when we start talking about classes later. &nbsp;For now, let&#8217;s truck along. */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Hello World!&quot;</span> &lt;&lt; endl; &nbsp;<span class="coMULTI">/* The console that cout refers to is that <br />
&nbsp; &nbsp; ugly MSDOS prompt window that Windows still uses. &nbsp;The console will boot <br />
&nbsp; &nbsp; up with the start of the program and then display &quot;Hello World!&quot;. &nbsp;cout <br />
&nbsp; &nbsp; uses the &lt;&lt; left redirect operator. &nbsp;You can think of it as arrows pointing <br />
&nbsp; &nbsp; in the direction the data will move, like it takes all the words and sends <br />
&nbsp; &nbsp; them to the screen. &nbsp;cout accepts strings like &quot;Hello World!&quot; and standard <br />
&nbsp; &nbsp; variables. &nbsp;The keyword &quot;endl&quot; means end line. &nbsp;It&#8217;s like the computer <br />
&nbsp; &nbsp; presses the return key to start a new line. &nbsp; &nbsp;<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//Here we&#8217;ll output some other stuff. &nbsp;(aLetter is still a global variable)</span><br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; aLetter &lt;&lt; <span class="st0">&quot;onny d&quot;</span> &lt;&lt; <span class="nu0">3</span> + <span class="nu0">2</span> &lt;&lt; endl; &nbsp;<span class="co1">//jonny d5</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// For some practice (and to make the program more useful), go back up to </span><br />
&nbsp; &nbsp; <span class="co1">// operators() and replace the commented (!) symbol with some uncommented </span><br />
&nbsp; &nbsp; <span class="co1">// code that will display some useful variables to know at each point.</span><br />
&nbsp; &nbsp; <span class="co1">// Your best friends will be named &quot;cut&quot; and &quot;paste&quot;.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; As I said above, &lt;iostream&gt; also includes &quot;cin&quot;, the console input object.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> number;<br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;Think of a number&#8230; (and enter it)&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;-&gt;&quot;</span>; &nbsp;<span class="co1">// Always prompt for an input. &nbsp;The user won&#8217;t know what </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// to do otherwise.</span><br />
&nbsp; &nbsp; <span class="kw3">cin</span> &gt;&gt; number; &nbsp;<span class="co1">// The data goes from the input to the variable.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;With my mystical abilities, I have divined that&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot; you chose the number &quot;</span> &lt;&lt; number &lt;&lt; <span class="st0">&quot;!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// I personally never use the old C way of collecting input, but I still </span><br />
&nbsp; &nbsp; <span class="co1">// frequently use C&#8217;s output command.</span><br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;This is how C programmers had to do output.<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;They also knew that %d is your number&#8230;<span class="es0">\n</span>&quot;</span>, number<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; aLetter = <span class="st0">&#8216;a&#8217;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;This is about %d times as %cnnoying!<span class="es0">\n</span>&quot;</span>, number, aLetter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; printf() uses escape characters to represent where info should be inserted. <br />
&nbsp; &nbsp; As you can see above, \n (backslash n) gives a carriage return, %d is the <br />
&nbsp; &nbsp; escape for integers, %f does floats and doubles, and %c escapes for <br />
&nbsp; &nbsp; characters. &nbsp;A bunch of other escapes are out there.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw4">float</span> pi = <span class="nu0">3.14</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;PI = %.2f<span class="es0">\n</span>&quot;</span>, pi<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;PI = %.4f<span class="es0">\n</span>&quot;</span>, pi<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;PI = %f<span class="es0">\n</span>&quot;</span>, pi<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;End this line<span class="es0">\n</span>&quot;</span>; &nbsp;<span class="co1">// You can still use \n with cout if you prefer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// it instead of &#8216;endl&#8217;.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// I mostly use printf() with SDL. &nbsp;SDL has it set that printf() and </span><br />
&nbsp; &nbsp; <span class="co1">// cout send text straight to an output text file to be read later. &nbsp;This </span><br />
&nbsp; &nbsp; <span class="co1">// is pretty useful for debugging (fixing) my programs.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; For certain special characters, you&#8217;ll have to use a backslash to show C++<br />
&nbsp; &nbsp; that you want the actual character. &nbsp;Plus, since backslash does this special <br />
&nbsp; &nbsp; trick, you have to use a backslash to escape a backslash.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Junk coming&#8230; <span class="es0">\&quot;</span>c:<span class="es0">\\</span>cow<span class="es0">\\</span><span class="es0">\&quot;</span>&quot;</span> &lt;&lt; endl; <br />
&nbsp; &nbsp; <span class="co1">// That&#8217;s about all you need to know about input/output until we </span><br />
&nbsp; &nbsp; <span class="co1">// get to classes. &nbsp;It wasn&#8217;t so bad, was it?</span><br />
&nbsp; &nbsp; <br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void stdPut()</span></p>
<p>
<span class="kw4">void</span> logic<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/*<br />
&nbsp; &nbsp; Logic is the key to having a program that does what you want it to do.<br />
&nbsp; &nbsp; C++ has all you need to build up your ideas into code. &nbsp;Booleans are <br />
&nbsp; &nbsp; commonly used in many languages. &nbsp;A boolean has only two possible <br />
&nbsp; &nbsp; values: 0 or 1.<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw4">bool</span> test = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// &#8216;if&#8217; is C++&#8217;s basic conditional. &nbsp;You use conditionals every time you say </span><br />
&nbsp; &nbsp; <span class="co1">// &quot;If pigs could fly, then I&#8217;d be a pig, or else I&#8217;d fly instead.&quot;.</span><br />
&nbsp; &nbsp; <span class="co1">// &quot;if&quot; must always be followed by a parenthesized statement that evaluates </span><br />
&nbsp; &nbsp; <span class="co1">// to a boolean. &nbsp;&quot;true&quot; is a C++ keyword that evaluates to 1. &nbsp;&quot;false&quot;, </span><br />
&nbsp; &nbsp; <span class="co1">// of course, evaluates to 0. &nbsp;Any integer can be used for the boolean </span><br />
&nbsp; &nbsp; <span class="co1">// test statement because C++ thinks of 0 as meaning false and any other </span><br />
&nbsp; &nbsp; <span class="co1">// number meaning true. &nbsp;Since &quot;true&quot; (or 1) is the test parameter, the </span><br />
&nbsp; &nbsp; <span class="co1">// &quot;then&quot; part will always execute. &nbsp;In C++, the &quot;then&quot; part can be either </span><br />
&nbsp; &nbsp; <span class="co1">// a _line_ of code or a _block_ of code. &nbsp;This one uses a line.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;First <span class="es0">\&quot;</span>if<span class="es0">\&quot;</span> statement&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here&#8217;s a block. &nbsp;Blocks are multiple code lines enclosed in {}</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>test<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Second <span class="es0">\&quot;</span>if<span class="es0">\&quot;</span> statement&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Test = &quot;</span> &lt;&lt; test &lt;&lt; <span class="st0">&quot; now.&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The &quot;else&quot; keyword executes it&#8217;s code when a directly preceding if&#8217;s </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// test is false.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;That was false?&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>
&nbsp; &nbsp; &nbsp;<span class="co1">// The operators == &amp;&amp; || != &gt; &lt; &gt;= and &lt;= compare two arguments and </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// return a boolean value. The operator ! negates a boolean value (changes </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// it from true to false or from false to true).</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">4</span> == <span class="nu0">3</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;equals&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">1</span> &amp;&amp; <span class="nu0">0</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;and&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">0</span> || <span class="nu0">1</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;or&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="st0">&quot;table&quot;</span> != <span class="st0">&quot;table&quot;</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;not equal&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">22</span> &gt; <span class="nu0">344</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;greater than&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">1</span> &lt; <span class="br0">&#40;</span><span class="nu0">1</span><span class="nu0">+1</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;less than&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">12</span> &gt;= <span class="nu0">12</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;greater than or equals&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="nu0">72</span> &lt;= <span class="nu0">56</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;less than or equals&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>!test<span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;not&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// For fun, try making all of the above statements evaluate to true and </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// print their messages without changing the operators.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> difficulty;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Choose a difficulty level: &quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cin</span> &gt;&gt; difficulty;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>difficulty &lt; <span class="nu0">0</span><span class="br0">&#41;</span> || <span class="br0">&#40;</span>difficulty &gt; <span class="nu0">5</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;That was not a choice for difficulty.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; endl &lt;&lt; <span class="st0">&quot;No switch for you!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/* The &quot;switch&quot; statement is a way to check a single variable for lots <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;of values. &nbsp;After each &quot;case&quot; is the value it checks for. &nbsp;Every case <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set needs a &quot;break&quot; statement. &nbsp;&quot;break&quot; is a way to end a control <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;structure, like this &quot;switch&quot; statement. &nbsp;Once &quot;break&quot; gets executed, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;none of the other cases are checked. &nbsp;The default statement is <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;used when none of the cases match. */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">switch</span><span class="br0">&#40;</span>difficulty<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Diff = 1&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">2</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Diff = 2&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">3</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Diff = 3&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">default</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;That&#8217;s too difficult!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">//END void logic()</span></p>
<p>
<span class="kw4">void</span> controls<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Though &quot;switch&quot; is a control statement, it is hardly as popular as the <br />
&nbsp; &nbsp; &nbsp;loop statements: while, for, and do while. */</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> countDown = <span class="nu0">10</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &quot;while&quot; is the most common loop. &nbsp;It uses a boolean test, just like </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the &quot;if&quot; statement, but it executes it&#8217;s block of code and then performs </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the test again. &nbsp;If it&#8217;s still true, it keeps looping. &nbsp;Try to be </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// careful with loops. &nbsp;A little typo can cause an infinite loop which can </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// crash the program (alerting the operating system) or cause you to ctrl-</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// alt-delete the whole thing. &nbsp;Typically, you don&#8217;t want to use something </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// that always evaluates to &quot;while (1)&quot;. &nbsp;Even so, you can use the &quot;break&quot; </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// command to escape such a loop.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>countDown&#8211;<span class="br0">&#41;</span> &nbsp;<span class="co1">// Run the program and check this one out.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; countDown &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// It performs the test before starting the loop, so the full countdown </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// isn&#8217;t shown.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">system</span><span class="br0">&#40;</span><span class="st0">&quot;PAUSE&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;countDown = <span class="nu0">10</span>; &nbsp;<span class="co1">// Reset the count.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &quot;do while&quot; executes the loop at least once before testing it.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; countDown &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>countDown&#8211;<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Since &quot;while&quot; doesn&#8217;t have a block after it, &nbsp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// you&#8217;ll need a semicolon to end the statement.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> letter = <span class="st0">&#8216;a&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> n = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>n &lt; <span class="nu0">26</span><span class="br0">&#41;</span> &nbsp;<span class="co1">// A counting loop.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; letter;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;letter++; &nbsp;<span class="co1">// Did you know you can do arithmetic on letters?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;n++;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;letter = <span class="st0">&#8216;A&#8217;</span>; &nbsp;<span class="co1">// Reset letter.</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Most programming languages include a convenience loop statement named <br />
&nbsp; &nbsp; &nbsp;&quot;for&quot;. &nbsp;&quot;for&quot; has three distinct code parts instead of a test. &nbsp;The first <br />
&nbsp; &nbsp; &nbsp;part is the initialization, where you set a variable. &nbsp;The second part <br />
&nbsp; &nbsp; &nbsp;is the test statement. &nbsp;The last part is the iterator, where it changes a <br />
&nbsp; &nbsp; &nbsp;variable. &nbsp;Semicolons are always used to separate these parts. &nbsp;So the <br />
&nbsp; &nbsp; &nbsp;initialization is executed only once, right at the start while both the <br />
&nbsp; &nbsp; &nbsp;test and the iterator are run after each loop.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;init<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; test<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; iterator<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; <span class="nu0">26</span>; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; letter;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;letter++;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void controls()</span></p>
<p>
<span class="kw4">void</span> arrays<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;C++ has a good way to store lots of information. &nbsp;It also does this very <br />
&nbsp; &nbsp; &nbsp;efficiently.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You can declare multiple variables like this:</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> num1, num2, num3, num4, num5;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// And you can initialize them together:</span><br />
&nbsp; &nbsp; &nbsp;num1 = num2 = num3 = num4 = num5 = <span class="nu0">3</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; num1 &lt;&lt; <span class="st0">&quot;, &quot;</span> &lt;&lt; num2 &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// But you wouldn&#8217;t want to clutter your code with tons of variables&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Why not use an array?</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> someArray<span class="br0">&#91;</span><span class="nu0">40</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Arrays use this notation where the number inside the square brackets <br />
&nbsp; &nbsp; &nbsp;indicates how many variables you&#8217;re storing. <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Arrays can be initialized right away&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> otherArray<span class="br0">&#91;</span><span class="nu0">10</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span><span class="nu0">0</span><span class="br0">&#125;</span>; &nbsp;<span class="co1">// Shortcut to set all to 0</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> anArray<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span> <span class="nu0">7</span>, <span class="nu0">1</span>, <span class="nu0">4</span>, <span class="nu0">16</span>, <span class="nu0">94</span> <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Or each element can be initialized later&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// To access a particular element of the array, we indicate the index.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Index 3: &quot;</span> &lt;&lt; anArray<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> &lt;&lt; endl;</p>
<p>&nbsp; &nbsp; &nbsp;<span class="co1">// The confusing part is that the indices of an array start at 0. &nbsp;That </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// means that the above statement would print 16.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; &nbsp; &nbsp; &nbsp; indices: &nbsp;0 &nbsp;1 &nbsp;2 &nbsp; 3 &nbsp; 4</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// int anArray[] = { 7, 1, 4, 16, 94 };</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; <span class="nu0">4</span> ; i++<span class="br0">&#41;</span> &nbsp;<span class="co1">// Start at index zero.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; anArray<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Be careful not to overrun the array. &nbsp;You&#8217;ll encounter weird results </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// and very likely cause an error if you use anArray[7] when anArray has </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// only five elements.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// C++ can use arrays to form strings of characters. &nbsp;Instead of filling </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// all of the available spaces with data, however, it needs to use the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// last element as an &quot;end of string&quot; character, &#8216;\0&#8242;. &nbsp;This is also </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// called the &quot;null terminator&quot;, &quot;null zero&quot;, or &quot;zero terminator&quot;. &nbsp;If </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the compiler doesn&#8217;t find this character in the array, it keeps </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// reading (or writing!) into other parts of memory (overruns the array).</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> someString<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span> = <span class="st0">&quot;Hello&quot;</span>; &nbsp;<span class="co1">// This means to only use 5 letters!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// You can assign literal strings to these arrays.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// C++ will take care of the null terminator</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// That&#8217;s too confusing, so you can make a character array figure out how </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// much memory it needs on it&#8217;s own, but only when you first create </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the array.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> anotherString<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="st0">&quot;How many elements does this have?&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> count = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> i = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>anotherString<span class="br0">&#91;</span>i<span class="br0">&#93;</span> != <span class="st0">&#8216;<span class="es0">\0</span>&#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;anotherString has &quot;</span> &lt;&lt; count &lt;&lt; <span class="st0">&quot; elements.&quot;</span> &lt;&lt; endl;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void arrays()</span></p>
<p>
<span class="kw4">void</span> stringClass<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Stuff in this section requires #include &lt;string&gt;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;C++ Strings are way easier than C strings to understand and use. &nbsp;Since <br />
&nbsp; &nbsp; &nbsp;it is a string class, it is loaded up (down?) with tons of ways to work <br />
&nbsp; &nbsp; &nbsp;with the information. &nbsp;We&#8217;ll focus on the basics that will get you almost <br />
&nbsp; &nbsp; &nbsp;anywhere with C++ strings.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;string word;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;word = <span class="st0">&quot;We shall overcome!&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Particular characters in strings can be accessed just like arrays.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> &lt;&lt; <span class="st0">&quot; &quot;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">5</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">6</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">7</span><span class="br0">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot; &quot;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">9</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">12</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">13</span><span class="br0">&#93;</span> &lt;&lt; word<span class="br0">&#91;</span><span class="nu0">17</span><span class="br0">&#93;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You can easily concatenate (combine) strings with the + operator.</span><br />
&nbsp; &nbsp; &nbsp;string word2 = <span class="st0">&quot; &nbsp;Unless otherwise noted.&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word + word2 &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;word += word2;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Because string is a class, objects (such as word and word2) each have <br />
&nbsp; &nbsp; &nbsp;access to special string functions. &nbsp;They are accessed with the dot (.) <br />
&nbsp; &nbsp; &nbsp;operator.<br />
&nbsp; &nbsp; &nbsp;The empty() function returns a boolean value telling whether or <br />
&nbsp; &nbsp; &nbsp;not the string is empty (useful for loops).<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span> word.<span class="me1">empty</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;EMPTY!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// The first argument to erase() is the index to start from. &nbsp;The second </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// argument is the number of characters to erase. &nbsp;All characters after </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// the erased ones slide right down to replace them.</span><br />
&nbsp; &nbsp; &nbsp;word.<span class="me1">erase</span><span class="br0">&#40;</span><span class="nu0">3</span>, <span class="nu0">6</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// insert() takes an index and a string, squeezing that string into the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// point the index represents.</span><br />
&nbsp; &nbsp; &nbsp;word.<span class="me1">insert</span><span class="br0">&#40;</span><span class="nu0">7</span>, word2<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// length() takes no arguments and just returns the size of the string.</span><br />
&nbsp; &nbsp; &nbsp;word.<span class="me1">erase</span><span class="br0">&#40;</span> <span class="nu0">0</span>, word2.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; word &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Strings can be compared with the ==, &lt;, &gt;, &lt;=, &gt;=, and != operators.<br />
&nbsp; &nbsp; &nbsp;The quirk here is that it&#8217;s alphabetic comparison.<br />
&nbsp; &nbsp; &nbsp;&quot;abc&quot; is greater than &quot;jvc&quot;<br />
&nbsp; &nbsp; &nbsp;&quot;ghi&quot; &lt; &quot;ghj&quot;<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>word != word2<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;word != word2&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span> &nbsp;<span class="co1">// END void stringClass()</span></p>
<p>
<span class="kw4">void</span> randomStuff<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;The rand() function is C++&#8217;s way of getting a random number. &nbsp;In main(), we <br />
&nbsp; &nbsp; &nbsp;used a function call that looked like this: srand(time(NULL))<br />
&nbsp; &nbsp; &nbsp;&quot;srand&quot; stands for &quot;seed random [number generator]&quot;. &nbsp;Seeding something <br />
&nbsp; &nbsp; &nbsp;means to initialize it to a value. &nbsp;&quot;srand&quot; gives rand() a different <br />
&nbsp; &nbsp; &nbsp;starting place than it has by default. &nbsp;The reason this matters is because <br />
&nbsp; &nbsp; &nbsp;rand() is not actually random. &nbsp;rand() draws from a big list of numbers and <br />
&nbsp; &nbsp; &nbsp;gives them to you in order. &nbsp;Without seeding rand(), it will always start <br />
&nbsp; &nbsp; &nbsp;at the same place. &nbsp;In fact, this won&#8217;t do the job alone, but when we seed <br />
&nbsp; &nbsp; &nbsp;rand() with a constantly changing number (aka time), then we can get fairly<br />
&nbsp; &nbsp; &nbsp;reliable random numbers. &nbsp;Here&#8217;s where one great use of the modulus (%) <br />
&nbsp; &nbsp; &nbsp;operator is. &nbsp;We can use it to restrict the range of possible numbers.<br />
&nbsp; &nbsp; &nbsp;Modulo will return the remainder of integer division. &nbsp;It&#8217;s based on <br />
&nbsp; &nbsp; &nbsp;division, so the result ranges only from 0 to the right side value.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">99</span> + <span class="nu0">1</span>; &nbsp;<span class="co1">// Gives a number 1-100</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">2</span>; &nbsp;<span class="co1">// 0-1</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">65</span>; &nbsp;<span class="co1">// 0-64</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;rando = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">4000</span>; &nbsp;<span class="co1">// 0-3999</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; rando &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> strength = <span class="br0">&#40;</span><span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">6</span> + <span class="nu0">1</span><span class="br0">&#41;</span> + <span class="br0">&#40;</span><span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">6</span> + <span class="nu0">1</span><span class="br0">&#41;</span> + <span class="br0">&#40;</span><span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="nu0">6</span> + <span class="nu0">1</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// 3-18</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> damage = <span class="nu0">1</span> + <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%strength;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> low = <span class="nu0">55</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> high = <span class="nu0">231</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> lowToHigh = <span class="kw3">rand</span><span class="br0">&#40;</span><span class="br0">&#41;</span>%<span class="br0">&#40;</span>high + <span class="nu0">1</span> &#8211; low<span class="br0">&#41;</span> + low;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void randomStuff()</span></p>
<p>
<span class="kw4">void</span> structKeyword<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Structures are handy little things similar to arrays in concept. &nbsp;Arrays <br />
&nbsp; &nbsp; &nbsp;are collections of variables of the same type, whereas structures hold a <br />
&nbsp; &nbsp; &nbsp;bunch of variables of any types (even other structures) that you want to <br />
&nbsp; &nbsp; &nbsp;have grouped together.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> ITEM &nbsp;<span class="co1">// How about an item structure?</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> type; &nbsp;<span class="co1">// Which item is it?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> uses; &nbsp;<span class="co1">// How many times can I use it?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">bool</span> ready; &nbsp;<span class="co1">// Can I use it now?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">bool</span> equipped; &nbsp;<span class="co1">// Do I have it at hand?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string itemName;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* &nbsp;Structs are like type definitions. &nbsp;Now that we told the compiler what <br />
&nbsp; &nbsp; &nbsp;this struct is, we can make an instance of one.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ITEM gravyBoat;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">type</span> = <span class="nu0">42</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">uses</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">ready</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">equipped</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;gravyBoat.<span class="me1">itemName</span> = <span class="st0">&quot;gravyboat&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> PLAYER &nbsp;<span class="co1">// But what use is an item without a player to use it?</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> hp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> magic;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> active;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> location;<br />
&nbsp; &nbsp; &nbsp; &nbsp;ITEM item;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> GRID &nbsp;<span class="co1">// Here&#8217;s a type of grid structure.</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">int</span> x,y; &nbsp; <span class="co1">// We&#8217;ll store some position data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> active; &nbsp;<span class="co1">// Does the grid position exist (say, on this map)?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> occupied; &nbsp;<span class="co1">// Is there something already in this grid position?</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;GRID grid<span class="br0">&#91;</span><span class="nu0">15</span><span class="br0">&#93;</span>; &nbsp;<span class="co1">// Now we have a grid with 16 possible positions.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;PLAYER player1; &nbsp;<span class="co1">// I don&#8217;t feel like initializing everything, but remember </span><br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">hp</span> = <span class="nu0">50</span>; <span class="co1">// &nbsp; to never use something that hasn&#8217;t been initialized.</span><br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">location</span> = <span class="nu0">10</span>;<br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">item</span>.<span class="me1">uses</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">item</span>.<span class="me1">itemName</span> = <span class="st0">&quot;gravyboat&quot;</span>;<br />
&nbsp; &nbsp; &nbsp;grid<span class="br0">&#91;</span>player1.<span class="me1">location</span><span class="br0">&#93;</span>.<span class="me1">occupied</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;player1.<span class="me1">item</span>.<span class="me1">uses</span>&#8211;;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;You&#8217;ve been &quot;</span> &lt;&lt; player1.<span class="me1">item</span>.<span class="me1">itemName</span> &lt;&lt; <span class="st0">&quot;ed! &nbsp;&#8230;At grid number &quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; player1.<span class="me1">location</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Structures are the precursors to a very important aspect of C++ and most <br />
&nbsp; &nbsp; &nbsp;other programming languages&#8230; &nbsp;Classes. &nbsp;Classes will be covered in just <br />
&nbsp; &nbsp; &nbsp;a few sections, but I&#8217;ll give you a little clue to one of their powerful <br />
&nbsp; &nbsp; &nbsp;features&#8230; &nbsp;Classes are like structures that can contain functions.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void structKeyword()</span></p>
<p>
<span class="kw4">void</span> recursion<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Recursion is a technique that requires a lot of practice to really <br />
&nbsp; &nbsp; &nbsp;figure it out. &nbsp;Recursion is when a function calls itself. &nbsp;There are <br />
&nbsp; &nbsp; &nbsp;literally a limitless number of reasons to do this. &nbsp;We&#8217;ll check out a few. &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Take a look below recursion() for the definition of this function:</span><br />
&nbsp; &nbsp; &nbsp;countDown<span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// As you can see, countDown() takes an integer and prints all the numbers </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// down to zero. &nbsp;This is an example of a recursive function that relies </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// on &quot;side-effects&quot; to produce a result. &nbsp;Side-effects are those that </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// come about by running a function and not just by dropping and catching</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// functions&#8217; return values.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This one is a recursive function that hints at the power of recursion.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> number = <span class="nu0">5</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> answer = fact<span class="br0">&#40;</span>number<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Factorial of &quot;</span> &lt;&lt; number &lt;&lt; <span class="st0">&quot; equals &quot;</span> &lt;&lt; answer &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void recursion()</span></p>
<p>
<span class="kw4">void</span> countDown<span class="br0">&#40;</span><span class="kw4">int</span> count<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt; count &lt;&lt; endl;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>count &gt; <span class="nu0">0</span><span class="br0">&#41;</span> countDown<span class="br0">&#40;</span>&#8211;count<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">int</span> fact<span class="br0">&#40;</span><span class="kw4">int</span> num<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>num == <span class="nu0">0</span><span class="br0">&#41;</span> &nbsp;<span class="co1">// Ends the recursion&#8230; &nbsp;We know that 0! = 1</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> num * fact<span class="br0">&#40;</span>num &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// A recursive definition of factorial</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// n! = n * (n-1) * (n-2) * &#8230; * 1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// The easiest ideas to model are those that </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// have a sequence like factorial does.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// num * fact(num &#8211; 1) could be rewritten as:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// &nbsp;n &nbsp;* &nbsp;(n-1)!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// As you can see (or will hopefully see), every recursive function has two </span><br />
&nbsp; &nbsp; <span class="co1">// main parts. &nbsp;It needs something to recurse around (num*fact(num-1)) and </span><br />
&nbsp; &nbsp; <span class="co1">// it needs something to end the recursion (if (num==0) return 1;). &nbsp;Lots </span><br />
&nbsp; &nbsp; <span class="co1">// of recursive functions have other parts, but they all have those two </span><br />
&nbsp; &nbsp; <span class="co1">// in common.</span><br />
<span class="br0">&#125;</span></p>
<p>
<span class="kw4">void</span> pointers<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Computers store running program data in what is called &quot;Random Access <br />
&nbsp; &nbsp; &nbsp;Memory&quot; or RAM. &nbsp;How does the computer keep track of all the stuff in RAM? &nbsp;<br />
&nbsp; &nbsp; &nbsp;It knows that the memory is set up so that a single &#8216;memory location&#8217; can <br />
&nbsp; &nbsp; &nbsp;be accessed by referring to its &#8216;memory address&#8217;. &nbsp;C++ has a very low-level<br />
&nbsp; &nbsp; &nbsp;(very close to how the computer actually works&#8230; and consequently kind of <br />
&nbsp; &nbsp; &nbsp;hard to deal with, but very efficient) way of working with memory <br />
&nbsp; &nbsp; &nbsp;addresses. &nbsp;We can store the address of some information in a variable <br />
&nbsp; &nbsp; &nbsp;called a &#8216;pointer&#8217; (it points to actual data).<br />
&nbsp; &nbsp; &nbsp;*/</span> <br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span>* intPointer; &nbsp;<span class="co1">// This can point to an integer&#8230; but doesn&#8217;t yet.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span>* charPointer; &nbsp;<span class="co1">// This can point to a character.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Pointers are denoted with a type like: int*</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This is their actual type now. &nbsp;Instead of being just of type &#8216;int&#8217;, </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// intPointer is of type &#8216;int*&#8217;. &nbsp;It&#8217;s best not to confuse this use of the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// asterisk &#8216;*&#8217; with what is used next. &nbsp;Think of this as part of the </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// type&#8217;s name.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> integer;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> character;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;intPointer = &amp;integer; &nbsp;<span class="co1">// Here we use the ampersand &#8216;&amp;&#8217; operator. &nbsp;This </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// returns the address of the variable it is put in</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// front of. &nbsp;You can think of this as the &quot;address of&quot; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// operator. &nbsp;Make sure you initialize your pointers to </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// an actual address like this before you try to use them.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// That could cause a crash of your program. &nbsp;Remember </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// that because it is a pretty common mistake.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;charPointer = &amp;character;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*intPointer = <span class="nu0">5</span>; &nbsp;<span class="co1">// This assigns the value 5 to the place that is pointed </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// at by intPointer. &nbsp;This place is also named by </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// the variable &quot;integer&quot;.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* &nbsp;The asterisk &#8216;*&#8217; or star is called the dereference operator. &nbsp;This means that <br />
&nbsp; &nbsp; &nbsp;it actually returns the data held in memory. &nbsp;*intPointer is like the <br />
&nbsp; &nbsp; &nbsp;variable name of the data in that location. &nbsp;You can remember what the * <br />
&nbsp; &nbsp; &nbsp;does if you think of it as the &quot;stuff at&quot; operator. &nbsp;Then it means the <br />
&nbsp; &nbsp; &nbsp;&quot;stuff at&quot; the address intPointer. &nbsp;Now we can really use these pointers. &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*charPointer = <span class="st0">&#8216;g&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span> <span class="br0">&#40;</span>*intPointer / <span class="nu0">5</span> == <span class="nu0">1</span><span class="br0">&#41;</span> *charPointer = <span class="st0">&#8216;p&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/* Now why don&#8217;t we just use normal variables for this? &nbsp;Well, we would.<br />
&nbsp; &nbsp; &nbsp;But if you&#8217;ve just been following along in this tutorial, there&#8217;s something <br />
&nbsp; &nbsp; &nbsp;you probably haven&#8217;t noticed. &nbsp;Let&#8217;s call a function.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> a = <span class="nu0">3</span>;<br />
&nbsp; &nbsp; &nbsp;square<span class="br0">&#40;</span>a<span class="br0">&#41;</span>; &nbsp;<span class="co1">// This function takes an int and sets it to equal itself </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// times itself. &nbsp;It&#8217;s declared void, so there&#8217;s no </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// returning data.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; a &lt;&lt; endl; &nbsp;<span class="coMULTI">/* This prints &#8217;3&#8242;, the exact value we passed in! &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; This is because we did something called <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8216;pass-by-value&#8217;. &nbsp;We passed the variable &#8216;a&#8217; to the <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function square(). &nbsp;square() made a copy of the <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value stored in &#8216;a&#8217;. &nbsp;As you can see below this <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; section, the function square() names this copied <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; variable &#8216;x&#8217;. &nbsp;It then sets &#8216;x&#8217; to equal x * x.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Obviously, we&#8217;re not working with &#8216;a&#8217; anymore. &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; That&#8217;s why it didn&#8217;t change when we wanted it to.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; So we&#8217;d rather use &#8216;pass-by-reference&#8217;. Take a look<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; at squarePtr(). &nbsp;Here we&#8217;re sending the address of a <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; variable. &nbsp;Let&#8217;s try using it.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span>* aPtr = &amp;a;; &nbsp;<span class="co1">// Here&#8217;s a pointer that we&#8217;ll assign to the address of &#8216;a&#8217;.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;squarePtr<span class="br0">&#40;</span>aPtr<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; a &lt;&lt; endl; &nbsp;<span class="co1">// Now we get our 9!</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;squarePtr<span class="br0">&#40;</span>&amp;a<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Another way to do this is to directly send the address</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// of &#8216;a&#8217;. &nbsp;This doesn&#8217;t require an extra pointer to use.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; a &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// A few uses for pointers will be expanded in the dynamicMemory() section</span><br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void pointers()</span></p>
<p><span class="kw4">void</span> square<span class="br0">&#40;</span><span class="kw4">int</span> x<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; x *= x;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> squarePtr<span class="br0">&#40;</span><span class="kw4">int</span>* x<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; *x *= *x; &nbsp;<span class="co1">// Kinda messy here, but we&#8217;re working with the &quot;stuff at&quot; x.</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> references<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;References are very much like pointers, but they make functions like <br />
&nbsp; &nbsp; &nbsp;squarePtr() much easier to design. &nbsp;A reference is just another name for <br />
&nbsp; &nbsp; &nbsp;the memory address of a variable.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> z = <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;int&amp; aReference = z; &nbsp;<span class="co1">// Here&#8217;s a reference.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;For the most part, references are used for functions that will take and <br />
&nbsp; &nbsp; &nbsp;change a given variable. &nbsp;Another use is for &#8216;aliases&#8217;, but they aren&#8217;t <br />
&nbsp; &nbsp; &nbsp;that useful, so I won&#8217;t go into them any further.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;Let&#8217;s try our nice, new, and clean squareRef() function.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> var = <span class="nu0">12</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;squareRef<span class="br0">&#40;</span>var<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; var &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// As you can see, I didn&#8217;t have to mess with my variable at all!</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// How convenient!</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void references()</span></p>
<p><span class="kw4">void</span> squareRef<span class="br0">&#40;</span>int&amp; x<span class="br0">&#41;</span> &nbsp;<span class="co1">// Notice the difference?</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;x *= x;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> functionOverloading<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Function overloading is much easier and more useful than it sounds.<br />
&nbsp; &nbsp; &nbsp;Overloading a function is to use the same function name for multiple <br />
&nbsp; &nbsp; &nbsp;functions. &nbsp;This comes in handy when you want to do similar tasks with <br />
&nbsp; &nbsp; &nbsp;different variable types without having to come up with (and remember) a <br />
&nbsp; &nbsp; &nbsp;unique name for each function.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;If you look at the &#8216;equal()&#8217; functions below this section, you can see <br />
&nbsp; &nbsp; &nbsp;some overloaded functions.<br />
&nbsp; &nbsp; &nbsp;We want to know if some integers are equal, but what if we don&#8217;t always <br />
&nbsp; &nbsp; &nbsp;know how many we&#8217;re working with?<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> a = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> b = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> c = <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> cha = <span class="st0">&#8216;@&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> chb = <span class="st0">&#8216;@&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; equal<span class="br0">&#40;</span>a<span class="br0">&#41;</span> &lt;&lt; endl &nbsp;<span class="co1">// always true (1)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; equal<span class="br0">&#40;</span>a, b<span class="br0">&#41;</span> &lt;&lt; endl &nbsp;<span class="co1">// true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; equal<span class="br0">&#40;</span>a, b, c<span class="br0">&#41;</span> &lt;&lt; endl &nbsp;<span class="co1">// false!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; equal<span class="br0">&#40;</span>cha, chb<span class="br0">&#41;</span> &lt;&lt; endl; &nbsp;<span class="co1">// true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Thankfully, the compiler is smart enough to recognize it when you use <br />
&nbsp; &nbsp; &nbsp;different types and different numbers of arguments to functions. &nbsp;That&#8217;s<br />
&nbsp; &nbsp; &nbsp;what allows for this feature. &nbsp;Different return types will not disambiguate<br />
&nbsp; &nbsp; &nbsp;the call on their own, though, so don&#8217;t try just that.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void functionOverloading()</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a == b;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b, <span class="kw4">int</span> c<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a == b &amp;&amp; b == c;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">bool</span> equal<span class="br0">&#40;</span><span class="kw4">char</span> a, <span class="kw4">char</span> b<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a == b;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw4">void</span> scope<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Scope is a term for what variables you have access to at a certain <br />
&nbsp; &nbsp; &nbsp;point in a program.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;We&#8217;ve already talked a little about the &#8216;global&#8217; scope.<br />
&nbsp; &nbsp; &nbsp;Global variables can be used anywhere in your program because they were <br />
&nbsp; &nbsp; &nbsp;declared outside of any function.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">//Global variable &#8216;aLetter&#8217;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; aLetter &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">char</span> tempLetter = aLetter;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This just created a variable that is &#8216;local&#8217; to the function scope().</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// When this function ends, this variable is automatically deleted.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// No other function can use &#8216;tempLetter&#8217; unless I pass it as an argument.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here I&#8217;m making another local variable and calling equal() with two </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// passed variables.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">bool</span> test = equal<span class="br0">&#40;</span>tempLetter, aLetter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>test<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;These are equal!&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// You&#8217;ll need to be aware of the scope you&#8217;re working in when it comes </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// to classes in a couple of sections&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void scope()</span></p>
<p>
<span class="kw4">void</span> dynamicMemory<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Dynamic memory is a term for program memory that is not stored at compile <br />
&nbsp; &nbsp; &nbsp;time. &nbsp;With the usual variable declarations that we&#8217;ve been working with, <br />
&nbsp; &nbsp; &nbsp;the variables have been hard-coded into the program and will work exactly <br />
&nbsp; &nbsp; &nbsp;the same for every time the program is run. &nbsp;If we want to make variables <br />
&nbsp; &nbsp; &nbsp;that don&#8217;t automatically take up space or if, say, we don&#8217;t know how many <br />
&nbsp; &nbsp; &nbsp;variables we need (in the case of an array), then we need to use C++&#8217;s <br />
&nbsp; &nbsp; &nbsp;dynamic memory operators, &#8216;new&#8217; and &#8216;delete&#8217;.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;Simply put, &#8216;new&#8217; creates a new variable and &#8216;delete&#8217; frees the memory <br />
&nbsp; &nbsp; &nbsp;that is used by that variable so that the memory can be used again before <br />
&nbsp; &nbsp; &nbsp;the program closes.<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;So, let&#8217;s try making an array of an unknown size.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> numElements = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span><span class="br0">&#40;</span>numElements == <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;numElements = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Hey, tell me how many variables we&#8217;ll have.&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cin</span> &gt;&gt; numElements;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Arrays are actually pointers to the first element of the array, so we </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// can write them like pointers:</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span>* anArray = <span class="kw3">new</span> <span class="kw4">int</span><span class="br0">&#91;</span>numElements<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Let&#8217;s initialize the elements</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; numElements; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;anArray<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Now we can use them for whatever</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; numElements; i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Element #&quot;</span> &lt;&lt; i &lt;&lt; <span class="st0">&quot; = &quot;</span> &lt;&lt; anArray<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Then free the memory when we&#8217;re done.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">delete</span><span class="br0">&#91;</span><span class="br0">&#93;</span> anArray;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;To delete an entire array, you have to put those square brackets after<br />
&nbsp; &nbsp; &nbsp;delete. &nbsp;If you don&#8217;t, it will treat it like a pointer instead of an array <br />
&nbsp; &nbsp; &nbsp;and will only delete the first element.<br />
&nbsp; &nbsp; &nbsp;*/</span></p>
<p>&nbsp; &nbsp; &nbsp;<span class="co1">// So, pointers to any other type work like that.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">double</span>* dPtr;<br />
&nbsp; &nbsp; &nbsp;dPtr = <span class="kw3">new</span> <span class="kw4">double</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Use it&#8230;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Then delete it.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">delete</span> dPtr;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Using &#8216;delete&#8217; effectively is the main concern for many programs written <br />
&nbsp; &nbsp; &nbsp;in C++. &nbsp;If an allocated resource is not freed, then the memory is not <br />
&nbsp; &nbsp; &nbsp;usable until the program ends. &nbsp;This is called a &quot;memory leak&quot;. &nbsp;Always <br />
&nbsp; &nbsp; &nbsp;remember to delete every dynamic variable. &nbsp;Some programmers even write <br />
&nbsp; &nbsp; &nbsp;a delete statement immediately whenever they write a new statement so that <br />
&nbsp; &nbsp; &nbsp;they don&#8217;t forget.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw4">struct</span> Something<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> type;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> number;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;Something* number1 = <span class="kw3">new</span> Something;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;number1-&gt;type = <span class="nu0">4</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; number1-&gt;type &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here we used a great little operator, the arrow operator.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This is a shortcut to replace the asterisk-dot combo.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// If we didn&#8217;t use this, we would write:</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(*number).type = 4;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; Since the dot operator has a higher precedence than the dereference</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; operator, the arrow is much cleaner (and makes me think of &quot;pointer&quot;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp; anyway). &nbsp;The arrow &quot;points&quot; to a member.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span>*number1<span class="br0">&#41;</span>.<span class="me1">type</span> = <span class="nu0">20</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="br0">&#40;</span>*number1<span class="br0">&#41;</span>.<span class="me1">type</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">delete</span> number1;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void dynamicMemory()</span></p>
<p>
<span class="kw4">void</span> classes<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//See ClassName.h, the #included header file for the details</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Let&#8217;s just go through some object handling now.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ClassName* object = <span class="kw3">new</span> ClassName;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Use that arrow operator to work in the scope of the object.</span><br />
&nbsp; &nbsp; &nbsp;object-&gt;data = <span class="nu0">3</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;7 * 2 = &quot;</span> &lt;&lt; object-&gt;fn<span class="br0">&#40;</span><span class="nu0">7</span><span class="br0">&#41;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Data * 2 = &quot;</span> &lt;&lt; object-&gt;fn<span class="br0">&#40;</span>object-&gt;data<span class="br0">&#41;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;object-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;InheritsFromClassName* anotherObject = <span class="kw3">new</span> InheritsFromClassName;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This still has the members of the base class.</span><br />
&nbsp; &nbsp; &nbsp;anotherObject-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// But it also has it&#8217;s own stuff&#8230;</span><br />
&nbsp; &nbsp; &nbsp;anotherObject-&gt;notCow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Here&#8217;s a great way to work with inherited classes.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// It&#8217;s part of a topic called &#8216;Polymorphism&#8217;.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ClassName** array = <span class="kw3">new</span> ClassName*<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>; &nbsp;<span class="co1">// Will hold two pointers to </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// objects of type ClassName.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = object; &nbsp;<span class="co1">// This is a ClassName object</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> = anotherObject; &nbsp;<span class="co1">// Hey! &nbsp;This is too (deep down somewhere)!</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Now we can cow() together in an array!</span><br />
&nbsp; &nbsp; &nbsp;array<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>-&gt;cow<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// But only with members of the array&#8217;s class type which </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// in this case is ClassName.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//array[1]-&gt;notCow(); // ERROR!! &nbsp;The program sees this as just a ClassName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<span class="co1">// If you definitely know that array[1] is an InheritsFromClassName object,</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// then you can use &#8216;type-casting&#8217; to change it back into its full </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// abilities. &nbsp;Type-casting is done by writing a type within parentheses </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// and putting that before the variable that you want to convert to </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// that type.</span><br />
&nbsp; &nbsp; &nbsp;InheritsFromClassName* ifcn = <span class="br0">&#40;</span>InheritsFromClassName*<span class="br0">&#41;</span>array<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Now we can use the InheritsFromClassName members!</span><br />
&nbsp; &nbsp; &nbsp;ifcn-&gt;notCow<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// This also works for the primitive types. &nbsp;Try this if you get warnings </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// from your compiler about incompatible types.</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">float</span> flo = <span class="nu0">6.0</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">//int q = flo; &nbsp;// Warning</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> z = <span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>flo;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// C++ offers some other cool type-casting operators, but I&#8217;ll just show </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// you one pretty useful one.</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// dynamic_cast returns what you would expect from a cast if it is valid, </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// but returns a NULL value if the cast is not valid.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// &nbsp;dynamic_cast&lt;TYPE&gt;(VARIABLE)</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;ClassName* cn = <span class="kw3">new</span> ClassName<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Junk is a class I defined that just has an integer member named &#8216;j&#8217;</span><br />
&nbsp; &nbsp; &nbsp;Junk* junky = dynamic_cast&lt;Junk*&gt;<span class="br0">&#40;</span>cn<span class="br0">&#41;</span>; &nbsp;<span class="co1">// Not a valid cast</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>junky == <span class="kw2">NULL</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Junky NULL<span class="es0">\n</span>&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; <span class="st0">&quot;Junky not NULL?<span class="es0">\n</span>&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="co1">// dynamic_cast does its work on the fly, so don&#8217;t use it too much when </span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// you need a program to run fast.</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Well, this is the end of the tutorial. &nbsp;I hope you&#8217;ve been able to absorb <br />
&nbsp; &nbsp; &nbsp;all that and come away with some basic, working knowledge of C++ and <br />
&nbsp; &nbsp; &nbsp;programming in general. &nbsp;If you have any questions including any on other <br />
&nbsp; &nbsp; &nbsp;C++ topics (seen below), just catch me at GrimFang4@hotmail.com<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;It&#8217;s been a pleasure.<br />
&nbsp; &nbsp; &nbsp;-Jonny D<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span class="coMULTI">/*<br />
&nbsp; &nbsp; &nbsp;Some Other Topics:<br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;templates<br />
&nbsp; &nbsp; &nbsp; &nbsp;extern, file splitting<br />
&nbsp; &nbsp; &nbsp; &nbsp;linked lists, queues, stacks<br />
&nbsp; &nbsp; &nbsp; &nbsp;bubble sort<br />
&nbsp; &nbsp; &nbsp; &nbsp;abstract classes, virtual functions<br />
&nbsp; &nbsp; &nbsp; &nbsp;file streams<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void classes()</span></p>
<p><span class="kw4">void</span> choice<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw4">int</span> choice = <span class="nu0">-1</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span>choice != <span class="nu0">16</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;Type a choice and press enter&#8230;&quot;</span> &lt;&lt; endl;<br />
&nbsp; &nbsp; &nbsp;<span class="kw3">cout</span> &lt;&lt; endl &lt;&lt; <span class="st0">&quot;1 = Operators &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;6 = String Class &nbsp; &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;11 = References&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;2 = Std &#8216;Put &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;7 = Random Stuff &nbsp; &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;12 = Function Overloading&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;3 = Logic &nbsp; &nbsp; &nbsp;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;8 = Struct Keyword &nbsp;&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;13 = Scope&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;4 = Controls &nbsp; &quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;9 = Recursion &nbsp; &nbsp; &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;14 = Dynamic Memory&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;5 = Arrays &nbsp; &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;10 = Pointers &nbsp; &nbsp; &nbsp; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot;15 = Classes&quot;</span> &lt;&lt; endl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; <span class="st0">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 16 = Exit Testorial!&quot;</span> &lt;&lt; endl &lt;&lt; endl &lt;&lt; <span class="st0">&quot;-&gt;&quot;</span>;</p>
<p>&nbsp; &nbsp; &nbsp;<span class="kw3">cin</span> &gt;&gt; choice;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">switch</span><span class="br0">&#40;</span>choice<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;operators<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// +, -, *, /, =, etc.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">2</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stdPut<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// cout and cin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">3</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logic<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// if (conditionals), &amp;&amp;, ||, ==, !, !=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">4</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;controls<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// switch, loops with while, do while, and for</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">5</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arrays<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// arrays[] as lists of variables</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">6</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stringClass<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Explaining the string class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">7</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;randomStuff<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// rand() and random number generation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">8</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;structKeyword<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Structures and struct, the precursor to classes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">9</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;recursion<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// Looping without loops, function self-calls</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">10</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pointers<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// -&gt; memory addresses</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">11</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;references<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// An easier way to pass by reference</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">12</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;functionOverloading<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// functions with nearly the same name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">13</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scope<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// local vs. global, precedence rules</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">14</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dynamicMemory<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// new and delete[] memory management</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">15</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;classes<span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">// what makes C++ better than C, classes and inheritance</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">16</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>choice != <span class="nu0">16</span><span class="br0">&#41;</span> <span class="kw3">system</span><span class="br0">&#40;</span><span class="st0">&quot;PAUSE&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> &nbsp;<span class="co1">// END void choice()</span><br />
&nbsp;</div>
<p><br/><br/><br />
Files:<br />
<a href="/tutorials/bluedinocode/jonny-ds-c-tutorial.zip">jonny-ds-c-tutorial.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sdltutorials.com/jonny-ds-c-tutorial/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
