Unary and Binary Operator Overloading

 Program for Operator Overloading.


      1.     Unary Operator Overloading:


Code:
#include<iostream>
using namespace std;

class INC
{
    int a , b, c;
public:
    INC(int a1 , int b1, int c1)
    {
        a=a1;
        b=b1;
        c=c1;
    }

    void operator ++ ()
    {
       ++a;
       ++b;
       ++c;
    }

    void show()
    {
        cout<<"\nValues after Increment : ";
        cout<<"\na="<<a<<"\nb="<<b<<"\nc="<<c;
    }
};

int main()
{
    INC A(1,2,3) , B(11,22,33);
    ++A;
    A.show();
    ++B;
    B.show();
}


2.     Binary Operator Overloading :


Code:

#include<iostream>
using namespace std;
class Binary
{
    int a;
public:
    Binary(int a1)
    {
        a=a1;
       ;
    }
 int operator + ( Binary b1 )
    {
       int c;
       c=a+b1.a;
       return c;
    }
    int operator - ( Binary b1)
    {
       int c;
       c=a-b1.a;
       return c;
    }
    int operator * ( Binary b1)
    {
       int c;
       c=a*b1.a;
       return c;
    }
    int operator / ( Binary b1)
    {
       int c;
       c=a/b1.a;
       return c;
    }
    void show()
    {
        cout<<"\nValues Entered : ";
        cout<<"\na="<<a;
    }
};
int main()
{
    Binary B(2) , A(11);
    int C;
    A.show();
    B.show();
    cout<<"\n";
    C=A+B;
    cout<<"\nValue after changes :"<<C;
    C=A-B;
    cout<<"\nValue after changes :"<<C;
    C=A*B;
    cout<<"\nValue after changes :"<<C;
    C=A/B;
    cout<<"\nValue after changes :"<<C;
}
             


Share this

Related Posts

Previous
Next Post »

Powered by Blogger.

Popular Posts