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

📄 c++ for dummys.txt

📁 Both the newest, hottest ight simulator and the simplest yet most powerful accounting programs
💻 TXT
📖 第 1 页 / 共 5 页
字号:
      In the example given here, C++ could tell right      corrected the problem, C++ would have masked 
      away — and without a doubt — that I had            the real problem. 
      screwed up. However, if C++ can figure out 
                                                          Finding an error buried in a program that 
      what I did wrong, why doesn’t it just fix the prob 
                                                          builds without complaining is difficult and time- 
      lem and go on? 
                                                          consuming. It’s far better to let the compiler find 
      The answer is simple but profound. C++ thinks        the error for you if at all possible. Generating 
      that I mistyped the >> symbol, but it may be         a compiler error is a waste of the computer’s 
      mistaken. What could have been a mistyped            time — forcing me to find a mistake that C++ 
      command may actually be some other, com              could have caught is a waste of my time. Guess 
      pletely unrelated error. Had the compiler simply     which one I vote for? 
                 The term parse means to convert the C++ commands into something that the 
                 machine-code-generating part of the process can work with. 
                 There was once a language that tried to fix simple mistakes like this for you. 
                 From my personal experience, I can tell you it was a waste of time — because 
                 (except for very simple cases) the compiler was almost always wrong. At 
                 least it warned me of the problem so I could fix it myself. 
  Executing Your Program
 
                 It’s now time to execute your new creation . . . that is, to run your program. You 
                 will run the CONVERT.EXE program file and give it input to see how well it works. 
                 To execute the Conversion program, click ExecuteRun or press Ctrl+F10. 
                 (I have no idea how they selected function keys. I would think that an action 
                 as common as executing a program would warrant its own function key — 
                 something without a Control or Shift key to hold down — but maybe that’s 
                 just me.) 
                 A window opens immediately, requesting a temperature in Celsius. Enter a 
                 known temperature, such as 100 degrees. After you press Enter, the program 
                 returns with the equivalent temperature of 212 degrees Fahrenheit as follows: 
                   Enter the temperature in Celsius:100 
                   Fahrenheit value is:212 
                   Press any key to continue . . . 
           
 
                                         Chapter 1: Writing Your First C++ Program        21 
     The message Press any key gives you the opportunity to read what you’ve 
     entered before it goes away. Press Enter, and the window (along with its con 
     tents) disappears. Congratulations! You just entered, built, and executed your 
     first C++ program. 
     Dev-C++ is not Windows
 
     Notice that Dev-C++ is not truly intended for developing Windows programs. 
     In theory, you can write a Windows application by using Dev-C++, but it isn’t 
     easy. (That’s so much easier in Visual Studio.NET.) 
     Windows programs show the user a very visually oriented output, all nicely 
     arranged in onscreen windows. Convesion.exe is a 32-bit program that exe 
     cutes under Windows, but it’s not a “Windows” program in the visual sense. 
     If you don’t know what 32-bit program means, don’t worry about it. As I said 
     earlier, this book isn’t about writing Windows programs. The C++ programs 
     you write in this book have a command line interface executing within an MS 
     DOS box. 
     Budding Windows programmers shouldn’t despair — you didn’t waste your 
     money. Learning C++ is a prerequisite to writing Windows programs. I think 
     that they should be mastered separately: C++ first, Windows second. 
     Dev-C++ help
 
     Dev-C++ provides a Help menu item. Choose Help followed by Help on Dev 
     C++ to open up a typical Windows help box. Help is provided on various aspects 
     of the Dev-C++ development package but not much else. Noticeably lacking is 
     help on the C++ language itself. Click a topic of interest to display help. 
Reviewing the Annotated Program
 
     Entering data in someone else’s program is about as exciting as watching some 
     one else drive a car. You really need to get behind the wheel itself. Programs 
     are a bit like cars as well. All cars are basically the same with small differences 
     and additions — OK, French cars are a lot different than other cars, but the 
     point is still valid. Cars follow the same basic pattern — steering wheel in front 
     of you, seat below you, roof above you and stuff like that. 
 
 
22   Part I: Introduction to C++ Programming 
                Similarly, all C++ programs follow a common pattern. This pattern is already 
                present in this very first program. We can review the Conversion program by 
                looking for the elements that are common to all programs. 
                Examining the framework 
                for all C++ programs 
                Every C++ program you write for this book uses the same basic framework, 
                which looks a lot like this: 
                  // 
                  // Template - provides a template to be used as the starting 
                  //                   point 
                  // 
                  // the following include files define the majority of 
                  // functions that any given program will need 
                  #include <cstdio> 
                  #include <cstdlib> 
                  #include <iostream> 
                  using namespace std; 
                  int main(int nNumberofArgs, char* pszArgs[]) 
                  { 
                      // your C++ code starts here 
                      // wait until user is ready before terminating program 
                      // to allow the user to see the program results 
                      system(“PAUSE”); 
                      return 0; 
                  } 
                Without going into all the boring details, execution begins with the code con 
                tained in the open and closed braces immediately following the line begin 
                ning main(). 
                I have copied this code into a file called Template.cpp located in the main 
                CPP_Programs folder on the enclosed CD-ROM. 
                Clarifying source code with comments
 
                The first few lines in Conversion.cpp appear to be freeform text. Either this 
                code was meant for human eyes or C++ is a lot smarter than I give it credit for. 
                These first six lines are known as comments. Comments are the programmer’s 
           
 
                                    Chapter 1: Writing Your First C++ Program      23 
  explanation of what he or she is doing or thinking when writing a particular 
  code segment. The compiler ignores comments. Programmers (good program 
  mers, anyway) don’t. 
  A C++ comment begins with a double slash (//) and ends with a newline. You 
  can put any character you want in a comment. A comment may be as long as 
  you want, but it’s customary to keep comment lines to no more than 80 char 
  acters across. Back in the old days — “old” is relative here — screens were 
  limited to 80 characters in width. Some printers still default to 80 characters 
  across when printing text. These days, keeping a single line to under 80 char 
  acters is just a good practical idea (easier to read, less likely to cause eye 
  strain, the usual). 
  A newline was known as a carriage return back in the days of typewriters — 
  when the act of entering characters into a machine was called typing and not 
  keyboarding. A newline is the character that terminates a command line. 
  C++ allows a second form of comment in which everything appearing after a 
  /* and before a */ is ignored; however, this form of comment isn’t normally 
  used in C++ anymore. (Later in this book, I describe the one case in which 
  this type of comment is applied.) 
  It may seem odd to have a command in C++ (or any other programming lan 
  guage) that’s specifically ignored by the computer. However, all computer lan 
  guages have some version of the comment. It’s critical that the programmer 
  explain what was going through her mind when she wrote the code. A pro- 
  grammer’s thoughts may not be obvious to the next colleague who picks up 
  her program and tries to use it or modify it. In fact, the programmer herself 
  may forget what her program meant if she looks at it months after writing the 
  original code and has left no clue. 
  Basing programs on C++ statements
 
  All C++ programs are based on what are known as C++ statements. This sec 
  tion reviews the statements that make up the program framework used by 
  the Conversion.cpp program. 
  A statement is a single set of commands. All statements other than comments 
  end with a semicolon. (There’s a reason that comments don’t end with a 
  semicolon, but it’s obscure. To my mind, comments should end in semicolons 
  as well, for consistency’s sake. Why nobody asked me about that remains a 
  mystery.) 
    
 
24   Part I: Introduction to C++ Programming 
                Program execution begins with the first C++ statement after the open brace 
                and continues through the listing, one statement at a time. 
                As you look through the program, you can see that spaces, tabs, and newlines 
                appear throughout the program. In fact, I place a newline after every state 
                ment in this program. These characters are collectively known as white space 
                because you can’t see them on the monitor. 
                You may add white space anywhere you like in your program to enhance 
                readability — except in the middle of a word: 
                 See wha 
                 t I mean? 
                Although C++ may ignore white space, it doesn’t ignore case. In fact, it’s case 
                sensitive to the point of obsession. The variable fullspeed and the variable 
                FullSpeed have nothing to do with each other. While the command int may 
                be understood completely, C++ has no idea what INT means. 
                Writing declarations
 
                The line int nCelsius; is a declaration statement. A declaration is a state 
                ment that defines a variable. A variable is a “holding tank” for a value of some 
                type. A variable contains a value, such as a number or a character. 
                The term variable stems from algebra formulae of the following type: 
                 x = 10 
                 y = 3 * x 
                In the second expression, y is set equal to 3 times x, but what is x? The vari 
                able x acts as a holding tank for a value. In this case, the value of x is 10, but 
                we could have just as well set the value of x to 20 or 30 or –1. The second for 
                mula makes sense no matter what the value of x. 
                In algebra, you’re allowed to begin with a statement, such as x = 10. In C++, 
                the programmer must first define the variable x before she can use it. 
                In C++, a variable has a type and a name. The variable defined on Line 11 is 
                called celsius and declared to hold an integer. (Why they couldn’t have just 
                said integer instead of int, I’ll never know. It’s just one of those things you learn 
                to live with.) 
           
 
                                       Chapter 1: Writing Your First C++ Program         25 
     The name of a variable has no particular significance to C++. A variable must 
     begin with the letters A through Z or a through z. All subsequent characters 
     must be a letter, a digit 0 through 9 or an underscore (_). Variable names can 
     be as long as you want to make them. 
     It’s convention that variable names begin with a lowercase letter. Each new 
     word within a variable begins with a capital letter, as in myVariable. 
     Try to make variable names short but descriptive. Avoid names such as x 
     because x has no particular meaning. A variable name such as lengthOfLine 
     Segment is much more descriptive. 
     Generating output
 
     The lines beginning with cout and cin are known as input/output statements, 
     often contracted to I/O statements. (Like all engineers, programmers love con 
     tractions and acronyms.) 
     The first I/O statement says output the phrase Enter the temperature in Celsius 
     to cout (pronounced “see-out”). cout is the name of the standard C++ output 
     device. In this case, the standard C++ output device is your monitor. 
     The next line is exactly the opposite. It says, in effect, Extract a value from the 
     C++ input device and store it in the integer variable celsius. The C++ input 
     device is normally the keyboard. What we’ve got here is the C++ analog to the 
     algebra formula x = 10 just mentioned. For the remainder of the program, the 

⌨️ 快捷键说明

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