Subscribe Us

Responsive Advertisement

Advertisement

Overloading Short Hand Operators (+= and -=) | Operator Function as Friend Function and Member function

 


Overloading Short Hand Operators (+= and -=) | Operator Function as Friend Function and Member function



#include<bits/stdc++.h>

using namespace std;

class increment

{

    int val;

public :

    increment()

    {

        val=0;

    }

    increment(int x)

    {

        val=x;


    }

    void operator+=(increment y)

    {

        val=val+y.val;


    }

    void display()

    {

        cout<<"the value "<<val<<endl;

    }

    friend void operator-=(increment &m,int n)

    {

        m.val-=n;

    }


};


int main()

{

    increment m(50),x(10);

     int z;

     z=40;

     m+=x;

    m.display();

    m-=z;

    m.display();


    return 0;

}


Post a Comment

0 Comments