Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views11 pages

Introducing The C++ IO System

The document introduces the C++ I/O system, covering formatted I/O, custom inserters, extractors, and manipulators, as well as file I/O basics and conversion functions. It provides detailed explanations of functions like setf(), width(), precision(), and fill(), along with examples of creating custom I/O operations. Additionally, it includes exercises to apply the concepts learned, such as creating a book class and implementing custom I/O functionalities.

Uploaded by

Tayef Shahriar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Introducing The C++ IO System

The document introduces the C++ I/O system, covering formatted I/O, custom inserters, extractors, and manipulators, as well as file I/O basics and conversion functions. It provides detailed explanations of functions like setf(), width(), precision(), and fill(), along with examples of creating custom I/O operations. Additionally, it includes exercises to apply the concepts learned, such as creating a book class and implementing custom I/O functionalities.

Uploaded by

Tayef Shahriar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Introducing the C++ I/O System

Contents
• Formatted I/O

• Using width(), precision() and fill()

• Using I/O Manipulators

• Creating Your Own Inserters

• Creating Extractors

• Creating Your Own Manipulators

• File I/O Basics

• Creating a Conversion Function

• Exercises
Formatted I/O
• To set a format flag, use the setf() function. This function is a member of ios.

• For example, to turn on the showpos flag, you can use this statement:
stream.setf(ios::showpos)

• Here stream is the stream you wish to affect.

• Because the format flags are defined within the ios class, you must access their values by using
ios and the scope resolution operator.

• It is possible to set more than one flag in a single call to setf(). To do this, OR together the
values of the flags you want to set.

• The complement of setf() is unsetf(). This member function of ios clears one or more format
flags.
Using width(), precision() and fill()
• In addition to the formatting flags, there are three member functions defined
by ios class that set these format parameters: the field width, the precision,
and the fill character.

• These are width(), precision() and fill() respectively.


Using I/O Manipulators
• I/O manipulators are special I/O format functions that you can format
information using C++’s I/O system.

• I/O manipulator affects only the stream of which the I/O expression is a part.

• I/O manipulators do not affect all streams currently opened for use.

• When a manipulator does not take an argument, such as oct, it is not


followed by parentheses. This is because it is the address of the manipulator
that is passed to the overloaded << operator.
Creating Your Own Inserters
• In C++, the output operation is called an insertion and the << is called the insertion operator.

• When you overload the << for output, you are creating an inserter function, or inserter for short.

• All inserter functions have this general form:


ostream &operator<<(ostream &stream, class-name ob){
return stream;

• What an inserter does is completely up to you.

• When you create an inserter, the left operand is a stream and the right operand is the object that you
want to output. Hence, an inserter cannot be a member function.

• In most programming situations you will encounter, an overloaded inserter will be a friend of the
class for which it was created.
Creating Extractors
• In C++, the >> is referred to as the extraction operator and a function that overloads it is
called an extractor.

• The general form of an extractor function is shown here:


istream &operator<<(istream &stream, class-name &ob){
return stream;

• Extractors return a reference to istream.

• The first parameter must be a reference to an input stream.

• The second parameter is a reference to the object that is receiving input.

• An extractor cannot be a member function like inserters.


Creating Your Own Manipulators
• It is not uncommon to have situations in which the same sequence of I/O
operations occurs frequently within a program. In these cases, you can use a
custom manipulator to perform these actions, thus simplifying your source code
and preventing accidental errors.

• Custom manipulators can help make any I/O intensive program clearer and more
efficient.

• Writing your own parameterless manipulators is quite easy and is examined here.

• It is crucial that your manipulators return a reference to the invoking stream.


File I/O Basics
• File I/O and console I/O are closely related.

• In C++, a file is opened by linking it to a stream.


fstream io; // input and output

• Once you have created a stream, one way to associate it with a file is by
using the function open().

• The constructor functions have the same parameters and defaults as the
open() function.

• To close a file use the member function close().


Creating a Conversion Function
• Sometimes it is useful to convert an object of one type into an object of another.

• A conversion function automatically converts an object into a value that is


compatible with the type of the expression in which the object is used.

• The general form of a conversion function is shown here:


operator type()

{ return value; }

• No parameters can be specified, and a conversion function must be a member of


the class for which it performs the conversion.
Exercises
• Create a class representing a book with attributes like title, author, and price. Use
formatted I/O to display the book's details neatly, including a fixed-width column for
the price.

• Modify the book class to include a double attribute for the average rating. Use
precision() to control the number of decimal places when displaying the rating.

• Create a custom extractor operator (>>) for the book class to read book details from
the user. Prompt the user for book information and create a book object.

• Create a custom manipulator that converts text to uppercase when used in the output
stream. Use it to display the book's title and author in uppercase.

You might also like