Beginning Programming with C++ For Dummies. Davis Stephen R.

Чтение книги онлайн.

Читать онлайн книгу Beginning Programming with C++ For Dummies - Davis Stephen R. страница 6

Beginning Programming with C++ For Dummies - Davis Stephen R. For Dummies

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

This is the same information previously shown in binary form.

      <main>: push ebp

      <main+1>: mov ebp, esp

      <main+3>: and esp, 0xfffffff0

      <main+6>: sub esp, 0x20

      <main+9>: call 0x40530c <__main>

      <main+14>: movl [esp+0x04],0x477024

      <main+22>: movl [esp],0x475f80

      <main+29>: call 0x469fac <operator<<>

      <main+34>: lea eax,[esp+0x14]

      <main+38>: mov [esp+0x04],eax

      This is still not very intelligible, but it’s clearly a lot better than just a bunch of ones and zeros. Don’t worry – you won’t have to write any assembly-language code in this book either.

      

The computer doesn’t actually ever execute the assembly-language instructions. It executes the machine instructions that result from converting the assembly instructions.

High-level languages

      Assembly language might be easier to remember than machine language, but there’s still a lot of distance between an algorithm like the tire-changing algorithm and a sequence of MOVE and ADD instructions. In the 1950s, people started devising progressively more expressive languages that could be automatically converted into machine language by a program called a compiler. These were called high-level languages because they were written at a higher level of abstraction than assembly language.

      One of the first of these languages was COBOL (Common Business-Oriented Language). The idea behind COBOL was to allow the programmer to write commands that were as much like English sentences as possible. Suddenly programmers were writing sentences like the following to convert temperature from Celsius to Fahrenheit (believe it or not, this is exactly what the machine and assembly-language snippets shown earlier do):

      INPUT CELSIUS_TEMP

      SET FAHRENHEIT_TEMP TO CELSIUS_TEMP * 9/5 + 32

      WRITE FAHRENHEIT_TEMP

      The first line of this program reads a number from the keyboard or a file and stores it into the variable CELSIUS_TEMP. The next line multiplies this number by 9/5 and adds 32 to the result to calculate the equivalent temperature in degrees Fahrenheit. The program stores this result in a variable called FAHRENHEIT_TEMP. The last line of the program writes this converted value to the display.

      People continued to create different programming languages, each with its own strengths and weaknesses. Some languages, like COBOL, were very wordy but easy to read. Other languages such as database languages or the languages used to create interactive web pages, were designed for very specific jobs. These languages include powerful constructs designed to handle specific problem areas.

The C++ language

      C++ (pronounced “C plus plus,” by the way) is a symbolically oriented high-level language. C++ started out life as simply C in the 1970s at Bell Labs. A couple of guys were working on a new idea for an operating system known as Unix (the predecessor to Linux and Mac OS and still used across industry and academia today). The original C language created at Bell Labs in the 1970s was modified slightly and adopted as a worldwide ISO standard in early 1989. C++ was created as an extension to the basic C language mostly by adding the features that I discuss in Parts V and VI of this book.

      When I say that C++ is symbolic, I mean that it isn’t very wordy; it uses symbols instead of the long words in languages like COBOL. However, C++ is easy to read once you’re accustomed to what the symbols mean. The same Celsius-to-Fahrenheit conversion code shown in COBOL earlier appears as follows in C++:

      cin >> celsiusTemp;

      fahrenheitTemp = celsiusTemp * 9 / 5 + 32;

      cout << 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.

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