Thursday, May 13, 2021
Home General Decrement operator overloading in C++

Decrement operator overloading in C++

Decrement operator in C++ is used to decrease the value of a variable by 1 or some other fixed interval, normally it is used in loops. Now decrement operator can be overloaded for our self-defined variables or classes in C++, the code is given below:

    //overloading decrement operator 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 decrement operator overloading we get a point variable as an argument and then selected each data member of the point object and decremented it and returned a whole point variable.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....