Saturday, October 22, 2016

Overloading Of Input/Output Operators of C++ :)

Tags

We know that in overloading we make the operator work on user defined datatype(Objects) same as on Built in Datatypes
So the Input Operator ">>"is overloaded for working on Objects and Output Operator "<<" also overloaded.

Following Program Illustrates the Concept:

#include<iostream>
using namespace std;
class time
{   int hours;
    int minutes;
    public:
    friend istream& operator>>(istream &,time &);  //for input in objects
    friend ostream& operator<<(ostream &,time &);  // for output from objects
};
istream& operator >>(istream &c,time &b)    //istream& for use in nested form like cin>>x>>y;
{   cout<<"\nEnter Time In Hours And Minutes\n";
c>>b.hours>>b.minutes;
return c;
}
ostream& operator <<(ostream &d,time &b)
{   cout<<"\nEntered Time";
d<<"\nHours: "<<b.hours<<" Minutes: "<<b.minutes;
return d;
}
main()
{    time t,n;
cin>>t>>n;
cout<<t<<n;

}

Output:





Special Thanks To Ms.Naina For This Program