in c++ programming language, input/output · in c++ programming language, input/output library...

26

Upload: others

Post on 21-May-2020

30 views

Category:

Documents


0 download

TRANSCRIPT

In C++ programming language, Input/outputlibrary refers to a family of class templatesand 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 andoutput, allowing bidirectional I/O.

There are two iostream operators areavailable. They are,

ISTREAM (>>)

OSTREAM (<<)

The istream class is the primary class usedwhen 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 usedfor dealing with output streams.

For output streams, the insertion operator(<<) is used to put values in the outputstream.

We insert our values into the stream, and thedata consumer (eg. monitor) uses them.

The ostream operator (i.e) insertion operator(<<) can be overloaded.

It is both a bitwise left-shift operator and anoutput operator, it is called as Insertionoperator when it is used for output.

It acts as an output operator only when cout(output stream object) appears on the leftside.

The preceding function, called operator <<(),returns a reference to ostream.

C++ overloads the << operator to work withthe built-in data types, we also may overloadthe << operator to work with our ownclasses.

It must have two arguments,

a reference to ostream (it can be of anyname)

an reference object to the class

Declaration,

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

Function Definition,

ostream &operator <<(ostream &out, constcalssType &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”<<“Denominator: ”<<d.dr;

return out;

}

The istream operator (i.e) extraction operator(>>) can also be overloaded.

It is both a bitwise right-shift operator and aninput operator, it is called as Extractionoperator when it is used for input.

It acts as an input operator only whencin(input stream object) appears on the leftside.

The preceding function, called operator>>(),returns a reference to istream.

It must have two arguments,

a reference to istream (it can be of anyname)

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,

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;

}

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,constrational &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 operatorusing the member function.

For overloading binary operator we need twoarguments has to passed.

But here we can’t able to pass the istream orostream reference object (i.e) cin or coutobjects are not passed.

So we can’t able to overload the cin, coutusing 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/input_output_operators_overloading.htm

http://www.learncpp.com/cpptutorial/31inputandoutputiostreams/