Chap 01
Chap 01
Preliminaries
Listing 1.1 shows our first C++ program, which when run,
simply outputs the message Hello World.
Listing 1.2
1 #include <iostream.h>
Annotation
1 This line uses the preprocessor directive #include to
include the contents of the header file iostream.h in the
program. Iostream.h is a standard C++ header file and
contains definitions for input and output.
2 This line defines a function called main. A function may
have zero or more parameters; these always appear
after the function name, between a pair of brackets. The
word void appearing between the brackets indicates that
main has no parameters. A function may also have a
return type; this always appears before the function
name. The return type for main is int (i.e., an integer
number). All C++ programs must have exactly one main
function. Program execution always begins from main.
3 This brace marks the beginning of the body of main.
4 This line is a statement. A statement is a computation
step which may produce a value. The end of a statement
is always marked with a semicolon (;). This statement
causes the string "Hello World\n" to be sent to the cout
output stream. A string is any sequence of characters
enclosed in double-quotes. The last character in this
string (\n) is a newline character which is similar to a
carriage return on a type writer. A stream is an object
which performs input or output. Cout is the standard
output stream in C++ (standard output usually means
your computer monitor screen). The symbol << is an
output operator which takes an output stream as its left
operand and an expression as its right operand, and
causes the value of the latter to be sent to the former. In
this case, the effect is that the string "Hello World\n" is sent
to cout, causing it to be printed on the computer monitor
screen.
Dialog 1.2
1 $ CC hello.cc
2 $ a.out
3 Hello World
4 $
Annotation
1 The command for invoking the AT&T C++ translator in a
UNIX environment is CC. The argument to this command
(hello.cc) is the name of the file which contains the
program. As a convention, the file name should end in .c,
.C, or .cc. (This ending may be different in other systems.)
2 The result of compilation is an executable file which is by
default named a.out. To run the program, we just use a.out
as a command.
3 This is the output produced by the program.
4 The return of the system prompt indicates that the
program has completed its execution.
The CC command accepts a variety of useful options. An
option appears as -name, where name is the name of the
option (usually a single letter). Some options take
arguments. For example, the output option (-o) allows you to
specify a name for the executable file produced by the
compiler instead of a.out. Dialog 1.3 illustrates the use of this
option by specifying hello as the name of the executable file.
Dialog 1.4
1 $ CC hello.cc -o hello
2 $ hello
3 Hello World
4 $
Execut-
LINKER able
Listing 1.6
1 #include <iostream.h>
6 workDays = 5;
7 workHours = 7.5;
8 payRate = 38.55;
9 weeklyPay = workDays * workHours * payRate;
10 cout << "Weekly Pay = ";
11 cout << weeklyPay;
12 cout << '\n';
13 }
Annotation
4 This line defines an int (integer) variable called workDays,
which will represent the number of working days in a
week. As a general rule, a variable is defined by
specifying its type first, followed by the variable name,
followed by a semicolon.
5 This line defines three float (real) variables which,
respectively, represent the work hours per day, the
hourly pay rate, and the weekly pay. As illustrated by this
line, multiple variables of the same type can be defined
at once by separating them with commas.
6 This line is an assignment statement. It assigns the
value 5 to the variable workDays. Therefore, after this
statement is executed, workDays denotes the value 5.
8 C++ Programming Copyright © 1998 Pragmatix Software
7 This line assigns the value 7.5 to the variable workHours.
8 This line assigns the value 38.55 to the variable payRate.
9 This line calculates the weekly pay as the product of
workDays, workHours, and payRate (* is the multiplication
operator). The resulting value is stored in weeklyPay.
10-12 These lines output three items in sequence: the string
"Weekly Pay = ", the value of the variable weeklyPay, and a
newline character.
When run, the program will produce the following output:
Weekly Pay = 1445.625
Listing 1.9
1 #include <iostream.h>
Listing 1.11
1 #include <iostream.h>
Annotation
7 This line outputs the prompt What is the hourly pay rate? to
seek user input.
8 This line reads the input value typed by the user and
copies it to payRate. The input operator >> takes an input
stream as its left operand (cin is the standard C++ input
stream which corresponds to data entered via the
keyboard) and a variable (to which the input data is
copied) as its right operand.
9-13 The rest of the program is as before.
When run, the program will produce the following output
(user input appears in bold):
What is the hourly pay rate? 33.55
Weekly Pay = 1258.125
Both << and >> return their left operand as their result,
enabling multiple input or multiple output operations to be
combined into one statement. This is illustrated by Listing
1.12 which now allows the input of both the daily work hours
and the hourly pay rate.
6 cout << "What are the work hours and the hourly pay rate? ";
7 cin >> workHours >> payRate;
Annotation
7 This line reads two input values typed by the user and
copies them to workHours and payRate, respectively. The
two values should be separated by white space (i.e., one
or more space or tab characters). This statement is
equivalent to:
(cin >> workHours) >> payRate;
Listing 1.16
1 #include <iostream.h>
1 1 0 1 0 0 0 1
Bit
92 // decimal
0134 // equivalent octal
0x5C // equivalent hexadecimal
Octal numbers use the base 8, and can therefore only use
the digits 0-7. Hexadecimal numbers use the base 16, and
therefore use the letter A-F (or a-f) to represent, respectively,
10-15. Octal and hexadecimal numbers are calculated as
follows:
0134 = 1 × 82 + 3 × 81 + 4 × 80 = 64 + 24 + 4 = 92
0x5C = 5 × 161 + 12 × 160 = 80 + 12 = 92
Figure 1.7 illustrates how the string variable str and the string
"HELLO" might appear in memory.