Overloading of io stream operators
-
Upload
saravanan-gopalakrishnan -
Category
Education
-
view
17 -
download
2
Transcript of Overloading of io stream operators
OVERLOADING OF IOSTREAM
OPERATORS
INTRODUCTION
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>
IOSTREAM
IOSTREAM OPERATORS
The iostream class can handle both input and output, allowing bidirectional I/O.
There are two iostream operators are available. They are,
ISTREAM (>>) OSTREAM (<<)
IOSTREAM OPERATORS
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.
ISTREAM OPERATOR (>>)
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.
OSTREAM OPERATOR (<<)
OVERLOADING OF IOSTREAM OPERATORS
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.
OVERLOADING OF 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
Contd,
Declaration,
friend ostream &operator <<(ostream &out, const classType &x) ;
Function Definition,
ostream &operator <<(ostream &out, const calssType &x) {
return out; }
PROTOTYPE (using FRIEND Function)
Using rational class,
Declarationfriend ostream &operator <<(ostream &out, rational
&d);
Definition ostream &operator <<(ostream &out, const rational
&d) {
out<<“\nNumerator:<<d.nr<<“\n”<<“Denominator: ”<<d.dr;
return out; }
EXAMPLE
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.
OVERLOADING OF ISTREAM
It must have two arguments, a reference to istream (it can be of any
name) an reference object to the class.
Contd,
Declaration,
friend istream& operator >>(istream &in, classType &x) ;
Function Definition,
istream& operator>>(istream &in ,calssType &x) {
return in; }
PROTOTYPE (using friend function)
Using rational class,
Declarationfriend 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; }
EXAMPLE
PROGRAM(Using FRIEND Function)
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;}
OUTPUT
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.
OVERLOADING 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.
CONCLUSION
http://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm
http://www.learncpp.com/cpptutorial/31inputandoutputiostreams/
REFERENCES
THANK YOU..