Search Here

C++ program to overload << and >> Operators

/* C++ program to overload << and >> operators  */

#include <iostream> 

using namespace std;
class location
{
    private:double longitude;
            double latitude;
    public: location();
            location(location &obj);
            ~location();
            friend ostream &operator<<(ostream &stream,location L);
            friend istream &operator>>(istream &stream,location &L);
};

location :: location() 
{
}

location :: location(location &obj)
{
    longitude =obj. longitude;
    latitude =obj. latitude;
}

location :: ~location()
{
    cout<<"\n destructor called from"<<this<<"...";
}

ostream &operator<<(ostream &stream,location L)
{
    stream<<"\n LONGITUDE:"<<L. longitude;
    stream<<"\n LATITUDE:"<<L. latitude;
}

istream &operator>>(istream &stream,location &L)
{
    cout<<"\n Enter the value of longitude: ";
    stream>>L. longitude;
    cout<<"\n Enter the value of latitude: ";
    stream>>L. latitude;
}

int main()
{
    location l1;
    cin>>l1;
    cout<<l1;
    location l2(l1);
    cout<<l2;
    
    return(0);
}



Overload << and >> Operators
Output

No comments:

Post a Comment