The introduction to On To C++ follows. Additional
information about this book, along with access to software, is available
via http://www.ai.mit.edu/people/phw/Books/
How On To C++ Teaches You C++
The purpose of this book is to help you learn the essentials of
C++ programming. In this section, you learn why you should
know C++ and how this book is organized.
The C++ programming language is related to
C. Because ++ is C's increment operator, and because the
developers of C++ viewed C++ as an incremental augmentation of C,
rather than as a completely different language, they decided to use C and
++ in C++'s name.
In the vernacular of programming, an object is a packet of
information stored in a chunk of computer memory. Every object
is associated with a data type, and the data type determines what
can be done to an object. All programming languages have
built-in data types, such as the integer data type and the
character data type.
An object-oriented programming language encourages you to
design programs around data types and data-type hierarchies that you
define yourself. Typically, you define data types and data-type
hierarchies so that you can describe individual nails,
horseshoes, horses, kingdoms, or whatever else happens to come up
naturally in your application.
In contrast, procedure-oriented programming languages encourage you
to think in terms of procedures, instead of in terms of data types and
data-type hierarchies.
In this book, you learn more about what object-oriented means and
why many programmers prefer object-oriented languages. For now, it
suffices to know that C++ is an object-oriented programming language,
whereas most other programming languages are procedure-oriented
programming languages.
C++
became a popular object-oriented programming language because its parent
language, C, was already popular. C, in turn, became popular by virtue
of attractive characteristics, such as the following:
- C is easy to learn.
- C programs are fast.
- C programs are concise.
- C compilers---programs that translate C programs into
machine instructions---are usually fast and concise.
- C compilers and C programs run on all sorts of computers,
from small personal computers to huge supercomputers.
- Unix, a popular operating system, happens to be written in C.
There are two principal reasons to learn C++:
- The productivity of C++ programmers
generally exceeds the productivity of C programmers. Hence, C++
programmers are in demand.
- The supply of powerful off-the-shelf C++ software modules,
both free and for sale, is increasing rapidly. The supply of
off-the-shelf C modules, most of which you can incorporate into
C++ programs, is already huge.
Also, because C++ is the most widely used object-oriented programming
language, you often hear programmers debate the merits of other
object-oriented languages in terms of advantages and disadvantages relative to
C++.
Four principles determined this introductory book's organization and style:
- The book should get you up and running in the language quickly.
- The book should answer your basic questions explicitly.
- The book should encourage you to develop a personal library of
solutions to standard programming problems.
- The book should deepen your understanding of the art of good
programming practice.
To get you up and running in C++ quickly, the sections in this
book generally supply you with the most useful approach to each
programming need, be it to display characters on your screen, to
define a new function, or to read information from a file.
To answer your basic questions explicitly, this book is divided
into parts that generally focus on one issue, which is plainly
announced in the title of the section. Accordingly, you see
titles such as the following:
- How To Write Arithmetic Expressions
- How To Define Simple Functions
- How To Create Classes and Objects
- How To Benefit from Data Abstraction
- How To Design Class Hierarchies
- How To Organize a Multiple-File Program
To encourage you to develop a personal library of solutions to
standard programming problems, this book introduces many useful,
productivity-increasing, general-purpose, templatelike
patterns---sometimes called cliches by experienced
programmers---that you can fill in to achieve particular-purpose
goals.
Cliches are introduced because learning to program involves more than
learning to use programming-language primitives, just as learning to
speak a human language involves more than learning to use vocabulary words.
To deepen your understanding of the art of good programming
practice, this book emphasizes the value of such ideas as data
abstraction and procedure abstraction, along with
principles such as
the explicit-representation principle,
the no-duplication principle,
the local-view principle,
the look-up principle,
the need-to-know principle,
and the keep-it-simple principle.
In this book, single-idea segments, analogous to slides, are arranged in
sections that are analogous to slide shows. The segments come in several
varieties: basic segments explain essential ideas; sidetrip
segments introduce interesting, but skippable, ideas; practice segments
provide opportunities to experiment with new ideas; and highlights
segments summarize important points.
Finally, the book develops a simple, yet realistic C++ program, which you
see in many versions as your understanding of the language increases. In
its ultimate version, the program reads information from a file describing
a railroad train, computes the load-bearing volume of each box car and tank
car using formulas drawn from descriptions of boxes and cylinders, and
displays a car-by-car report. Programs similar to the one developed can
help you to manage a railroad, either model or real.
Highlights
- C++ is an object-oriented programming language.
Object-oriented programming languages emphasize user-defined data
types and data-type
hierarchies, rather than computations to perform.
- C++ descends from C, an already-popular programming
language; thus, heredity is the most conspicuous reason why C++ itself
enjoys considerable popularity.
- This book gets you up and running in C++ quickly; it
answers your basic questions explicitly; it equips you with
program patterns that you can adapt to your own purposes; and it
deepens your understanding of the art of good programming
practice.