C++ program to overload Logical NOT (!) operator

//here, operator function is friend function

#include <iostream>
using namespace std;
class Values
{
    int a,b;
    public:
        Values(int av,int bv)
        {
            a=av;
            b=bv;
        }
        friend int operator!(Values);
//by friend function
};

int operator!(Values v)
{
    if(v.a!=v.b)
        return 1;
    else
        return 0;
}

int main()
{
    Values v1(5,6);
    if(!v1)
//same as operator!(v1);
        cout<<"They are not equal";
    else
        cout<<"They are equal.";
    return 0;
}

No comments:

Post a Comment