Features Of Iostream.h
It is will known that C++ is a tool for Object oriented
programming (OOP). In order to handle the various object oriented features of a
program, it is essential to have robust input and output facilities. Most of
the object oriented programs handle three items: objects, message passing and
optional parameters with the massage from the outside world. C++ provides a new
way to perform the input and output operations called iostream method.
The standard header
file input and output stream (iostream.h) contains
a set of small and specific general purpose functions for handling input and
output data. The I/O stream is a sequence of following characters written for
the screen display or read from the keyboard. The standard input and output
operations in C++ are normally performed by using the I/O stream as cin for input and cout for output. The Borland C++ uses iostream.h for most stream related operations. The following are
the list of various stream related header files to be included for input and
output processing for different versions of the C++ compiler.
C++ compilers
|
Header Files To Be
Included
|
Borland C++
Turbo C++
Microsoft Visual C++
Zortech C++
UNIX C++ (AT&T)
|
iostream . h, ios .h, iomanip . h
iostream . h, ios .h,
iomanip . h
ios . h, istream . h,
ostream . h and streamb . h
stream . h
stream . h
|
In this book,
discussions on stream operations are limited only to the Borland C++ and Turbo
C++ compilers. The C programming language comes with an extensive library of
functions for handling input and output but it cannot add new format specifier
to printf/ scanf functions. C++ supports input and output of
numbers, characters and character strings more conveniently then C.
(i) Ios.h The ‘ios’ correspond
to input and output streams (hence io).
The definitions based on ‘istream’ correspond
to input streams and those based on
‘ostream’ to output streams. C++
allows three types of stream classes, namely,
istream
ostream
iostream
(ii) Istream The istream consists of input
functions to read a streams of characters from the keyboard.
(iii) Ostream The
ostream consists of output functions to write a character onto the screen.
(iv) 1Ostream the
iostream supports both input/output stream of functions to read a stream of
characters form the keyboard and to display a stream of objects onto the video
screen.
In fact, C++ dopes
not have built-in input and output functions for handling input and output of
data from the outside world, but it supports different kind of stream related
header files for providing these facilities. The stream library is a hierarchy
of classes. The streambuf class is the basis of all streams. It defines
the basic characteristics of buffers
that hold characters for input and output. The ios
class is derived from streambuf. It defines the basic formatting and
error control capabilities used in streambuf. The ios is a virtual base class for the classes istream (input stream) and ostream (output stream). The iostream (input/output stream) class is
derived from both istream and ostream.
Keyboard and Screen I/O
(a) Cout The cout
is used to display an object onto the standard device, normally the video
screen. The insertion operator (the double less the sign <<) is used
along with the cout stream.
The general
syntax of the cout stream is,
Cout << variable 1 <<
variable 2 <<.....<< variable n;
For example, the following are certain valid cout member function usage in
C++
(1)
int x = 123;
float y
= - 45 . 67;
cout << x << y ;
The output will be 123 - 45 . 67
(3) Use of different data types freely on one line is
permitted.
int x = 123;
float y
= - 45 . 67;
cout <<
“ x = << x << ‘\t’ << “ y = “
<< y;
The output will be
x =123
y = - 45 . 67
(4) The cout
stream is used to display different data types.
/ / cout is used to display
different data types
#includ <iostream.h>
Void main ( )
{
int num = 3 ;
cout
<< “ number = “ << num << “\n”;
char
ch = ‘a’;
cout
<< “ character = “ << ch << ‘\n’;
float fa = - 34 . 45;
cout
<< “ real number = “
<<fa << “\n”;
}
Output of the above
program
number = 3
character = a
real number = - 34 . 450001
(b) Cin The cin is used to read a number, a
character or a string of characters from a standard input device, normally the
keyboard. The extraction operator (the double greater then sign >>) is
use along with the cin operator.
The general
syntax of the cin is
cin >> variable 1 >> variable 2
>> …variable n;
For example, the following are certain valid cin member function usage in
C++.
int a,b;
float f1, f2;
char c1,c2;
(1)
cin
>> a >> b >> f1 >> f2 >> c1 >> c2 ;
(2)
cin >> a >> b:
cin >> f1 >> f2 ;
cin >> c1 >> c2 ;
(3)
cout << “ enter two integers \n” ;
cin >> a >> b ;
cout << “ enter two floating point
numbers \n” ;
cin >> f1 >> f2;
cout << “ enter any two characters
\n” ;
cin >> c1 >> c2 ;
PROGRAM
A program to read any two numbers through the keyword and to
perform simple arithmetic operations (i.e.; addition, subtraction,
multiplication and division) and display the result using cin and cout
functions.
/ / using cout stream
#includ <iostream.h>
Void main (void)
{‘
int a, b, sum, sub, mul, div;
cout << “ Enter any two numbers”
<< endl;
cin >> a >> b;
sum = a+b;
sub = a-b
mul = a*b;
div = a/b;
cout << ‘ a = “ << a << “
b = “ << b << “ sum = “;
cout << sum << endl;
cout << ‘ a = “ << a << “
b = “ << b << “ sub = “;
cout << sub << endl;
cout << ‘ a = “ << a << “
b = “ << b << “ sum = “;
cout << mul << endl;
cout << ‘ a = “ << a << “
b = “ << b << “ sum = “;
cout << div << endl;
}
Output of the above
program
Enter any two numbers
10 20
a = 10 b = 20 sum = 30
a = 10 b = 20 sub = -10
a = 10 b = 20 mul = 200
a = 10 b = 20 div = 0
(c) Cerr and clog In addition to the function cout, C++ provides other functions of the class ostream called cerr and clog.
They are used to redirect error messages to other devices. The output of cerr
is unbuffered, while the output of clog
is buffered. The use of the above functions are illustrated below.
/ / using cerr and clog
#include <iostream.h>
void main ( )
{
cout << “ My name is Komputer !
using cout \n”;
cerr
<< “ many greetings to you ! using cerr \n”;
clog << “ helloo, computer ! using
clog \n”;
}
Summary of C++ I/O streams
Stream
|
Meaning
|
cin
cout
cerr
clog
|
Keyboard input (stdin)
Screen output (stdout)
Standard error device output (stderr)
Buffered output of standard error (stderr)
|
No comments:
Post a Comment