Overloading steam insertion and extraction operators in C++

0
297

For putting data into the stream and getting data from the stream, the stream insertion and extraction operators are used, Now these operators are already defined for defined datatypes in C++, but for our self-defined datatypes we need to overload these two operators, the code is:

    //overloading stream insertion and extraction operators in C++
    #include<iostream>
    using namespace std;
    class point{
    private:
        int x,y;
     
    public:
        point()
        {
            x =0;
            y = 0;
        
        }
        friend ostream&operator<< (ostream& cin, point& z){
        
            cin<<z.x<<"  "<<z.y<<endl;
            return cin;
        }    
     
        friend istream&operator>>(istream& a, point& z)
        {
        
            a>>z.x;
            a>>z.y;
            return a;
        }
     
    };
     
    int main(){
     
        point n;
     
        cin>>n;
     
        cout<<n;
    } 

In the above code, the steam insertion and extraction operators are overloaded in the overload functions of “<<” and “>>”