⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.htm

📁 C++ From Scratch: An Object-Oriented Approach is designed to walk novice programmers through the ana
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//-->                                                                </SCRIPT>	<META NAME="Author" Content="Steph Mineart">	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>C++ From Scratch -- Ch 2 -- Getting Started</TITLE><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>C++ From Scratch</H1></CENTER><CENTER>  <P><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>   <HR></CENTER><H1 align="center"> 2  </H1><h1 align="center"> Getting Started</h1><ul>  <li><a href="#Heading1">Why Teach a Process that Is Only Good for Smaller Projects?</a>     <ul>      <li><a href="#Heading2">Namespaces</a>       <li><a href="#Heading3">using namespace std</a>       <li><a href="#Heading4">Returning a Value</a>       <li><a href="#Heading5">main() Is More Equal than Others</a>       <li><a href="#Heading6">Using cout to Print to the Screen</a>       <li><a href="#Heading7"> Variables</a>       <li><a href="#Heading8">Characters</a>       <li><a href="#Heading9">Built-In Types</a>       <li><a href="#Heading10">Constants</a>     </ul></ul><hr size=4> <p><b>In This Chapter</b></p><ul>  <li>     <p> How Big Is a Small Project</p>  </li>  <p></p>  <li>     <p> Bootstrapping Your Knowledge</p>  </li>  <p></p>  <li>     <p> Creating the Project</p>  </li>  <p></p>  <li>     <p> Examining the Code</p>  </li>  <p></p>  <li>     <p> Code Spelunking</p>  </li>  <p></p>  <li>     <p> Analyzing the Code</p>  </li></ul><p>With a fairly simple program such as Decryptix!, my first goal is to get a   version up and running--and to keep it running. After it is working, I'll add   features, redesigning on-the-fly as I go.</p><p>On a large project, this can be a fatally inefficient process. As features   are added, the complexity of the overall project grows, and without a good design   you end up with code that is hard to maintain. </p><p>With a smaller project such as Decryptix!, however, the risk is minimal. If   a new feature requires a complete redesign and rewrite of the program, no problem:   It only takes a couple of days to write it in the first place.  </p><p><b>How Big Is a Small Project?</b></p><p>How small must a program be to design it as you go? I'd argue that any program   that takes one person more than a few weeks to program ought to be subject to   a more rigorous design process. Here's why: Programs that evolve organically,   rather than by design, often have to be rewritten at least once. If doing so   is painful, it is worth working out the design up front. If rewriting is trivial,   however, nothing was lost by diving right in.  </p><h3> <a name="Heading1">Why Teach a Process that Is Only Good for Smaller Projects?</a></h3><p>"Why," I hear you ask, "show an example of organic design if all large projects   require formal design?" The answer is fairly straightforward: There's a lot   to learn in both programming and design. This book aims to teach programming;   you'll find lots of books (including a few I wrote) on object-oriented analysis   and design. You can't learn everything at once.</p><p>Nothing I teach in this book is inconsistent with good design; we just won't   take the time to design everything up front. I don't know about you, but I'm   eager to dive into some code.  </p><p><b>Bootstrapping Your Knowledge</b></p><p>In a classic C++ primer, I'd start with the structure of the program, introduce   statements and expressions, add variables and constants, and then turn to classes.   I'd build skill upon skill, and I'd be about 600 pages into the book before   you could even begin to write your Decryptix! program.</p><p>This book is different. You're going to jump right in and wallow around awhile.   Not all of it will make sense at first, and I'll gloss over lots of detail only   to return to it later in the book, but the essential flow of the program can   be explained pretty quickly. From time to time you'll take an "Excursion" to   related--but not strictly relevant--areas of C++.  </p><p><b>Creating the Project</b></p><p>This book is designed to be of use regardless of which compiler you are using   or what platform (for example, Windows or Mac) you are developing for. From   time to time, however, I'll demonstrate how you can accomplish a specific task   in Microsoft Visual C++ 6.0. Your compiler might be somewhat different, but   the principles are the same. With the knowledge that is provided here you can   easily read the documentation for your compiler and make the necessary adjustments.</p><p>I begin by creating a project. On the drive on which I installed my compiler   I have created a directory called Decryptix Projects. This will house all the   versions of the program I will create.</p><p>First I start Visual C++ and tell it to create a new Win32 Console Application   called Decryptix, as shown in Figure 2.1.</p><p><b>Figure 2.1</b> <i>Microsoft Visual C++ New Project.</i></p><p>If your compiler offers a <i>wizard</i> (a series of dialog boxes that helps   you make these decisions), choose whatever provides you with the simplest, text-based,   non-windowed, ISO-standard environment. In this case, I choose <b>empty application</b>.</p><p>After creating the project, Visual C++ drops me in the Integrated Development   Environment (IDE). I choose <b>File</b>, <b>New</b>, and enter a new C++ source   file named Decryptix.cpp.</p><p>In other environments, for example a text editor in UNIX, I'd just open a new   file and save it as Decryptix.cpp. Often, in IDEs, saving the file with the   .cpp extension signals that this is C++ source code and turns on source code   indentation support (and, sometimes, color-coded text!). Source code indentation   support means that when you type your source code the editor automatically indents   it properly for you. Thus, if you enter</p><pre><tt>if ( someValue &gt; thisValue )</tt></pre><p>and then press Enter, the editor automatically indents the next line. (Don't   worry about what this code does, it will all be explained in time.)</p><blockquote>  <hr>  <p><strong>NOTE: </strong> To learn what support your editor provides, please     check the documentation that comes with your compiler.  </p>  <hr></blockquote><p><b>Examining the Code</b></p><p>Now take a look at a preliminary version of Decryptix, in Listing 2.1. You   can open a file in your project, save it as Decryptix.cpp, and then enter this   code, exactly as shown.</p><blockquote>  <hr>  <p><strong>TIP: </strong> I strongly advise you to enter all the source code     yourself because that is the best way to learn. If you simply can't stand     the thought of all that typing, however, you can retrieve this code from the     CD that accompanies this book, or you can download this code--and all the     code for this book--from my Web site (go to <tt>www.libertyassociates.com</tt>     and click on Books &amp; Resources).</p>  <hr></blockquote><p>This program is quite advanced, and of course you won't understand much of   what you are reading. Don't be intimidated, however; this chapter and Chapter   3, "Program Flow," go over it line by line. You might find, however, that you   can get a pretty good idea of what the program is doing just by reading it as   prose.</p><p>Try running it and examining what it does, and then try matching the code to   the output.</p><p><b>Listing 2.1 First Glimpse of Decryptix!</b></p><pre><tt>0:  #include &lt;iostream&gt;</tt><tt>1:  </tt><tt>2:  int main()</tt><tt>3:  {</tt><tt>4:  	std::cout &lt;&lt; "Decryptix. Copyright 1999 Liberty ";</tt><tt>5:  	std::cout &lt;&lt; "Associates, Inc. Version 0.2\n " &lt;&lt; std::endl;</tt><tt>6:  	std::cout &lt;&lt; "There are two ways to play Decryptix: ";</tt><tt>7:  	std::cout &lt;&lt; " either you can guess a pattern I create, ";</tt><tt>8:  	std::cout &lt;&lt; "or I can guess your pattern.\n\n";</tt><tt>9:  </tt><tt>10:  	std::cout &lt;&lt; "If you are guessing, I will think of a\n ";</tt><tt>11:  	std::cout &lt;&lt; "pattern of letters (e.g., abcde).\n\n";</tt><tt>12:  </tt><tt>13:  	std::cout &lt;&lt; "On each turn, you guess the pattern and\n";</tt><tt>14:  	std::cout &lt;&lt; " I will tell you how many letters you \n";</tt><tt>15:  	std::cout &lt;&lt; "got right, and how many of the correct\n";</tt><tt>16:  	std::cout &lt;&lt; " letters were in the correct position.\n\n";</tt><tt>17:  </tt><tt>18:  	std::cout &lt;&lt; "The goal is to decode the puzzle as quickly\n";</tt><tt>19:  	std::cout &lt;&lt; "as possible. You control how many letters \n";</tt><tt>20:  	std::cout &lt;&lt; "can be used and how many positions\n";</tt><tt>21:  	std::cout &lt;&lt; " (e.g., 5 possible letters in 4 positions) \n";</tt><tt>22:  	std::cout &lt;&lt; "as well as whether or not the pattern might\n";</tt><tt>23:  	std::cout &lt;&lt; " contain duplicate letters (e.g., aabcd).\n\n";</tt><tt>24:  </tt><tt>25:  	std::cout &lt;&lt; "If I'm guessing, you think of a pattern \n";</tt><tt>26:  	std::cout &lt;&lt; "and score each of my answers.\n\n" &lt;&lt; std::endl;</tt><tt>27:  </tt><tt>28:  	const int minLetters = 2;</tt><tt>29:  	const int maxLetters = 10;</tt><tt>30:  	const int minPositions = 3;</tt><tt>31:  	const int maxPositions = 10;</tt><tt>32:  </tt><tt>33:  	int          howManyLetters = 0, howManyPositions = 0;</tt><tt>34:  	bool          duplicatesAllowed = false;</tt><tt>35:  	int          round = 1;</tt><tt>36:  </tt><tt>37:  	std::cout &lt;&lt; "How many letters? (";</tt><tt>38:  	std::cout &lt;&lt; minLetters &lt;&lt; "-" &lt;&lt; maxLetters &lt;&lt; "): ";</tt><tt>39:  	std::cin &gt;&gt; howManyLetters;</tt><tt>40:  </tt><tt>41:  	std::cout &lt;&lt; "How many positions? (";</tt><tt>42:  	std::cout &lt;&lt; minPositions &lt;&lt; "-" &lt;&lt; maxPositions &lt;&lt; "): ";</tt><tt>43:  	std::cin &gt;&gt; howManyPositions;</tt><tt>44:  </tt><tt>45:  	char     choice;</tt><tt>46:  	std::cout &lt;&lt; "Allow duplicates (y/n)? ";</tt><tt>47:  	std::cin &gt;&gt; choice;</tt><tt>48:  </tt><tt>49:  	return 0;</tt><tt>50:  }</tt></pre><p>Compile, link, and run this program. In Visual C++ you can do all this at once   by pressing <b>Ctrl+F5</b>. Here's the output:</p><pre><tt>Decryptix. Copyright 1999 Liberty Associates, Inc. Version 0.2</tt><tt>There are two ways to play Decryptix:</tt><tt> either you can guess a pattern I create,</tt><tt>or I can guess your pattern.</tt><tt>If you are guessing, I will think of a</tt><tt> pattern of letters (e.g., abcde).</tt><tt>On each turn, you guess the pattern and</tt><tt> I will tell you how many letters you</tt><tt>got right, and how many of the correct</tt><tt> letters were in the correct position.</tt><tt>The goal is to decode the puzzle as quickly</tt><tt>as possible. You control how many letters</tt><tt>can be used and how many positions</tt><tt> (e.g., 5 possible letters in 4 positions)</tt><tt>as well as whether or not the pattern might</tt><tt> contain duplicate letters (e.g., aabcd).</tt><tt>If I'm guessing, you think of a pattern</tt><tt>and score each of my answers.</tt><tt>How many letters? (2-10):</tt></pre><p><b>Analyzing the Code</b></p><p>The very first line of this program (Line 0) is </p><pre><tt>#include &lt;iostream&gt;</tt></pre><p>The goal of this line is to add to your current file the information it needs   to support <i>Input</i> and <i>Output</i> <i>streaming</i>: the capability to   read from the keyboard (input) and write to the screen (output). </p><blockquote>  <hr>  <p> <b>Input Stream</b>--How data comes into your program; typically from the     keyboard</p>  <p> <b>Output Stream</b>--How data leaves your program; typically to the display</p>  <hr></blockquote><p>Here's how it works: C++ now includes a group of supporting code called the   standard library, which provides objects to handle input and output. <tt>cin</tt>   is an object that handles input from the keyboard, and <tt>cout</tt> is an object   that handles output to the screen. The details of how they work are not important   at this point, but to use them you must include in your program the file iostream,   which provides their definitions. The definition of an object tells the compiler   what it needs to know in order for the object to be used.</p><blockquote>  <hr>  <p> <tt>cin</tt> is pronounced <i>see-in</i>, and <tt>cout</tt> is pronounced     <i>see-out</i>.</p>  <hr></blockquote><p>You include this file in your program with the <tt>#include</tt> statement.   When your compiler is invoked, the precompiler runs, reading through your program   and looking for lines that begin with the <tt>#</tt> symbol. When it sees <tt>#include</tt>,   it knows it must read in a file. The angle brackets (<tt>&lt;</tt> and <tt>&gt;</tt>)   say "look in the usual place." When you installed your compiler, it should have   set up "the usual place" to look for these files.</p><blockquote>  <hr>  <p> Some folks pronounce <tt>#</tt> as <i>hash</i>, others as <i>cross-hash</i>.     I call it <tt>pound</tt>, so I pronounce this line of code <i>pound include     eye-oh-stream .</i></p>  <hr></blockquote><p>The net effect is that the file iostream is read into your program at this   point, which is just what you want. You can now use the <tt>cout</tt> object,   as you'll see in a few moments. </p><blockquote>  <hr>  <p><strong>NOTE: </strong> Using the angle brackets, <tt>&lt;iostream&gt;</tt>     indicates that the precompiler is to "look in the usual place." An alternative     is to use double quote marks--for instance <tt>"myfile.h"</tt>--which say     "look in the current project directory and, failing that, look in the usual     place."</p>  <hr></blockquote><h3> <a name="Heading2">Namespaces</a></h3><p>Unlike the code in Chapter 1, "Introduction," this version uses the new ANSI/ISO   standard library header file &lt;iostream&gt; rather than &lt;iostream<b>.h</b>&gt;   (note that the new header doesn't use .h). </p><p>These headers support the new namespace protocols, which enable you to avoid   conflicts in the names of objects and methods when working with code you buy   from other vendors. For example, there might be two objects named <tt>cout</tt>.   We solve this by "qualifying" the name with <tt>std::</tt>, as shown on lines   4-26. This qualification with <tt>std::</tt> indicates to the compiler that   it is to use the <tt>cout</tt> object that is defined in the standard (<tt>std</tt>)   library, which comes with your compiler.</p><p>Unfortunately, this makes the code look much more complicated and difficult 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -