How C++ Evolved: Procedural, Structured, and Object-Oriented Programming
How C++ Evolved: Procedural, Structured, and Object-Oriented Programming
Programs
The word program is used in two ways: to describe individual instructions, or source
code, created by the programmer, and to describe an entire piece of executable software.
A program can be defined as either a set of written instructions created by
a programmer or an executable piece of software.
Until recently, programs were thought of as a series of procedures that acted upon data. A
procedure, or function, is a set of specific instructions executed one after the other. The
data was quite separate from the procedures, and the trick in programming was to keep
track of which functions called which other functions, and what data was changed.
The principle idea behind structured programming is as simple as the idea of divide and
conquer. A computer program can be thought of as consisting of a set of tasks. Any task
that is too complex to be described simply would be broken down into a set of smaller
component tasks, until the tasks were sufficiently small and self-contained enough that
they were easily understood.
Type the first program directly into your editor, exactly as shown. Once you are certain it
is correct, save the file, compile it, link it, and run it. It will print the words Hello World
to your screen. Don't worry too much about how it works, this is really just to get you
comfortable with the development cycle. Every aspect of this program will be covered
over the next couple of days.
WARNING: The following listing contains line numbers on the left. These numbers are
for reference within the book. They should not be typed in to your editor. For example, in
line 1 of Listing 1.1, you should enter:
#include <iostream.h>
1: #include <iostream.h>
2: #include <conio.h>
3: void main()
4: {
5: int x = 5;
6: int y = 7;
7: cout "\n";
8: cout << x + y << " " << x * y;
9: cout "\n";
10: getch();
11: }
2. Program
OUTPUT
Hello there.
Here is 5: 5
The manipulator endl writes a new line to the screen.
Here is a very big number: 70000
Here is the sum of 8 and 5: 13
Here's a fraction: 0.625
And a very very big number: 4.9e+07
Don't forget to replace Jesse Liberty with your name...
Jesse Liberty is a C++ programmer!
Comments
When you are writing a program, it is always clear and self-evident what you are trying
to do. Funny thing, though--a month later, when you return to the program, it can be quite
confusing and unclear. I'm not sure how that confusion creeps into your program, but it
always does.
To fight the onset of confusion, and to help others understand your code, you'll want to
use comments. Comments are simply text that is ignored by the compiler, but that may
inform the reader of what you are doing at any particular point in your program.
Types of Comments
C++ comments come in two flavors: the double-slash (//) comment, and the slash-star
(/*) comment. The double-slash comment, which will be referred to as a C++-style
comment, tells the compiler to ignore everything that follows this comment, until the end
of the line.
The slash-star comment mark tells the compiler to ignore everything that follows until it
finds a star-slash (*/) comment mark. These marks will be referred to as C-style
comments. Every /* must be matched with a closing */.
As you might guess, C-style comments are used in the C language as well, but C++-style
comments are not part of the official definition of C.
Many C++ programmers use the C++-style comment most of the time, and reserve C-
style comments for blocking out large blocks of a program. You can include C++-style
comments within a block "commented out" by C-style comments; everything, including
the C++-style comments, is ignored between the C-style
What Is a Variable?
Your computer's memory can be viewed as a series of cubbyholes. Each cubbyhole is one
of many, many such holes all lined up. Each cubbyhole--or memory location--is
numbered sequentially. These numbers are known as memory addresses. A variable
reserves one or more cubbyholes in which you may store a value.
Your variable's name (for example, myVariable) is a label on one of these cubbyholes,
so that you can find it easily without knowing its actual memory address. Figure 3.1 is a
schematic representation of this idea. As you can see from the figure, myVariable starts
at memory address 103. Depending on the size of myVariable, it can take up one or more
memory addresses.
The C++ compiler recognizes some special characters for formatting. Table 3.2 shows
the most common ones. You put these into your code by typing the backslash (called the
escape character), followed by the character. Thus, to put a tab character into your code,
you would enter a single quotation mark, the slash, the letter t, and then a closing single
quotation mark:
This example declares a char variable (tabCharacter) and initializes it with the
character value \t, which is recognized as a tab. The special printing characters are used
when printing either to the screen or to a file or other output device.
New Term: An escape character changes the meaning of the character that
follows it. For example, normally the character n means the letter n, but when it is
preceded by the escape character (\) it means new line.
Enumerated Constants
Enumerated constants enable you to create new types and then to define variables of
those types whose values are restricted to a set of possible values. For example, you can
declare COLOR to be an enumeration, and you can define that there are five values for
COLOR: RED, BLUE, GREEN, WHITE, and BLACK.
The syntax for enumerated constants is to write the keyword enum, followed by the type
name, an open brace, each of the legal values separated by a comma, and finally a closing
brace and a semicolon. Here's an example:
2. It makes RED a symbolic constant with the value 0, BLUE a symbolic constant
with the value 1, GREEN a symbolic constant with the value 2, and so forth.
Every enumerated constant has an integer value. If you don't specify otherwise, the first
constant will have the value 0, and the rest will count up from there. Any one of the
constants can be initialized with a particular value, however, and those that are not
initialized will count upward from the ones before them. Thus, if you write
then RED will have the value 100; BLUE, the value 101; GREEN, the value 500; WHITE, the
value 501; and BLACK, the value 700.
You can define variables of type COLOR, but they can be assigned only one of the
enumerated values (in this case, RED, BLUE, GREEN, WHITE, or BLACK, or else 100, 101,
500, 501, or 700). You can assign any color value to your COLOR variable. In fact, you can
assign any integer value, even if it is not a legal color, although a good compiler will
issue a warning if you do. It is important to realize that enumerator variables actually are
of type unsigned int, and that the enumerated constants equate to integer variables. It
is, however, very convenient to be able to name these values when working with colors,
days of the week, or similar sets of values. Listing 3.7 presents a program that uses an
enumerated type.