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

0% found this document useful (0 votes)
6 views13 pages

Streams in C++

The document provides an overview of streams in C++, explaining the concepts of source and destination streams, as well as pre-defined console streams like cin and cout. It details formatted input/output functions, manipulators, and user-defined manipulators for customizing output. Examples are included to illustrate the usage of these concepts in C++ programming.

Uploaded by

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

Streams in C++

The document provides an overview of streams in C++, explaining the concepts of source and destination streams, as well as pre-defined console streams like cin and cout. It details formatted input/output functions, manipulators, and user-defined manipulators for customizing output. Examples are included to illustrate the usage of these concepts in C++ programming.

Uploaded by

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

STREAMS IN C++

1 logo 公司名

• The stream is an intermediator between I/O devices and the
user.
• Stream is flow of data in bytes in sequence.

• Source Stream: When data is received from input devices in


sequence
then it is called as source stream
• Destination Stream: when the data is passed to output devices
then it is
called as destination stream.

2 logo 公司名
称 2
PRE-DEFINED CONSOLE STREAMS:
• C++ contains Cin and Cout predefined streams that opens
automatically
when a program begins its execution.
• Cin represents the input stream connected to the standard
input device.
• Cout represents the output stream connected to standard
output device.

1. cout

2. cin
3 logo 公司名
称 3
The Standard Output Stream
(cout):

cou Standard output, usually screen, corresponding to


t iostream in C++. It passes data to output devices such
as monitor and printers. Thus, it controls output.

E-g;
#include<iostream.h>
void main()
{
cout<<“Welcome”;
}

Output:
Welcome

4 logo 公司名
称 4
The Standard Input Stream
(Cin)
cin Standard input, usually keyboards, corresponding to
iostream in C++. It handles input from input devices
usually from keyboard
E-g;
#include<iostream.h>
void main()
{
int a,b,c;
cout<<“\n Enter Two Numbers to sum:”;
cin>>a>>b
c=a+b;
cout<<“\n The Sum is:”<<c;
}

Output:
Enter Two Numbers to sum:
10
42
The Sum is: 52 5 logo 公司名
称 5
Formatted I/
• O:C++ provides various formatted console I/O functions for
formatting the output. They are of three types.

ios class functions

Manipulators

User-Defined
Manipulators
B.1 ios class functions:
• width() - specify the output width.
• precision() - specify No.of digits after decimal point.
• fill() - specify the fill character for unused portion of
width.
• setf() - set the ios flags(dec,oct,hex,etc,.)
• unsetf() - clear the flags 6specified logo 公司名
称 6
1) width():-

Syntax: width(w);
E-g;
Cout<<width(6);
Cout<<2014;
Output:
2 0 1 4

2) precision():-

Syntax: precision(n);
E-g;
Cout<<width(6);
Cout<<precision(2);
Cout<<21.412903;
Output:
2 1 . 4 1

7 logo 公司名
称 7
3) fill()

Syntax: fill(ch);
E-g;
Cout<<fill(‘*’);
Cout.width(6)
Cout<<2014;
Output:
* * 2 0 1 4

4) setf()

Syntax: setf(arg1,arg2);
arg1→formatting flag; arg2→bit-
field
E-g;
cout.setf(ios::left,ios::adjustfield);
cout.width(12);
cout<<“TOTAL MARK”;
T O T A L M 8A R K logo 公司名
Output: 称 8
B.2 Manipulators:
• Manipulators are operators that are used to format the
display.
• To access these manipulators, the file iomanip should
be included
Manipuators Equivalent ios
in the program.
functions
i.e) #include<iomanip.h>
setw() width()
setprecision( precision()
)
setfill() fill()
setiosflags() setf()
resetiosflags unsetf()
()

• endl is a manipulator operator-used to insert new line


a = 1 2
• E-g; int a=12;Cout<<setw(3)<<“a=”<<setw(5)<<a<<endl;
9 logo 公司名
称 9
B.3 Custom/User-Defined Manipulators:
• The programmer can also define his/her own
manipulator
according to the requirements of the program.
• Syntax: ostream & manipulator_name(ostream
& obj)
{
//body of the function
return obj;
}

E-g: ostream & tab (ostream & o)


{
o <<"\t";
return o;
}
void main( )
{
cout <<1<<tab<<2 <<tab<<3;
}
10 logo 公司名
OUTPUT 称 10
› Code Explanation- a function tab that
overloads the insertion operator (<<) to
insert a tab character ("\t") into an output
stream.
› ostream &tab(ostream & o): This is a
function that takes an ostream reference
(like cout) as a parameter and returns a
reference to the same stream after adding a
tab character ("\t").
› Inside the function, the tab character (\t) is
inserted into the stream o using the
standard << operator, and then the function
returns the stream o, which nowlogocontains
公司名
the tab. 称
› In the main function, the statement
cout << 1 << tab << 2 << tab << 3; is
executed.
› First, cout << 1 outputs the number 1
to the console.
› Then tab is invoked. Since tab is a
function that overloads the <<
operator, it inserts a tab character (\t)
into the output stream.
› After that, cout << 2 outputs the
number 2, followed by another tab
inserted by tab. logo 公司名

Thank You

13

You might also like