5 - Input-Output Streams As An Introduction
5 - Input-Output Streams As An Introduction
in_stream.close( );
out_stream.close( );
#include <fstream.h>
int main()
{
ifstream in_stream;
ofstream out_stream;
in_stream.close( );
out_steam.close( );
return ;
}
• 5.1.3 Introduction to classes and Objects
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
• If your program is sending output to a file that is
connected to an output stream called out_stream, you
can use these same commands to ensure that numbers
with decimal point will be written in a way we normally
write amounts of money.
• Just insert the following in your program
out_stream.setf(ios::fixed);
out_stream.setf(ios::showpoint);
out_stream.precision(1);
• The other formatting instructions in our magic formula
are:
• Setf is an abbreviation for set flags. A flag is an
instruction to do something in one of the two possible
ways. If the flag is given as an argument to setf, then the
flag tells the computer to write output to that stream in
some specific way. What it causes the stream to do
depends on the flag.
• In the above example, there are two calls to the function
setf and these two calls set the two flags ios::fixed and
ios::showpoint.
• The flag ios::fixed causes the stream to output numbers
of type double in what is called fixed-point notation.
• If the flag ios::fixed is set, then all floating point numbers
that are output to that stream will be written in ordinary
everyday notation.
• The flag ios::showpoint tells the stream to always include
a decimal point in floating point numbers
• Another useful flag is ios::showpos. If this flag is set for a
stream, then positive numbers output to that stream will
be written with the plus sign in front of them, e.g.
• The width function tells the stream how many spaces to
use when giving an item as output.
cout.setf(ios::showpos);
cout.width(4);
• 5.3 Character I/O
• 5.3.1 The member Functions get and put
Syntax:
Output_Stream.put (Char_expression);
Examples:
cout.put(next_symbol;
cout.put(‘a’)
• 5.4 Inheritance
• One of the most powerful features of C++ is the use of
derived classes. A derived class is obtained from
another class by adding features.
• E.g the class of input-file streams is derived from the
class of all input streams by adding additional member
functions such as open and close.
• The cin belongs to the class of all input streams, but
does not belong to the class of input-file streams
because cin has no member functions named open and
close.
• The word inheritance is just another name for the topic of
derived classes.
• 5.1 Inheritance among Stream Classes
• Recall that an object is a variable that has member
functions.
• A class is a type whose variables are objects.
• Streams are objects, so streams types such as ifstream
and ofstream are classes.
• An input-file stream can be connected to a file using the
member function open, but the stream cin has no
member function named open.
• An input-file is a similar but different kind of stream than
cin.
• An input-file stream is of type ifstream.
• cin is of type istream.
• The classes ifstream and istream are different but
closely related types.
• The class ifstream is a derived class of the class istream.
• Consider the following function, which reads two integers
from the input stream source_file and writes their sum to
the screen:
void two_sum(ifstream& source_file)
{
int n1, n2;
source_file >> n1 >> n2;
cout << n1 “ + ” << n2 << “ = ” << (n1 + n2) << endl;
}
two_sum(fin)