 In C++ programming language, Input/output
library refers to a family of class templates
and supporting functions in the C++
Standard Library that implement stream-
based input/output capabilities.
 These class templates are available in
#include<iostream>
 The iostream class can handle both input and
output, allowing bidirectional I/O.
 There are two iostream operators are
available. They are,
 ISTREAM (>>)
 OSTREAM (<<)
 The istream class is the primary class used
when dealing with input streams.
 For input streams, the extraction operator
(>>) is used.
 when the user presses a key on the keyboard,
the key code is placed in an input stream.
 The ostream class is the primary class used
for dealing with output streams.
 For output streams, the insertion operator
(<<) is used to put values in the output
stream.
 We insert our values into the stream, and the
data consumer (eg. monitor) uses them.
 The ostream operator (i.e) insertion operator
(<<) can be overloaded.
 It is both a bitwise left-shift operator and an
output operator, it is called as Insertion
operator when it is used for output.
 It acts as an output operator only when cout
(output stream object) appears on the left
side.
 The preceding function, called operator <<(),
returns a reference to ostream.
 C++ overloads the << operator to work with
the built-in data types, we also may overload
the << operator to work with our own
classes.
 It must have two arguments,
 a reference to ostream (it can be of any
name)
 an reference object to the class
Declaration,
friend ostream &operator <<(ostream &out,
const classType &x) ;
Function Definition,
ostream &operator <<(ostream &out, const
calssType &x)
{
return out;
}
 Using rational class,
Declaration
friend ostream &operator <<(ostream &out, rational
&d);
Definition
ostream &operator <<(ostream &out, const rational
&d)
{
out<<“nNumerator:<<d.nr<<“n”<<“Denomina
tor: ”<<d.dr;
return out;
}
 The istream operator (i.e) extraction operator
(>>) can also be overloaded.
 It is both a bitwise right-shift operator and an
input operator, it is called as Extraction
operator when it is used for input.
 It acts as an input operator only when
cin(input stream object) appears on the left
side.
 The preceding function, called operator>>(),
returns a reference to istream.
 It must have two arguments,
 a reference to istream (it can be of any
name)
 an reference object to the class.
Declaration,
friend istream& operator >>(istream &in,
classType &x) ;
Function Definition,
istream& operator>>(istream &in ,calssType &x)
{
return in;
}
 Using rational class,
Declaration
friend istream& operator >>(istream &in, rational
&d);
Definition
istream& operator >>(istream &in, rational &d)
{
cout<<"nEnter the numerator and
denominator”;
in>>d.nr>>d.dr;
return in;
}
using namespace std;
#include<iostream>
class rational
{
int nr,dr;
public:
rational addRational(rational r1)
{
rational temp;
temp.nr=nr+r1.nr;
temp.dr=dr;
return temp;
}
friend istream &operator >>(istream &in,rational
&d)
{
cout<<"nEnter the nr and dr: ";
in>>d.nr>>d.dr;
return in;
}
friend ostream &operator <<(ostream &out,const
rational &d )
{
out<<"nNr: "<<d.nr<<"nDr:
"<<d.dr;
return out;
}
};
//defined outside the class (optional)
/*istream &operator >>(istream &in,rational &d)
{
cout<<"nEnter the value";
in>>d.nr>>d.dr;
return in;
}
ostream &operator <<(ostream &out,const rational
&d)
{
out<<"nr: "<<d.nr<<"Dr: "<<d.dr;
return out;
}*/
int main()
{
rational r1,r2;
cin>>r1;
cin>>r2;
rational r3=r1.addRational(r2);
cout<<r3;
}
 We can’t overload the iostream operator
using the member function.
 For overloading binary operator we need two
arguments has to passed.
 But here we can’t able to pass the istream or
ostream reference object (i.e) cin or cout
objects are not passed.
 So we can’t able to overload the cin, cout
using member function.
 If we want to overload iostream by member
function we want to add definions into header
files and library files for corresponding
objects.
 Thus, the overloading of iostream operators
can be achieved only through friend function.
 http://www.tutorialspoint.com/cplusplus/inp
ut_output_operators_overloading.htm
 http://www.learncpp.com/cpptutorial/31inpu
tandoutputiostreams/
Overloading of io stream operators

Overloading of io stream operators

  • 3.
     In C++programming language, Input/output library refers to a family of class templates and supporting functions in the C++ Standard Library that implement stream- based input/output capabilities.  These class templates are available in #include<iostream>
  • 5.
     The iostreamclass can handle both input and output, allowing bidirectional I/O.  There are two iostream operators are available. They are,  ISTREAM (>>)  OSTREAM (<<)
  • 6.
     The istreamclass is the primary class used when dealing with input streams.  For input streams, the extraction operator (>>) is used.  when the user presses a key on the keyboard, the key code is placed in an input stream.
  • 7.
     The ostreamclass is the primary class used for dealing with output streams.  For output streams, the insertion operator (<<) is used to put values in the output stream.  We insert our values into the stream, and the data consumer (eg. monitor) uses them.
  • 9.
     The ostreamoperator (i.e) insertion operator (<<) can be overloaded.  It is both a bitwise left-shift operator and an output operator, it is called as Insertion operator when it is used for output.  It acts as an output operator only when cout (output stream object) appears on the left side.  The preceding function, called operator <<(), returns a reference to ostream.
  • 10.
     C++ overloadsthe << operator to work with the built-in data types, we also may overload the << operator to work with our own classes.  It must have two arguments,  a reference to ostream (it can be of any name)  an reference object to the class
  • 11.
    Declaration, friend ostream &operator<<(ostream &out, const classType &x) ; Function Definition, ostream &operator <<(ostream &out, const calssType &x) { return out; }
  • 12.
     Using rationalclass, Declaration friend ostream &operator <<(ostream &out, rational &d); Definition ostream &operator <<(ostream &out, const rational &d) { out<<“nNumerator:<<d.nr<<“n”<<“Denomina tor: ”<<d.dr; return out; }
  • 13.
     The istreamoperator (i.e) extraction operator (>>) can also be overloaded.  It is both a bitwise right-shift operator and an input operator, it is called as Extraction operator when it is used for input.  It acts as an input operator only when cin(input stream object) appears on the left side.  The preceding function, called operator>>(), returns a reference to istream.
  • 14.
     It musthave two arguments,  a reference to istream (it can be of any name)  an reference object to the class.
  • 15.
    Declaration, friend istream& operator>>(istream &in, classType &x) ; Function Definition, istream& operator>>(istream &in ,calssType &x) { return in; }
  • 16.
     Using rationalclass, Declaration friend istream& operator >>(istream &in, rational &d); Definition istream& operator >>(istream &in, rational &d) { cout<<"nEnter the numerator and denominator”; in>>d.nr>>d.dr; return in; }
  • 18.
    using namespace std; #include<iostream> classrational { int nr,dr; public: rational addRational(rational r1) { rational temp; temp.nr=nr+r1.nr; temp.dr=dr; return temp; }
  • 19.
    friend istream &operator>>(istream &in,rational &d) { cout<<"nEnter the nr and dr: "; in>>d.nr>>d.dr; return in; } friend ostream &operator <<(ostream &out,const rational &d ) { out<<"nNr: "<<d.nr<<"nDr: "<<d.dr; return out; } };
  • 20.
    //defined outside theclass (optional) /*istream &operator >>(istream &in,rational &d) { cout<<"nEnter the value"; in>>d.nr>>d.dr; return in; } ostream &operator <<(ostream &out,const rational &d) { out<<"nr: "<<d.nr<<"Dr: "<<d.dr; return out; }*/
  • 21.
  • 23.
     We can’toverload the iostream operator using the member function.  For overloading binary operator we need two arguments has to passed.  But here we can’t able to pass the istream or ostream reference object (i.e) cin or cout objects are not passed.  So we can’t able to overload the cin, cout using member function.
  • 24.
     If wewant to overload iostream by member function we want to add definions into header files and library files for corresponding objects.  Thus, the overloading of iostream operators can be achieved only through friend function.
  • 25.

Editor's Notes

  • #4 IOStream- Channel or medium for input and output operations.