Overloading of io stream operators

26
OVERLOADING OF IOSTREAM OPERATORS

Transcript of Overloading of io stream operators

Page 1: Overloading of io stream operators

OVERLOADING OF IOSTREAM

OPERATORS

Page 2: Overloading of io stream operators

INTRODUCTION

Page 3: Overloading of io stream operators

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

Page 4: Overloading of io stream operators

IOSTREAM OPERATORS

Page 5: Overloading of io stream 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

Page 6: Overloading of io stream 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 (>>)

Page 7: Overloading of io stream operators

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 (<<)

Page 8: Overloading of io stream operators

OVERLOADING OF IOSTREAM OPERATORS

Page 9: Overloading of io stream 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

Page 10: Overloading of io stream operators

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,

Page 11: Overloading of io stream operators

Declaration,

friend ostream &operator <<(ostream &out, const classType &x) ;

Function Definition,

ostream &operator <<(ostream &out, const calssType &x) {

return out; }

PROTOTYPE (using FRIEND Function)

Page 12: Overloading of io stream operators

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

Page 13: Overloading of io stream operators

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

Page 14: Overloading of io stream operators

It must have two arguments, a reference to istream (it can be of any

name) an reference object to the class.

Contd,

Page 15: Overloading of io stream operators

Declaration,

friend istream& operator >>(istream &in, classType &x) ;

Function Definition,

istream& operator>>(istream &in ,calssType &x) {

return in; }

PROTOTYPE (using friend function)

Page 16: Overloading of io stream operators

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

Page 17: Overloading of io stream operators

PROGRAM(Using FRIEND Function)

Page 18: Overloading of io stream operators

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; }

Page 19: Overloading of io stream operators

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; }};

Page 20: Overloading of io stream operators

//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;}*/

Page 21: Overloading of io stream operators

int main(){ rational r1,r2; cin>>r1; cin>>r2; rational r3=r1.addRational(r2); cout<<r3;}

Page 22: Overloading of io stream operators

OUTPUT

Page 23: Overloading of io stream operators

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

Page 24: Overloading of io stream operators

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

Page 26: Overloading of io stream operators

THANK YOU..