Write a program in C++ for Unary operator overloading

#include<iostream.h>
class load
{
         int m,n;
         public:
         load()
        { 

                 m=0, n=0;}
                 load(int a, int b)
                { 
                          m=a;
                          n=b; 
                 }
                 void display()
                { 
                         cout<<m<<"\n";
                         cout<<n<<"\n"; 
                }
                void operator++();
};
void load ::operator++()
{
         m++;
         n++;
}
void main()
{
        load x1(100,200);
        x1++;
        ++x1;
        x1.display();
}



Sample Output:




If the overload increment function declares as friend function then it will take a single argument.

friend void operator++(load &); //function declaration
void operator++(load &x)
{
        x.m++;
        x.n++;
}



Post a Comment

Previous Post Next Post