Starting+Out+With+C+++From+Control+Structures+through+Objects+(6th+Ed)+Tony+Gaddis+(P1)

Page 41

Bismillah hiir Rehman nir Raheem-----------------------------Assalat o Wasalam o Alika Ya RasoolALLAH 10

Chapter 1 (ntroduction to Computers and Programming

c++ is popular nOt on ly because of its

us uf

i

mixture of low- and high-level features, bur also because of its portability. This means th at a C++ program can be written on one type of co mputer and then fun o n many other types of systems. Th is usually requires the pcogram to be recompiled on each type of system, but rhe progra m itself may need little o r no change.

Yo

NOTE: Programs wrinen fo r specific g raphical env ironments often req uire significant ch anges when moved to a differenr type of system . Examples of such graphical env ironments are Windows, the X-Window System , and the Mac OS X operating system.

Source Code, Object Code, and Executable Code

Ri

az

When a C++ program is written, it must be typed inro rhe computer and saved to a fil e. A text editor, which is similar to a word processing program, is used for thi s task. The stare路 ments written by the programmer a rc called source code, a nd the file thc}' arc saved in is called the source file.

Ha

ss

an

Afrer the source code is saved to a file, the process of translating it to machine language can begi n. During the first phase of this process, a program ca lled the preprocessor reads the source code. T he preprocessor searches fo r special lines that begi n with rhe t symbol. These lines contain commands that cause the preprocessor to modify the source code in some way. During the next phase the compiler steps through the preprocessed SOll rce codc, translating each source code inst.ruction into the appropriate machine language instruction. This process willll ncover any syntax errors that may be in the program. Syn路 la x errors are illega l uses of key words, operators, punctuation, and other language elements. If the progra m IS free of syn tax errors, the co mp iler stores the transla ted machine language instructions, which are called object code, in an object file.

uh a

m

m

ad

Although an object file contains machine language instructions, it is nO( a complete pro路 gram. Here is why; C++ is conveniently equipped with a library of prewritten code for performing common operations or sometimes-diffi cult tasks. For example, the library con tains hardware-specific code for displaying messages on the screen and reading input from the keyboard. It also provides routines for mathematical functions, sllch as calculati ng the square root of a number. This collection of code, ca lled the run-time library, is extensive. Programs almost al wa ys use some part of it. When rhe compil er generates an object fi le, however, it does not include machine code for any run-time library routi nes [he programmer might have used. During the last phase of the translation process, another program called th e linker combines the object file with the necessary library romines. Once the linker has fimshed with this step, an executable file is created. The execura ble file contains machine language instructions, or executable code, a nd is rcady to rlln on the computer.

M

Figure 1-5 illustrates the process of translating a C++ source file into an executable file.

The cnrire process of invoking the preprocessor, compiler, and linker can be initiated wirh a single action. For example, o n a Linux system, the following command causes the C++ program named hello _cpp CO be preprocessed, compiled, and link ed. T he executa ble code is stored in a file named hello. g++

-0

he l lo hello . cpp

To Read Online & Download: WWW.ISSUU.COM/SHEIKHUHASSAN


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.