C++ Weekend Crash Course Chapter04
C++ Weekend Crash Course Chapter04
SESSION
4
C++ Instructions
Session Checklist
✔ Reviewing the Conversion program from Sessions 2 and 3
✔ Understanding the parts of a C++ program
✔ Introducing common C++ commands
I
n Sessions 2 and 3, you were asked to enter a C++ program by rote. The idea
was to learn the C++ environment (whichever environment you chose) rather
than learn how to program. This session analyzes the Conversion.cpp program.
You will see exactly what each part of the program does and how each part con-
tributes to the overall solution.
The Program
Listing 4-1 is the Conversion.cpp program (again) except that it is annotated by
commenting features which I describe in the remainder of the lesson.
4689-9 ch04.f.qc 3/7/00 9:20 PM Page 38
38 Friday Evening
There are several aspects to this program that you have to take
on faith, at least for now. Be patient. Every structure found in
this program is explained in time.
Note
Listing 4-1
Conversion.cpp
//
// Conversion - convert temperature from Celsius
// degree units into Fahrenheit degree
// units:
// Fahrenheit = Celsius * (212 - 32)/100 + 32
//
#include <stdio.h> // framework
#include <iostream.h>
int main(int nNumberofArgs, char* pszArgs[])
{
// here is our first statement
// it’s a declaration
int nCelsius;
Session 4
The C++ Program Explained
Our Human Program back in Session 1 consisted of a sequence of commands.
Similarly, a C++ program consists of a sequence of C++ statements that the com-
puter processes in order. These statements fall into a series of broad types. Each
of these is described here.
Every program written in C++ begins with the same basic framework:
#include <stdio.h>
#include <iostream.h>
int main(int nNumberofArgs, char* pzArgs[])
{
...your code goes here...
return 0;
}
You don’t need to worry too much about the details of this framework — these
details will come later — but you should have some idea of what they’re about. The
first two lines are called include statements because they cause the contents of
the named file to be included at that point in the program. We’ll just consider
them magic at this point.
The next statement in every framework is the int main(...) statement. This
is followed by an open and closed brace. Your programs are written within these
braces. Execution of the program begins at the open brace and ends at the return
statement, which immediately precedes the closed brace.
Unfortunately, a more detailed explanation of this framework must be left to
future chapters. Don’t worry . . . we get to it before the weekend is out.
4689-9 ch04.f.qc 3/7/00 9:20 PM Page 40
40 Friday Evening
Comments
The first few lines of the program appear to be free form text. Either this “code”
was meant for human eyes or the computer is a lot smarter than anyone’s ever
given it credit for being. These first six lines are known as comments. A comment
is a line or portion of a line that is ignored by the C++ compiler. Comments enable
the programmer to explain what he or she was doing or thinking while writing a
particular segment of code.
A C++ comment begins with a double slash (“//”) and ends with a newline. You
can put any character you want in a comment. A comment may be as long as you
want, but it is customary to keep comments to 80 characters or so, because that’s
all that will fit on the computer screen.
A newline would have been known as a “carriage return” back in the days of
typewriters, when the act of entering characters into a machine was called “typing”
and not “keyboarding.” A newline is the character that terminates a command line.
C++ allows a second form of comment in which everything appearing after a /*
and before a */ are ignored; however, this form of comment is not normally used
in C++ anymore.
It may seem odd to have a command in C++, or any other pro-
gramming language, which is ignored by the computer. However,
all computer languages have some version of the comment. It is
Note critical that the programmer explain what was going through her
or his mind at the time that the code was written. It may not be
obvious to the next person who picks up the program and uses
it or modifies it. In fact, after only a few months it may not be
obvious to the programmer what he or she meant.
Tip
The next four lines represent that framework I mentioned earlier. Remember that
the program begins executing with the first statement after the open brace.
4689-9 ch04.f.qc 3/7/00 9:20 PM Page 41
Statements
The first noncomment line after the brace is a C++ statement. A statement is a sin-
Session 4
should end in a semicolon as well, for consistency’s sake if for no other reason.)
As you look through the program, you can see that spaces, tabs, and newlines
appear throughout the program. In fact, I have placed a newline after every state-
ment in this program. These characters are collectively known as white space
because you can’t see any of them on the monitor. A white space is a space, a
tab, a vertical tab, or a newline. C++ ignores white space.
You may add white space anywhere you like in your program to
enhance readability, except in the middle of a word.
Tip
While C++ may ignore white space, it does not ignore case. The variable full-
speed and the variable FullSpeed have nothing to do with each other. While the
command int may be understood completely, C++ has no idea what INT means.
Declarations
In the second expression, y is set equal to 3 times x, but what is x? The variable x
acts as a holding tank for a value. In this case, the value of x is 10, but we could
have just as well set the value of x to 20 or 30 or –1. The second formula makes
sense no matter what the value of x.
In algebra, it’s allowed to begin with a statement such as x = 10. In C++, the
programmer must first define the variable x before it can be used.
In C++, a variable has a type and a name. The line int nCelcius; declares an
variable nCelcius designed to hold an integer. (Why they couldn’t have just said
integer instead of int, I’ll never know. It’s just one of those things that you learn
to live with.)
4689-9 ch04.f.qc 3/7/00 9:20 PM Page 42
42 Friday Evening
Input/output
The lines beginning with cout and cin are known as input/output statements,
often contracted to I/O statements. (Like all engineers, programmers love contrac-
tions and acronyms.)
The first I/O statement says output the phrase “Enter the temperature in
Celsius:” to cout (pronounced “see-out”). cout is the name of the standard C++
output device. In this case, the standard C++ output device is your monitor.
The next line is exactly the opposite. This line says to extract a value from the
C++ input device and store it in the integer variable nCelsius. The C++ input
device is normally the keyboard. This is the C++ analogue to the algebra formula x
= 10 mentioned above. For the remainder of the program, the value of nCelsius is
whatever the user enters here.
Expressions
In the next two lines, which are marked as a “calculation expression,” the program
declares a variable nFactor and assigns it the value resulting from a calculation.
This command calculates the difference of 212 and 32. In C++, such a formula is
called an expression.
4689-9 ch04.f.qc 3/7/00 9:20 PM Page 43
Session 4
The spoken language can be very ambiguous. The term equals is one of those
ambiguities.
The word equals can mean that two things have the same value as in 5 cents
equals a nickel. Equals can also imply assignment as in math when you say that
y equals 3 times x.
To avoid ambiguity, C++ programmers call = the assignment operator.
The assignment operator says store the results of the expression on the right
of the = in the variable to the left. Programmers say that nFactor is assigned the
value 212 – 32.
Expressions (continued)
REVIEW
You have finally seen an explanation of the Conversion program entered in
Sessions 2 and 3. Of necessity, this explanation has been at a high level. Don’t
worry, however; the details are forthcoming.
All programs begin with the same framework.
C++ allows you to include comments that are explanations to yourself and
others as to what different parts of a program do.
4689-9 ch04.f.qc 3/7/00 9:20 PM Page 44
44 Friday Evening
C++ expressions look a lot like algebraic expressions, except that C++ vari-
ables have to be declared before they can be used.
= is called assignment.
C++ input and output statements default to the keyboard and the screen or
MS-DOS window.
QUIZ YOURSELF
1. What does the following C++ statement do? (See Comments.)
// I’m lost