EEB 334 - Chapter 6
EEB 334 - Chapter 6
EEB 334 - Chapter 6
Sheikh
Senior Lecturer
Department of Electrical Engineering
University of Botswana
• The types ifstream and ofstream are defined in the library with
ifstream in_stream;
the header file fstream.h and following directive should be
ofstream out_stream;
included
#include <fstream.h>
• Stream variables, such as in_steram and out_stream declared
above, must each be connected to file.
• This is called opening the file and is done with a function
named open.
in_stream.open(“infile.dat”);
• When used with a stream of type ofstream, the member function open will
create the output file if it does not already exist.
• If the output file does exist, the member function open will discard the contents
of the file, so that the output file is empty after the call to open.
• After a file is connected to the stream out_stream with a call to open, the
program can send output to that file using the insertion operator <<.
• e.g.. the following writes two strings and the contents of the variables
one_number and another_number to the which is connected to the stream
out_stream.
in_stream.close( );
out_stream.close( );
#include <fstream.h>
int main()
{
ifstream in_stream;
ofstream out_stream;
in_stream.open(“infile.dat”);
out_stream.open(“outfile.dat”);
in_stream.close( );
out_steam.close( );
return 0;
}
Introduction to classes and Objects
• The layout of a program’s output is called format of the output. E.g. the magic
formula
cout.set(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.set(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);
Character I/O
The member Functions get and put
Syntax:
Output_Stream.put (Char_expression);
Examples:
cout.put(next_symbol;
cout.put(‘a’)
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.
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 put-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:
• Suppose your program contains the above function definition and the following
stream declaration
Ifstream fin;
• If fin is connected to a file with a call to open, you can use the function two_sum
to read two integers from that file and write their sum to the screen. The call
would be the following
two_sum(fin)
• Suppose that later on in the same program, you want your program to read two
numbers from the keyboard and write their sum to the screen.
• Since all input streams are similar, you might think you can use cin as the
argument in a second call to two_sum as shown below: