📄 chap01.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Gordon Dodrill">
<META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (Win95; I) [Netscape]">
<TITLE>C++ Tutorial - Chapter 1</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<B>C++ Tutorial - Chapter 1</B>
<P><B><FONT SIZE=+3>S</FONT><FONT SIZE=+2>IMPLE</FONT><FONT SIZE=+3> T</FONT><FONT SIZE=+2>HINGS</FONT></B>
<P>As we begin the study of C++ and object oriented programming, a few
comments are in order to help you get started. Since the field of object
oriented programming is probably new to you, you will find that there is
a significant amount of new terminology for you to grasp. This is true
of any new endeavor and you should be warned not to be intimidated by all
of the new concepts. We will add a few new topics in each chapter and you
will slowly grasp the entire language.
<P>Chapters one through four of this tutorial will concentrate on the non
object oriented additions to C++. We will not begin the discussion of any
object oriented programming techniques until chapter five.
<P><B>EVEN COMMENTS ARE IMPROVED IN C++</B>
<P>Example program ------> <B><A HREF="CONCOM.CPP">CONCOM.CPP</A></B>
<P>Examine the file named CONCOM.CPP for an example of several new things
in C++. We will take the new constructs one at a time beginning with the
comments.
<P>A C++ comment begins with the double slash "//", starts anywhere on
a line, and runs to the end of that line where it is automatically terminated.
The old method of comment definition used with ANSI-C can also be used
with C++ as illustrated in lines 11 through 14, among other places in this
program. The new method is the preferred method of comment definition because
it is impossible to inadvertently comment out several lines of code. This
can be done by forgetting to include the end of comment notation when using
the older C method of comment notation. Good programming practice would
be to use the new method for all comments and reserve the old method for
use in commenting out a section of code during debugging since the two
methods can be nested.
<P>It would be well to caution you at this point however, that you should
not use comments when the same sense of program definition can be obtained
by using meaningful names for variables, constants, and functions. The
careful selection of variable and function names can make nearly any code
self documenting and you should strive to achieve this in your code.
<P><B>THE KEYWORDS const AND volatile</B>
<P>There are two new keywords used in lines 9 through 11 which were not
part of the original K&R definition of C, but are part of the ANSI-C
standard. The keyword <B>const </B>is used to define a constant. In line
9 the constant is of type <B>int</B>, it is named START, and is initialized
to the value 3. The compiler will not allow you to accidentally or purposefully
change the value of START because it has been declared a constant. If you
had a variable named STARTS in this program, the system would not allow
you to slightly misspell STARTS as START and accidentally change it. The
compiler would give you an error message so you could fix the error. Since
it is not permissible to change the value of a constant, it is imperative
that you initialize it when it is declared so it will have a useful value.
<P>You will note that the keyword <B>const </B>is also used in the function
header in line 23 to indicate that the formal parameter named <B>data_value</B>
is a constant throughout the function. Any attempt to assign a new value
to this variable will result in a compile error. This is a small thing
you can add to your programs to improve the compiler's ability to detect
errors for you.
<P>The keyword <B>volatile </B>is also part of the ANSI-C standard but
was not included in the original K&R definition of C. Even though the
value of a <B>volatile </B>variable can be changed by you, the programmer,
there may be another mechanism by which the value could be changed, such
as by a hardware interrupt timer causing the value to be incremented. The
compiler needs to know that this value may be changed by some external
force when it optimizes the code. A study of code optimization methods
is very interesting, but beyond the scope of this tutorial. Note that a
constant can also be volatile, which means that you cannot change it, but
the system can modify it through some hardware function.
<P>Ignore the output statement given in line 25 for a few minutes. We will
study it in some detail later in this chapter. If you are experienced in
the K&R style of programming, you may find line 5 and 23 a little strange.
This illustrates prototyping and the modern method of function definition
as defined by the ANSI-C standard. Prototyping is optional in C but absolutely
required in C++. For that reason, chapter 4 of this tutorial is devoted
entirely to prototyping.
<P>It would be advantageous for you to compile and execute this program
with your C++ compiler to see if you get the same result as given in the
comments at the end of the listing. One of the primary purposes of compiling
it is to prove that your compiler is loaded and executing properly.
<P><B>THE SCOPE OPERATOR</B>
<P>Example program ------> <B><A HREF="SCOPEOP.CPP">SCOPEOP.CPP</A></B>
<P>The example program named SCOPEOP.CPP illustrates another construct
that is new to C++. There is no corresponding construct in either K&R
or ANSI-C. This allows access to the global variable named <B>index </B>even
though there is a local variable of the same name within the <B>main()
</B>function. The use of the double colon in front of the variable name,
in lines 11, 13, and 16, instructs the system that we are interested in
using the global variable named <B>index</B>, defined in line 4, rather
than the local variable defined in line 8.
<P>The use of this technique allows access to the global variable for any
use. It could be used in calculations, as a function parameter, or for
any other purpose. It is not really good programming practice to abuse
this construct, because it could make the code difficult to read. It would
be best to use a different variable name instead of reusing this name,
but the construct is available to you if you find that you need it sometime.
<P>Be sure to compile and execute this program before proceeding on to
the next example program where we will discuss the cout operator used in
lines 10, 11, 15, and 16.
<P><B>THE iostream LIBRARY</B>
<P>Example program ------> <B><A HREF="MESSAGE.CPP">MESSAGE.CPP</A></B>
<P>Examine the example program named MESSAGE.CPP for our first hint of
object oriented programming, even though it is a very simple one. In this
program, we define a few variables and assign values to them for use in
the output statements illustrated in lines 17 through 20, and in lines
23 through 26. <B>cout </B>is used to output data to the standard device,
the monitor, but it works a little differently from our old familiar <B>printf()</B>
function, because we do not have to tell the system what type we are outputting.
Note that <B>cout </B>is not actually an output function, but we can sort
of think of it as one until we fully define it later in this tutorial.
For the time being, we will simply state that <B>cout </B>is an object
used to send output data to the monitor.
<P>C++, like the C language, has no input or output operations as part
of the language itself, but defines the stream library to add input and
output functions in a very elegant manner. A portion of the stream library
is included in this program in line 2.
<P>The operator <<, sometimes called the "put to" operator but more
properly called the insertion operator, tells the system to output the
variable or constant following it, but lets the system decide how to output
the data. In line 17, we first tell the system to output the string, which
it does by copying characters to the monitor, then we tell it to output
the value of <B>index</B>. Notice however, that we fail to tell it what
the type is or how to output the value. Since we don't tell the system
what the type is, it is up to the system to determine what the type is
and to output the value accordingly. After the system finds the correct
type, we also leave it up to the system to use the built in default as
to how many characters should be used for this output. In this case, we
find that the system uses exactly as many characters as needed to output
the data, with no leading or trailing blanks, which is fine for this output.
Finally, the newline character is output as a single character string,
and the line of code is terminated with a semicolon.
<P>When we told the <B>cout </B>object to output some data in line 17,
we actually called two different functions because we used it to output
two strings and a variable of type <B>int</B>. This is the first hint at
object oriented programming because we simply broadcast a message to the
system to print out a value, and let the system find an appropriate function
to do so. We are not required to tell the system exactly how to output
the data, we only tell it to output it. This is a very weak example of
object oriented programming, and we will study it in much more depth later
in this tutorial.
<P>In line 18, we tell the system to output a different string, followed
by a floating point number, and another string of one character, the newline
character. In this case, we told it to output a floating point number without
telling it that it was a floating point number, once again letting the
system choose the appropriate output means based on its type. We did lose
a bit of control in the transaction, however, because we had no control
over how many significant digits to print before or after the decimal point.
We chose to let the system decide how to format the output data.
<P>The variable named <B>letter </B>is of type <B>char</B>, and is assigned
the value of the uppercase X in line 14, then printed as a letter in line
19 because the <B>cout </B>object knows it is a character and outputs it
accordingly.
<P>Because C++ has several other operators and functions available with
streams, you have complete flexibility in the use of the stream output
functions. You should refer to your compiler documentation for details
of other available formatting commands. Because it is expected to be mandated
by the upcoming ANSI-C++ standard, the <B>cout </B>and <B>printf()</B>
statements can be mixed in any way you desire. However all compilers do
not yet conform to this standard and some use different forms of buffering
for the two kinds of output. This could result in scrambled output, but
it should be easy for the student to repair the output in such a way that
only one form is used, either the <B>cout </B>or the <B>printf()</B>.
<P><B>MORE ABOUT THE stream LIBRARY</B>
<P>The stream library was defined for use with C++ in order to add to the
execution efficiency of the language. The <B>printf()</B> function was
developed early in the life of the C language and is meant to be all things
to all programmers. As a result, it became a huge function with lots of
extra baggage, most of which is used by only a few programmers. By defining
the special purpose stream library, the design of C++ allows the programmer
to use full formatting capabilities, but only load what is needed for the
current programming job. Although it is not all illustrated here, there
is a full set of formatting functions available in the C++ stream library.
Check your compiler documentation for the complete list.
<P>Lines 23 through 26 illustrate some of the additional features of the
stream library which can be used to output data in a very flexible yet
controlled format. The value of <B>index </B>is printed out in decimal,
octal, and hexadecimal format in lines 23 through 25. When one of the special
stream operators, <B>dec</B>, <B>oct</B>, or <B>hex</B>, is output, all
successive output will be in that number base. Looking ahead to line 30,
we find the value of <B>index </B>printed in hex format due to the selection
of the hexadecimal base in line 25. If none of these special stream operators
are output, the system defaults to decimal format.
<P><B>THE cin OPERATOR</B>
<P>In addition to the predefined stream object, named <B>cout</B>, there
is a predefined <B>cin</B> stream object which is used to read data from
the standard input device, usually the keyboard. The <B>cin </B>stream
uses the >> operator, usually called the "get from" operator but properly
called the extraction operator. It has most of the flexibility of the <B>cout</B>
stream. A brief example of the use of the <B>cin </B>stream is given in
lines 28 through 30. The special stream operators, <B>dec</B>, <B>oct</B>,
and <B>hex</B>, also select the number base for the <B>cin </B>stream separately
from the <B>cout </B>stream. If none is specified, the input stream also
defaults to decimal.
<P>In addition to the <B>cout </B>stream object and the <B>cin</B> stream
object there is one more standard stream object, <B>cerr</B>, which is
used to output to the error handling device. This device cannot be redirected
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -