📄 chap01.htm
字号:
to a file on some systems, like the output to the <B>cout </B>stream object
can be. The three streams, <B>cout</B>, <B>cin</B>, and <B>cerr</B>, correspond
to the <B>stdout</B>, the <B>stdin</B>, and the <B>stderr </B>stream pointers
of the C programming language. Their use will be illustrated throughout
the remainder of this tutorial.
<P>The stream library also has file I/O capability which will be briefly
illustrated in the next example program.
<P>Be sure to compile and execute this program before going on to the next
one. Remember that the system will ask you to enter an integer value which
will be echoed back to the monitor, but changed to the hexadecimal base.
<P><B>FILE STREAM OPERATIONS</B>
<P>Example program ------> <B><A HREF="FSTREAM.CPP">FSTREAM.CPP</A></B>
<P>Examine the example program named FSTREAM.CPP for examples of the use
of streams with files. We will be using a few C++ objects in this program
but we hAve not yet studied them, so you will not understand them at this
point. Don't spend too much time trying to understand them yet.
<P>In this program a file is opened for reading, another for writing, and
a third stream is opened to the printer to illustrate the semantics of
stream operations on a file. Both input and output files are of type FILE
in C programs, but the ifstream type is used for file input and the <B>ofstream
</B>is used for output files. This is illustrated in lines 8 through 10
of this example program. Actually <B>ifstream </B>is a C++ class and <B>infile
</B>is an object of that class as we will see later in this tutorial.
<P>The only difference between the streams in the last program and the
streams in this program is the fact that in the last program, the streams
were already opened for us by the system. You will note that the stream
object named <B>printer </B>is used in the same way we used the <B>cout
</B>stream object in the last program. Finally, because we wish to exercise
good programming practice, we close all of the files we have opened prior
to ending the program.
<P>This is our first example of object oriented programming because we
are actually using objects in this program. The object named <B>infile
</B>is told to open itself in line 17, then it is told to get one character
at a time in line 44, and finally it is told to close itself in line 52.
The "dot" notation is used with objects in a similar manner that structures
are used in C. The name of the object is mentioned, followed by a "dot",
and followed by the name of the action that the object is to execute. This
terminology will be used in great profusion later in this tutorial, so
don't worry about it at this time. The objects <B>outfile </B>and <B>printer
</B>are manipulated in exactly the same manner.
<P>For more information on the stream file I/O library, see Bjarne Stroustrup's
book which is listed in the introduction to this tutorial, or refer to
your compiler documentation. Don't worry too much about it yet, after you
learn the terminology by studying the rest of this tutorial, you will be
able to return to a personal study of the stream library, and profit greatly
from the study.
<P>Be sure to compile and execute this program. When you execute it, it
will request a file to be copied. You can enter the name of any ASCII file
that resides in the current directory.
<P><B>VARIABLE DEFINITIONS</B>
<P>Example program ------> <B><A HREF="VARDEF.CPP">VARDEF.CPP</A></B>
<P>Examine the file named VARDEF.CPP for a few more additions to the C++
language which aid in writing a clear and easy to understand program. In
C++, as in ANSI-C, global and static variables are automatically initialized
to zero when they are declared. The variables named <B>index </B>in line
4, and <B>goofy </B>in line 27 are therefore automatically initialized
to zero. Of course, you can still initialize either to some other value
if you so desire. Global variables are sometimes called external since
they are external to any functions.
<P>Automatic variables, those declared inside of any function, are not
automatically initialized but will contain the value that happens to be
in the location where they are defined, which must be considered a garbage
value. The variable named <B>stuff </B>in line 8, therefore does not contain
a valid value, but some garbage value which should not be used for any
meaningful purpose. In line 11, it is assigned a value based on the initialized
value of <B>index </B>and it is then displayed on the monitor for your
examination.
<P><B>THE C++ REFERENCE</B>
<P>Notice the ampersand in line 9. This defines <B>another_stuff</B> as
a reference which is a new addition to C++. The reference should not be
used very often, if ever, in this context, but this is a very simple example
used to introduce the reference and discuss its use and operation. The
reference is not the same as any other entity used in C because it operates
like a self dereferencing pointer. Following its initialization, the reference
becomes a synonym for the variable <B>stuff</B>, and changing the value
of <B>stuff </B>will change the value of <B>another_stuff</B> because they
are both actually referring to the same variable. The synonym can be used
to access the value of the variable <B>stuff </B>for any legal purpose
in the language. It should be pointed out that a reference must be initialized
to reference some variable when it is defined or the compiler will respond
with an error. Following initialization, the reference cannot be changed
to refer to a different variable. The reference is difficult to discuss
because we want to call it a reference variable, but it is not a variable
since it cannot be changed. Whether we like the name or not it is simply
called a reference.
<P>The use of the reference in this way can lead to very confusing code,
but it has another use where it can make the code very clear and easy to
understand. We will study the proper use of the reference in chapter 4
of this tutorial.
<P><B>DEFINITIONS ARE EXECUTABLE STATEMENTS</B>
<P>Coming from your background of C, you will find the statement in line
16 very strange, but this is legal in C++. Anyplace it is legal to put
an executable statement, it is also legal to define a new variable because
a data definition is an executable statement according to the definition
of C++. In this case, we define the new variable named <B>more_stuff</B>
and initialize it to the value of 13. It has a scope from the point where
it is defined to the end of the block in which it is defined, so it is
valid throughout the remainder of the <B>main() </B>program. The variable
named <B>goofy </B>is defined even later in line 27.
<P>It is significant that the variable is declared near its point of usage.
This makes it easier to see just what the variable is used for, since it
has a much more restricted scope of validity. When you are debugging a
program, it is convenient if the variable declaration is located in close
proximity to where you are debugging the code.
<P><B>WHAT ABOUT definition AND declaration?</B>
<P>The words definition and declaration refer to two different things in
C++, and in ANSI-C also for that matter. They really are different and
have different meanings, so we should spend a little time defining exactly
what the words mean in C++. A declaration provides information to the compiler
about the characteristics of something such as a type or a function but
it doesn't actually define any code to be used in the executable program.
A definition, on the other hand, actually defines something that will exist
in the executable program, either some useful variables, or some executable
code. You are required to have one and only one definition of each entity
in the program. In short, a declaration introduces a name into the program
and a definition introduces some code and requires memory space to store
something.
<P>If we declare a <B>struct</B>, we are only declaring a pattern to tell
the compiler how to store data later when we define one or more variables
of that type. But when we define some variables of that type, we are actually
declaring their names for use by the compiler, and defining a storage location
to store the values of the variables. Therefore, when we define a variable,
we are actually declaring it and defining it at the same time.
<P>C permits multiple definitions of a variable in any given namespace,
provided the definitions are the same and it generates only a single variable
for the multiple definitions. C++, however, does not permit redefinition
of a variable or any other entity for a very definite reason that we will
discuss later.
<P>We will refer to these definitions many times throughout the course
of this tutorial so if this is not clear now, it will clear up later.
<P><B>A BETTER for LOOP</B>
<P>Take careful notice of the <B>for </B>loop defined in line 20. This
loop is a little clearer than the <B>for </B>loop that is available in
ANSI-C, because the loop index is defined in the <B>for </B>loop itself.
The scope of this loop index depends on how new your compiler is.
<P>If your compiler is a little old, the scope of the loop index is from
its definition to the end of the enclosing block. In this case its scope
extends to line 32 since the closing brace in line 32 corresponds to the
most recent opening brace prior to the definition of the variable. Since
the variable is still available, it can be used for another loop index
or for any other purpose which an integer type variable can legally be
used for.
<P>If your compiler is relatively new, the scope of the loop index is from
its definition to the end of the loop. In this case its scope extends to
line 25 because that is where the loop ends. This is a very new extension
to the C++ language by the standards committee, and will only be available
with the newest compilers.
<P>Regardless of the age of your compiler, the variable named <B>count2
</B>is defined and initialized during each pass through the loop because
it is defined within the block controlled by the <B>for </B>loop. Its scope
is only the extent of the loop, lines 23 to 25, so that it is automatically
deallocated each time the loop is completed. It is therefore defined, initialized,
used and deallocated five times, once for each pass through the loop.
<P>You will notice that the variable <B>count2 </B>is assigned a numerical
value in line 23 but when it is printed out, a character value is actually
output. This is because C++ is careful to use the correct type for output.
If you have a very old compiler, it may output a numerical value here.
<P>Finally, as mentioned earlier, the static variable named <B>goofy </B>is
defined and automatically initialized to zero in line 27. Its scope is
from the point of its definition to the end of the block in which it is
defined, line 32.
<P>Be sure to compile and execute this program.
<P><B>OPERATOR PRECEDENCE</B>
<P>Operator precedence is identical to that defined for ANSI-C so no attempt
will be made here to define it. There is a small difference when some operators
are overloaded which we will study later in this tutorial. Some of the
operators act slightly different when overloaded than the way they operate
with elements of the predefined language.
<P>Do not worry about the previous paragraph, it will make sense later
in this tutorial after we have studied a few more topics.
<P><B>PROGRAMMING EXERCISES</B>
<OL>
<LI>
Write a program that displays your name and date of birth on the monitor
three times using the <B>cout </B>stream object. Define any variables you
use as near as possible to their point of usage.</LI>
<LI>
Write a program with a few <B>const </B>values and <B>volatile </B>variables
and attempt to change the value of the constants to see what kind of error
message your compiler gives you.</LI>
<LI>
Write a program that uses stream objects to interactively read in your
birthday with three different <B>cin </B>statements. Print your birthday
in octal, decimal, and hexadecimal notation just for the practice.</LI>
</OL>
<A HREF="chap02.htm">Advance to Chapter 2</A>
<P><A HREF="cpplist.htm">Return to the Table of Contents</A>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -