Write a program to perform Unary Operator Overloading

/*
    Overloading a Unary Operator
    To Negate -
*/
#include<iostream>
using namespace std;

class Sample
{
    int a,b;

    public:
            void accept()
            {
                cout<<"\n\n Enter any two number: ";
                cin>>a>>b;
            }

            void display()
            {
                cout<<"\n\n A: "<<a;
                cout<<"\n B: "<<b;
            }

            void operator-();
};

void Sample::operator-()
{
    a=-a;
    b=-b;
}

int main()
{
    Sample S;

    S.accept();

    cout<<"\n\n Before Negate : ";
    S.display();

    -S;

    cout<<"\n\n After Negate : ";
    S.display();

    return 0;
}

Previous
Next Post »