Davis Stephen R.

Beginning Programming with C++ For Dummies


Скачать книгу

<< fahrenheitTemp;

      The first line reads a value into the variable celsiusTemp. The subsequent calculation converts this Celsius temperature to Fahrenheit, just as before; the third line outputs the result.

      C++ has several other advantages compared with other high-level languages. For one, C++ is universal. There is a C++ compiler for almost every computer in existence.

      In addition, C++ is efficient. The more tasks a high-level language tries to do automatically (to make your programming job easier), the less efficient the machine code generated tends to be. That doesn’t make much of a difference for a small program like most of those in this book, but it can make a big difference when manipulating large amounts of data, as when you’re moving pixels around on the screen, or when you want blazing real-time performance. It’s no accident that Unix and Windows are written in C++ and the Macintosh O/S is written in a language very similar to C++.

      The goal of the remaining chapters of this book is get you programming in C++. You won’t have to cram every detail of the C++ language into your head, but you’ll end up with enough of it under your belt to write some pretty awesome programs.

Chapter 2

      Installing Code::Blocks

       In This Chapter

      ▶ Reviewing the compilation process

      ▶ Installing the Code::Blocks development environment

      ▶ Testing your installation with a default program

      ▶ Reviewing the common installation errors

      In this chapter, you review what it takes to use C++ source code to create executable programs that you can run on Windows, Linux, or Macintosh computers. Then you install the Code::Blocks integrated development environment used in the remainder of the book – and build a default test program to check out your installation. If all is working, then by the time you reach the end of this chapter, you’ll be ready to start writing and building C++ programs of your own – with a little help, of course!

Reviewing the Compilation Process

      You need two programs to create your own C++ programs. First, you need a text editor that you can use to enter your C++ instructions. Any editor capable of generating straight ASCII text letters will work; I’ve written programs using the Notepad editor that comes with Windows. However, an editor that knows something about the syntax of C++ is preferable; it can save you a lot of typing, and sometimes highlight any mistakes you might make as you type, in much the same way that a spell checker highlights misspelled words in a word processor.

      The second program you need is a compiler that converts your C++ source statements into machine language that the computer can understand and interpret. This process of converting from source-code C++ statements to machine code is called building. Graphically, the process looks something like Figure 2-1.

      The process of building a program actually has two steps: The C++ compiler first converts your C++ source code statements into a machine executable format in a step known as compiling. It then combines the machine instructions from your program with instructions from a set of libraries that come standard with C++ in a second step known as linking to create a complete executable program.

      Figure 2-1: The C++ program development process.

      Most C++ compilers these days come in a software package known as an Integrated Development Environment or IDE. IDEs include the editor, the compiler, and several other useful development programs together in a common bundle. Not only does this save you from having to purchase the programs separately, but also offers productivity benefits by combining them into a single package:

      ✔ The editor can invoke the compiler quickly without making you switch back and forth manually.

      ✔ The editors in most IDEs provide quick and efficient means for finding and fixing coding errors.

      ✔ Some IDEs include visual programming tools that allow the programmer to draw common windows such as dialog boxes on the display.

      ✔ The IDE generates the C++ code necessary to display onscreen boxes automatically.

      

As nice as that sounds, the automatically generated code only displays the windows. A programmer still has to generate the real code that gets executed whenever the operator selects buttons within those windows.

      Invariably, these visual IDEs are tightly coupled into one particular operating system. For example, the popular Visual Studio is strongly tied into the .NET environment in Windows. It’s not possible to use Visual Studio without learning the .NET environment – and something about Windows – along with C++ (or one of the other .NET languages). In addition, the resulting programs only run in a .NET environment.

      In this book, you use a public-domain C++ IDE known as Code::Blocks. Versions of Code::Blocks exist for Windows, Linux, and Mac OS. Versions of Code::Blocks for these three operating systems (as well as a few others) are available for free download at www.codeblocks.org. The specific version used in the generation of the book is version 13.12. You can also download the Windows 13.12 binary at www.dummies.com/extras/beginningprogrammingcplusplus.

      You’ll use Code::Blocks to generate the programs in this book. These programs are known as console applications because they take input from – and display text back to – a console window. Okay, this isn’t as sexy as developing programs in onscreen windows, but staying with console applications lets you focus on C++ without being distracted by the requirements of a windowed environment. In addition, using console applications will allow the programs in the book to run the same on all environments that support Code::Blocks.

Installing Code::Blocks

      The following instructions take you through installing Code::Blocks on a Windows, Mac OS, or Linux computer. Jump to the section that applies to your operating system.

Windows installation

      This section provides detailed installation instructions for installing Code:: Blocks for Windows Version 13.12 available at www.dummies.com/extras/beginningprogrammingcplusplus.

      1. Download the file codeblocks-13.12mingw-setup.exe.

      That’s straightforward enough.

      2. Double-click the downloaded file to start the installation process.

      Depending on which version of Windows you’re using, you may a warning pop-up message like this one:

      Do you want to allow the following program from an unknown publisher to make changes to your computer?

      3. Select Allow.

      Setup unpacks the files it needs to start and run the Code::Blocks Setup Wizard. This process may take a minute. When it finishes, the startup window shown in Figure 2-2 appears.

      Figure 2-2: The Code:: Blocks Setup Wizard guides you through the installation process.

      4. Close any other programs that may be running and then select Next.

      The Setup Wizard displays the generic End User License Agreement (EULA). There’s nothing much here to get excited about.

      5. Select I Agree.

      The Setup Wizard then displays a list of the components that you may choose to install. See Figure 2-3. The defaults are okay.