21-01-2013, 01:15 PM
An Overview of C++
1An Overview.pdf (Size: 203.86 KB / Downloads: 112)
Objectives
• To recognize the components of a typical C++ program.
• To understand the predefined data types in C++ and how they can be used to store
information in a program.
• To become familiar with the facilities in the simplified and standard input/output (I/O)
libraries for reading input data and displaying results.
• To understand the structure of expressions in C++ and how to use the common
operators to express calculations.
• To recognize the statement forms if, switch, while, and for and be able to use them
in simple programs.
• To appreciate the importance of decomposing a program into individual functions and
understand how those functions operate.
• To be able to write simple programs that integrate the various control facilities
presented in this chapter.
What is C++?
In the early days of computing, programs were written in machine language, which
consists of the primitive instructions that can be executed directly by the machine.
Machine-language programs are difficult to understand, mostly because the structure of
machine language reflects the design of the hardware rather than the needs of
programmers. In the mid-1950s, a group of programmers under the direction of John
Backus at IBM had an idea that profoundly changed the nature of computing. Would it
be possible, they wondered, to write programs that resembled the mathematical formulas
they were trying to compute and have the computer itself translate those formulas into
machine language? In 1955, this team produced the initial version of Fortran (whose
name is an abbreviation of formula translation), which was the first example of a higherlevel
programming language. Since that time, many new programming languages have
been invented, as shown in the evolutionary diagram in Figure 1-1.
The object-oriented paradigm
Over the last decade or so, computer science and programming have gone through
something of a revolution. Like most revolutions—whether political upheavals or the
conceptual restructurings that Thomas Kuhn describes in his 1962 book The Structure of
Scientific Revolutions—this change has been driven by the emergence of an idea that
challenges an existing orthodoxy. Initially, the two ideas compete. For a while, the old
order maintains its dominance. Over time, however, the strength and popularity of the
new idea grows, until it begins to displace the older idea in what Kuhn calls a paradigm
shift. In programming, the old order is represented by the procedural paradigm, in
which programs consist of a collection of procedures and functions that operate on data.
The challenger is the object-oriented paradigm, in which programs are viewed instead
as a collection of data objects that exhibit particular behavior.
The compilation process
When you write a program in C++, your first step is to create a file that contains the text
of the program, which is called a source file. Before you can run your program, you
need to translate the source file into an executable form. The first step in that process is
to invoke a program called a compiler, which translates the source file into an object file
containing the corresponding machine-language instructions. This object file is then
combined with other object files to produce an executable file that can be run on the
system. The other object files typically include predefined object files, called libraries,
that contain the machine-language instructions for various operations commonly required
by programs. The process of combining all the individual object files into an executable
file is called linking. The process is illustrated by the diagram shown in Figure 1-2.
Comments
Much of the text in Figure 1-3 consists of English-language comments. A comment is
text that is ignored by the compiler but which nonetheless conveys information to other
programmers. A comment consists of text enclosed between the markers /* and */ and
may continue over several lines. Alternatively, a single-line comment is begun by the
marker // and continues until the end of the line. The powertab.cpp program includes a
comment at the beginning that describes the operation of the program as a whole, one
before the definition of the RaiseIntToPower function that describes what it does, and a
couple of one-line comments that act very much like section headings in English text.