Cover | Table of Contents | Colophon
int total; // Total number accounts
total. You can let the compiler decide what
particular bytes of memory to use; that's a minor
bookkeeping detail you don't need to worry about.
total is a simple
variable
.
It can hold only one integer and describe only one total. A series of
integers can be organized into an array. Again, C++ will handle the
details, imposing that organization on the
computer's memory.
int balance[100]; // Balance (in cents) for all 100 accounts
struct rectangle {
int width; // Width of rectangle in pixels
int height; // Height of rectangle in pixels
color_type color; // Color of the rectangle
fill_type fill; // Fill pattern
};
?LSTUIT User is a twit
http://www.oreilly.com/catalog/cplus2.
hello program.
% mkdir hello % cd hello
man command. (Unix uses man as
an abbreviation for "manual.") To
get information about a particular subject, use the command:
man subject
iostream package, you would type:
man iostream
man -k keyword
man page with the
word "output" in its title, use the
command:
man -k output
hello
program and execute it.
/* and ends with */. This type
of comment can span multiple lines as shown:
/* This is a single-line comment. */ /* * This is a multiline comment. */
// and goes
to the end of the line:
// This is another form of comment. // The // must begin each line that is to be a comment.
/* */ comment style is that
you can easily span multiple lines, whereas with the //
style you have to keep putting the // on
each line. The disadvantage of /* */ is that
forgetting a */ can really screw up your code.
(Remember this because it's the answer to one of the
questions later in the book.)
/* */
style comments for big, multiline comments, and the
// style is reserved for comments that take up
only a single line.
p, q, and r:
int p,q,r;
int account_number; int balance_owed;
balance_owed in dollars or cents? It would be much
better if we added a comment after each declaration explaining what
we are doing.
int account_number; // Index for account table int balance_owed; // Total owed us (in pennies)
source_ptr,
current_index). All uppercase is reserved for
constants (e.g., MAX_ITEMS,
SCREEN_WIDTH). This convention is the classic
convention followed by most C and C++ programs.
RecordsInFile). Sometimes they use the
capitalization of the first letter to indicate information about the
variable. For example, recordsInFile might be used
to denote a local variable while RecordsInFile
would denote a global variable. (See Chapter 9 for information about local and global
variables.) You should be careful when making up rules for your
variables because the more complex the rules, the more likely someone
will violate them or get confused.
gp, ground_pt,
gnd_pt, g_pnt, and many others.
On the other hand, there's only one full spelling:
ground_point.
#define constants are being replaced with new-style
while (! done) {
std::cout << "Processing\n";
next_entry( );
}
if (total <= 0) {
std::cout << "You owe nothing\n";
total = 0;
} else {
std::cout << "You owe " << total << " dollars\n";
all_totals = all_totals + total;
}
while (! done)
{
std::cout << "Processing\n";
next_entry( );
}
if (total <= 0)
{
std::cout << "You owe nothing\n";
total = 0;
}
else
{
std::cout << "You owe " << total << " dollars\n";
all_totals = all_totals + total;
}
// poor programming practice temp = box_x1; box_x1 = box_x2; box_x2 = temp; temp = box_y1; box_y1 = box_y2; box_y2 = temp;
/* * Swap the two corners */ /* Swap X coordinate */ temp = box_x1; box_x1 = box_x2; box_x2 = temp; /* Swap Y coordinate */ temp = box_y1; box_y1 = box_y2; box_y2 = temp;
http://www.oualline.com.
/******************************************************** * Heading comments * ********************************************************/ data declarations
/******************************************************** * Heading comments * ********************************************************/ data declarations int main( ) { executable statements return(0); }
main. The name
main is special, because it is the first function
called. Any other functions are called directly or indirectly from
main. The function main begins
with:
int main( )
{
return(0); }
return(0); is used to tell the operating
system that the program exited normally (status=0). A nonzero status
indicates an error—the bigger the return value, the more severe
the error. Typically 1 is used for most simple errors, such as a
missing file or bad command-line syntax.
#include <iostream>
main routine contains the instruction: std::cout << "Hello World\n";
\n
tells C++
to write out a newline character. C++ uses a
semicolon to
end a statement in much the same way we use a period to end a
sentence. Unlike line-oriented languages such as BASIC, the end of a
line does not end a statement. The sentences in this book can span
several lines—the end of a line is treated as a space
separating words. C++ works the same way. A single statement can span
several lines. Similarly, you can put several sentences on the same
line, just as you can put several C++ statements on the same line.
However, most of the time your program is more readable if each
statement starts on a separate line.
|
Operator
|
Meaning
|
|---|---|
*
|
Multiply
|
/
|
Divide
|
+
|
Add
|
-
|
Subtract
|
%
|
Modulus (remainder after division)
|
(1 + 2) * 4
1 + 2 * 4
std::cout is used to
output data to the console. We'll learn what a
object is later in Chapter 13, but
for now all we have to know is that the
operator
<<
tells C++
what to output.std::cout << "Hello World\n";
"Hello
World\n" and write it to the
console. Multiple << operators may be used
together. For example, both the following lines output the same
message:
std::cout << "Hello World\n"; std::cout << "Hello " << "World\n";
std::cout << "Half of " << 64 << " is " << (64 / 2) << "\n";
Half of 64 is 32
// Problem code std::cout << "Half of" << 64 << "is" << (64 / 2) << "\n";
Half of64is32
average // average of all grades pi // pi to 6 decimal places number_of_students // number of students in this class
3rd_entry // Begins with a number all$done // Contains a "$" the end // Contains a space int // Reserved word
total // total number of items in current entry totals // total of all entries
entry_total // total number of items in current entry all_total // total of all entries
answer can be: int answer; // the result of our expression
answer. The semicolon is used to
indicate the statement end, and the comment is used to define this
variable for the programmer.
type
name; // comment
,
float, etc.) Name is any
valid variable name. The comment explains what the variable is and
what it will be used for. Variable declarations come just before the
main( ) line at the top of a program. (In Chapter 9 you will see how local
variables may be declared elsewhere.)
int name; // comment
int zip; // zip code for current address ......... zip = 92126;
int answer; // Result of a simple computation
answer = (1 + 2) * 4;
answer on the left side of the equal
sign (=) is assigned the value of the expression (1 + 2) *
4 on the right side. The semicolon ends the statement.
variable = expression;
term
is used to store an integer value that is used in two later
expressions. Variables, like expressions, can be output using the
output operator <<, so we use this operator
to check the results.
#include <iostream>
int term; // term used in two expressions
int main( )
{
term = 3 * 5;
std::cout << "Twice " << term << " is " << 2*term << "\n";
std::cout << "Three times " << term << " is " << 3*term << "\n";
return (0);
}
float variable; // comment
std::cout. For example:
std::cout << "The answer is " << (1.0 / 3.0) << "\n";