Increment operator overloading in C++

0
174
Decrement operator overloading in C++
Overloading steam insertion and extraction operators in C++

Increment operator in C++ is used to increase the value of a variable by 1 or some other fixed interval. normally it is used in loops. Now increment operator overloading is used for our self-defined variables or classes in C++, the code is given below:

    //Increment operator overloading 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;
        }
        
        friend point&operator ++(point& a)
        {
            a.x++;
            a.y++;
     
            return a;
        
        }
    };
     
    int main(){
     
        point n;
        cin>>n;
        cout<<n++;
     
    }

In the above code we defined our own point class, we overloaded stream insertion and extraction operators, In the increment operator overloading function we get a point variable as an argument and then selected each data member of the point object and incremented it and returned a whole point variable

Decrement operator overloading in C++
Overloading steam insertion and extraction operators in C++

Leave a Reply