Prologue from Introduction to C++
To return to my main page, click here
Introduction to C++: Chapter 1
Prologue
Introduction to Programming
"Begin at the beginning, and go on till you
come to the end: then stop." This method of
telling a story is as good today as it was when
the King of Hearts prescribed it to the White
Rabbit in Alice in Wonderland. In this book, we
must begin with you, the reader, since my job
is to explain a technical subject to you. It
might appear that I'm at a severe disadvantage;
after all, I've never met you.
Nevertheless, I can make some pretty good
guesses about you. You almost certainly own a
computer and know how to use its most common
application, word processing. If you use the
computer in business, you probably also have an
acquaintance with spreadsheets and perhaps some
database experience as well. Now you have
decided to learn how to program the computer
yourself rather than relying completely on
programs written by others. On the other hand,
you might be a student using this book as a
text in an introductory course on programming.
In that case, you'll be happy to know that this
book isn't written in the dry, overly academic
style employed by many textbook writers. I hope
that you will enjoy reading it as much as my
"test readers" have.
Whether you are using this book on your own
or in school, there are many good reasons to
learn how to program. You may have a problem
that hasn't been solved by commercial software;
you may want a better understanding of how
commercial programs function so you can figure
out how to get around their shortcomings and
peculiarities; or perhaps you're just curious
about how computers perform their seemingly
magical feats. Whatever the initial reason, I
hope you come to appreciate the great creative
possibilities opened up by this most ubiquitous
of modern inventions.
If you already know how to program in another
language and are using this book to learn how
to program in C++, you'll have a head start; I
hope that you'll learn enough to make it worth
your while to wade through some material you
already know. In any event, before we begin, we
should agree on definitions for some fundamental
words in the computing field.
Many of the technical words used in this book
are in the Glossary at the end of the book; it
is also very helpful to have a good technical
dictionary of computer terms, as well as a good
general dictionary of English.
Of course, you may not be able to remember
all of these technical definitions the first
time through. If you can't recall the exact
meaning of one of these terms, just look up the
word or phrase in the index; it will direct you
to the page where the definition is stated. You
could also look in the Glossary at the end of
the book, which lists key technical terms in
alphabetical order.
To give you some idea of what to expect while
you're reading, I have provided a list of
objectives at the beginning of each chapter
after this one. For the moment, let's start
with definitions of some programming terms.
Definitions
Hardware refers to the physical components of a
computer, the ones you can touch. Examples
include the keyboard, the monitor, and the
printer.
Software refers to the other, nonphysical
components of a computer, the ones you cannot
touch. If you can install it on your hard disk,
it's software. Examples include a spreadsheet,
a word processor, and a database program.
An algorithm is a set of precisely defined
steps to calculate an answer to a problem or
set of problems, which is guaranteed to arrive
at such an answer eventually. As this implies,
a set of steps that might never end is not an
algorithm.
Programming is the art and science of solving
problems by the following procedure:
1. Find or invent a general solution to a
class of problems.
2. Express this solution as an algorithm or
set of algorithms.
3. Translate the algorithm(s) into terms so
simple that a stupid machine like a
computer can follow them to calculate the
specific answer for any specific problem in
the class.
An application program is a program that
actually accomplishes some useful or
interesting task. Examples include inventory
control, payroll, and game programs.
Data are the pieces of information that are
operated on by programs. The singular of "data"
is "datum"; however, the word "data" is
commonly used as both singular and plural.
Source code is a program in a form suitable for
reading and writing by a human being.
An executable program (or just an executable,
for short) is a program in a form suitable for
running on a computer.
Object code is a program in a form suitable for
incorporation into an executable program.
Compilation is the process of translating
source code into object code. Almost all of the
software on your computer was created by this
process.
A compiler is a program that performs
compilation as defined above.
Compile time means "while the compiler is
compiling the source code of a program".
How to Write a Program
Now you have a definition of programming.
Unfortunately, however, this doesn't tell you
how to write a program. The process of solving
a problem by programming in C++ follows these
1
steps:
Problem: After discussions between the user
and the programmer, the programmer
defines the problem precisely.
Algorithms:The programmer finds or creates
algorithms that will solve the
problem.
C++: The programmer implements these
algorithms as source code in C++.
Executable:The programmer runs the C++
compiler, which must already be
present on the programmer's machine,
to translate the source code into an
executable program.
Hardware: The user runs the resulting
executable program on a computer.
These steps advance from the most abstract to
the most concrete, which is perfectly
appropriate for an experienced C++ programmer.
However, if you're using this book to learn how
to program in C++, obviously you're not an
experienced C++ programmer, so before you can
follow this path to solving a problem you're
going to need a fairly thorough grounding in
all of these steps. It's not really feasible to
discuss each step exhaustively before going to
2
the next one, so I've created a little "step
indicator", which you'll see on each page of
the text, with the currently active step shown
in bold. For example, when we're discussing
algorithms, the indicator will display
Algorithms in bold.
The five steps of this indicator correspond
to the five steps in problem solving just
defined. I hope this device will make it easier
for you to follow the sometimes tortuous path
to programming knowledge.
The steps for solving a problem via
programming might sound reasonable in the
abstract, but that doesn't mean that you can
follow them easily without practice. Assuming
that you already have a pretty good idea of
what the problem is that you're trying to
solve, the Algorithms step is likely to be the
biggest stumbling block. Therefore, it might be
very helpful to go into that step in a bit more
detail.
Baby Steps
If we already understand the problem we're
going to solve, the next step is to figure out
a plan of attack, which we will then break down
into small enough steps to be expressed in C++.
This is called stepwise refinement, since we
start out with a "coarse" solution and refine
it until the steps are within the capability of
the C++ language. For a complex problem, this
may take several intermediate steps, but let's
start out with a simple example. Say that we
want to know how much older one person is than
another. We might start with the following
general outline:
1. Get ages from user.
2. Calculate difference of ages.
3. Print the result.
This can in turn be broken down further as
follows:
1. Get ages from user.
a. Ask user for first age.
b. Ask user for second age.
2. Subtract second age from first age.
3. Print result.
This looks okay, except that if the first
person is younger than the second one, then the
result will be negative. That may be
acceptable. If so, we're just about done, since
these steps are simple enough for us to
translate them into C++ fairly directly.
Otherwise, we'll have to modify our program to
do something different depending on which age
is higher. For example,
1. Get ages from user.
a. Ask user for first age.
b. Ask user for second age.
2. Compute difference of ages.
a. If first age is greater than second,
subtract second age from first age.
b. Otherwise, subtract first age from
second age.
3. Print result.
You've probably noticed that this is a much
more detailed description than would be needed
to tell a human being what you want to do.
That's because the computer is extremely stupid
and literal: it does only what you tell it to
do, not what you meant to tell it to do.
Unfortunately, it's very easy to get one of the
steps wrong, especially in a complex program.
In that case, the computer will do something
ridiculous, and you'll have to figure out what
you did wrong. This "debugging", as it's
called, is one of the hardest parts of
programming. Actually, it shouldn't be too
difficult to understand why that is the case.
After all, you're looking for a mistake you've
made yourself. If you knew exactly what you
were doing, you wouldn't have made the mistake
in the first place.
I hope that this brief discussion has made
the process of programming a little less
mysterious. In the final analysis, it's
3
basically just logical thinking.
On with the Show
Now that you have some idea how programming
works, it's time to see exactly how the
computer actually performs the steps in a
program, which is the topic of Chapter 2.
_______________________________________________
1. This description is actually a bit
oversimplified. We'll examine another step
in the process of making an executable
program, called linking, in a later chapter.
2. The steps don't actually occur in such an
exact progression anyway. In real life most
programs are written in an incremental
process as assumptions are changed and
errors are found and corrected.
3. Of course, the word just in this sentence is
a bit misleading; taking logical thinking
for granted is a sure recipe for trouble.
To return to my main page, click here