Tuesday, March 28, 2017

C++ Part 43

Review Part VI

Parameters and Arguments
All the different terms that have to do with parameters and arguments can
be confusing. However, if you keep a few simple points in mind, you will
be able to easily handle these terms.
1. The formal parameters for a function are listed in the function declaration
and are used in the body of the function definition. A formal parameter (of
any sort) is a kind of blank or placeholder that is filled in with something
when the function is called.
2. An argument is something that is used to fill in a formal parameter. When
you write down a function call, the arguments are listed in parentheses after
the function name. When the function call is executed, the arguments are
“plugged in” for the formal parameters.
3. The terms call-by-value and call-by-reference refer to the mechanism that is
used in the “plugging in” process. In the call-by-value method only the value
of the argument is used. In this call-by-value mechanism, the formal parameter is a local variable that is initialized to the value of the corresponding argument.
In the call-by-reference mechanism the argument is a variable and the entire
variable is used. In the call-by-reference mechanism, the argument variable is
substituted for the formal parameter so that any change that is made to the
formal parameter is actually made to the argument variable.

A File Has Two Names
Every input and every output file used by your program has two names.
The external file name is the real name of the file, but it is used only in
the call to the function open, which connects the file to a stream. After the
call to open, you always use the stream name as the name of the file.

Calling a Member Function
SYNTAX
Calling _Object.Member_Function_Name(Argument_List);
EXAMPLES
in_stream.open("infile.dat");
out_stream.open("outfile.dat");
out_stream.precision(2);
The meaning of the Member_Function_Name is determined by the class of
(that is, the type of) the Calling _Object.


Classes and Objects
An object is a variable that has functions associated with it. These
functions are called member functions. A class is a type whose variables
are objects. The object’s class (that is, the type of the object) determines
which member functions the object has.

The exit Statement
The exit statement is written
exit(Integer_Value);
When the exit statement is executed, the program ends immediately. Any
Integer_Value may be used, but by convention, 1 is used for a call to
exit that is caused by an error, and 0 is used in other cases. The exit
statement is a call to the function exit, which is in the library with header
file named cstdlib. Therefore, any program that uses the exit statement
must contain the following directives:
#include
using namespace std;
(These directives need not be given one immediately after the other. They
are placed in the same locations as similar directives we have seen.)

Summary of File I/O Statements
In this sample the input comes from a file with the directory name infile.dat, and the output
goes to a file with the directory name outfile.dat.
■ Place the following include directives in your program file:
#include
#include
#include
■ Choose a stream name for the input stream (for example, in_stream), and declare it to be a variable
of type ifstream. Choose a stream name for the output file (for example, out_stream), and declare
it to be of type ofstream:
using namespace std;
ifstream in_stream;
ofstream out_stream;
■ Connect each stream to a file using the member function open with the external file name as an argument.
Remember to use the member function fail to test that the call to open was successful:
in_stream.open("infile.dat");
if (in_stream.fail( ))
{
cout << "Input file opening failed.\n"; exit(1); } out_stream.open("outfile.dat"); if (out_stream.fail( )) { cout << "Output file opening failed.\n"; exit(1); } ■ Use the stream in_stream to get input from the file infile.dat just like you use cin to get input from the keyboard. For example: in_stream >> some_variable >> some_other_variable;
■ Use the stream out_stream to send output to the file outfile.dat just like you use cout to send
output to the screen. For example:
out_stream << "some_variable = " << some_variable << endl; ■ Close the streams using the function close: in_stream.close( ); out_stream.close( );

Appending to a File
If you want to append data to a file so that it goes after any existing
contents of the file, open the file as follows.
SYNTAX
Output_Stream.open(File_Name, ios::app);
EXAMPLE
ofstream outStream;
outStream.open("important.txt", ios::app);

'\n' and "\n"
'\n' and "\n" sometimes seem like the same thing. In a cout statement,
they produce the same effect, but they cannot be used interchangeably in
all situations. '\n' is a value of type char and can be stored in a variable
of type char. On the other hand, "\n" is a string that happens to be made
up of exactly one character. Thus, "\n" is not of type char and cannot be
stored in a variable of type char.



No comments:

Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two diļ¬€erent implementations of the same instru...