19CUG38 – OOPS WITH C++
Manipulators
in C++
Prepared & Presented By
Mrs.B.Meena Preethi
Assistant Professor
Department of SS
Sri Krishna Arts and Science
College
ODD 2020-21
Learners Support Publications www.lsp4you.com
TOPICS TO BE COVERED
Manipulators in C++
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
What is a Manipulator ?
Manipulators are operators used in C++
for formatting output.
The data is manipulated by the
programmer's choice of display.
Manipulators are functions specifically
designed to be used in conjunction with
the insertion (<<) and extraction (>>)
operators on stream objects, for example:
cout<< endl;
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
Manipulators in C++
Manipulator Declaration in
endl iostream.h
setw iomanip.h
setprecision iomanip.h
setf iomanip.h
setbase iomanip.h
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
Endl manipulator
endl manipulator is used to Terminate a
line and flushes the buffer.
std::endl sends a newline character '\n'
and flushes the output buffer.
'\n' sends the newline character, but does
not flush the output buffer.
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
Endl manipulator
Example
#include <iostream.h>
int main()
{
cout<<"USING '\\n' ...\n";
cout<<"Line 1 \nLine 2 \nLine 3 \n"; cout<<"USING end
..."<< endl;
cout<< "Line 1" << endl << "Line 2" << endl << "Line 3" <<
endl; return 0;
}
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
Output - Endl manipulator
USING '\n' ...
Line 1
Line 2
Line 3
USING end ...
Line 1
Line 2
Line 3
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
setw() and setfill() manipulators
setw manipulator sets the width of the filed assigned
for the output.
The field width determines the minimum number of
characters to be written in some output
representations. If the standard width of the
representation is shorter than the field width, the
representation is padded with fill characters (using
setfill).
setfill character is used in output insertion operations
to fill spaces when results have to be padded to the
field width.
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
setw() and setfill() manipulators
Syntax
setw([number_of_characters]);
setfill([character]);
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
setw() and setfill() manipulators
Syntax
setw([number_of_characters]);
setfill([character]);
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
Output
Syntax
setw([number_of_characters]);
setfill([character]);
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
setf() and setprecision() manipulator
setprecision manipulator sets the total
number of digits to be displayed, when
floating point numbers are printed.
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
The setprecision Manipulator is
used with floating point numbers.
It is used to set the number of digits
printed to the right of the decimal
point.
This may be used in two forms:
- Fixed
- Scientific
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
Syntax:
setprecision([number_of_digits]);
Learners Support Publications www.lsp4you.com
Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects, for examp
Syntax:
setprecision([number_of_digits]);
Example:
cout<<setprecision(5)<<1234.537;
// output will be : 1234.5
Learners Support Publications www.lsp4you.com
Learners Support Publications www.lsp4you.com
Integral Stream Base: dec, oct, hex and
setbase
oct, hex, or dec:
change base of which integers are interpreted from the stream.
Example:
int n = 15;
cout << hex << n;
prints "F"
setbase:
changes base of integer output
load <iomanip>
Accepts an integer argument (10, 8, or 16)
cout << setbase(16) << n;
parameterized stream manipulator - takes an argument
Learners Support Publications www.lsp4you.com
Summary of Manipulators
Learners Support Publications www.lsp4you.com
MCQ
1. _____ manipulator is used to Terminate a line and
flushes the buffer.
a) endl
b) “\n”
c) “-”
.
2. _____ is used to set the number of digits printed to the right
of the decimal point.
a) setwidth
b) setprecision
c) setbase
d) All the above
Learners Support Publications www.lsp4you.com
MCQ
3. ___manipulator sets the width of the filed assigned for
the output.
a) endl
b) setw
c) Setfill
.4. _____ change base of which integers are interpreted from
the stream
a) setwidth
b) setprecision
c) setbase
d) All the above
Learners Support Publications www.lsp4you.com
MCQ
5. __________character is used in output insertion
operations to fill spaces when results have to be padded to
the field width.
a) endl
b) setw
c) Setfill
d) setbase
.
Learners Support Publications www.lsp4you.com
INTERACTION
&
QUERIES.
Learners Support Publications www.lsp4you.com