Saturday, February 2, 2013

Operator overloading in C++


Operator Overloading in C++:
It is a special feature of C++. C++ allows operators to be use as function name and allow user to redefine them with new functionality.
Return_type classname::operator op()
{
            statements
}

Unary operator overloading:
When we have an operator that works on single operand such type of overloading is known as unary operator overloading.
class minus
{
            private:
                        int a,b,c;
            public:
                        void get()
                        {
                                    cout<<”Enter value”;                   
                                    cin>>a>>b>>c;
                        }



                        void get()
                        {
                                    cout<<”Minus”;                 
                                    cout<<a<<endl;                 
                                    cout<<b<<endl;                 
                                    cout<<c<<endl;                 
                        }
                        void operator -()
                        {
                                    a=-a;
                                    b=-b;
                                    c=-c;
                        }
};
Void main()
{
            minus m;
            m.get();
            m.display();
            -m; // m.operator-();
            m.display();
}

Binary operator overloading:
When we have an operator that works on two operands such type of overloading is known as binary operator overloading.
class sum
{
            private:
                        Int x,y;
            pubic:
                        sum()
                        {
                                    x=10;
                                    y=10;
                        }
                        void display()
                        {
                                    cout<<”value of x is ”<<x;
                                    cout<<”value of y is ”<<y;
                        }
                        sum operator +(sum obj)
                        {
                                    Sum t;
                                    t.x=obj.x+x;
                                    t.y=obj.y+y;
                                    return t;
                        }
};
void main()
{
            sum s1,s2,s3;
            s1=s2+s3;  // s1=s2.operator +(s3);
            s1.display();
            s2.display();
            s3.display();
            getch();
}


Note: Operators that can’t be overloaded
sizeof, ., .*, ::, ?:

Rules for Operator overloading:
·        Existing operator can be overloaded new operator cannot be overloaded.
·        The operator must have at least one operand i.e. a user defined type.
·        We cannot change the basic meaning of an operator.
·        Binary operator overloaded through a member function take one argument, those which are overloaded through a friend function take two argument.
·        Unary operator overloaded by mean of a member function take no explicit argument and returns no explicit value.
·        Those which are overloaded by the means of friend function take one reference argument.

0 comments:

Post a Comment

Powered by Blogger.