Operator overloading in C++
Operator overloading:-
- Operator overloading is a syntactic sugar.
- It is the type of compile time polymorphism.
- Overloaded operators are used for class objects or other data members other than basic data types.
- When we overload an operator we do not have to call explicitly that overloaded function.
- It is a rule that,
1) Do not change the meaning of previously defined operator (but we can change the meaning).
2) Do not change precedence and associatively of that operator.
- We can provide an overloaded function of an operator as a member function or a friend function.
e.g.
class demo
{
public:
int i;
int operator +(demo &d)
{
return i+d.i;
}
demo d1,d2;
int no;
no = d1 + d2;
d1.+(d2);
int operator -(demo &d)
{
return i-d.i;
}
int operator *(demo &d)
{
return i*d.i;
}
int operator /(demo &d)
{
return i/d.i;
}
};
int main()
{
demo d1;
demo d2;
cout<<d1+d2;
cout<<d1-d2;
cout<<d1*d2;
cout<<d1/d2;
}
- In operator overloading,
d1+d2 is replaced as
d1.+(d2);
- In this, ‘+’ operator, 1st operand is considered as a caller object and 2nd operand is considered as a parameter.
d1-d2 is replaced as
d1.-(d2);
d1*d2 is replaced as
d1.*(d2);
d1/d2 is replaced as
d1./(d2);
- When to provide an operator overloaded function as friend function:
1) In case of binary operator, if both the operands are of different class object then provide a friend function.
e.g.
class A;
class B
{
public:
int i;
A()
{
i=10;
}
friend int operator +(A &a,B &b);
friend int operator +(A &b,B &a);
};
class A
{
public:
int j;
B()
{
j=20;
}
friend int operator +(A &a,B &b);
friend int operator +(A &b,B &a);
};
int operator +(A &a,B &b)
{
return a.i+b.j;
}
int operator +(B &b, A &a)
{
return b.j+a.i;
}
int main()
{
A a;
B b;
cout<<a+b;
cout<<b+a;
}
- Provide a definition for overloaded operator ‘+’ which accepting object of a same class.
e.g.
int operator +(A &a1,A &a2)
{
return a1.j+a2.j;
}
int operator +(B &b1,B &b2)
{
return b1.i+b2.i;
}
2) In case of a binary operator if the 1st operand is not a type of an object (it is not an invoker) in that case we have to provide a friend function.
e.g.
class demo
{
public:
int i,j;
demo()
{
i=10;
j=20;
}
friend int operator +(int x,demo &d);
};
int operator +(int x,demo d)
{
return x+d.i+d.j;
}
int main()
{
demo d;
cout<<2+d;
return 0;
}
- If we want to provide a definition d+7 then we can provide operator overloaded function as a member function, because, in this case an invoker is of an object type.
- Definition for that member function is,
int operator +(int x)
{
return x+i+j;
}
Overloading of increment and decrement operator:-
- Overloading for this operator is different than normal overloading.
- Because, both the increment and decrement operator can be worked as pre and post.
- Due to which we have to write a function which distinguishes difference between pre-increment and post-increment or pre decrement and post-decrement.
- To provide this function, the compiler provides a different mechanism like a hidden argument which is not passed by the caller but which is used for distinguishing between pre and post operator.
e.g.
class demo
{
public;
int i;
demo()
{
i=10;
}
friend int operator ++(demo &d); // pre-increment
friend int operator ++(demo &d,int dummy); // post-increment
friend int operator –(demo &d); // pre-decrement
friend int operator –(demo &d,int dummy); // post-decrement
};
int operator ++(demo &d)
{
d.i++;
return d.i;
} // this is for pre-increment
int operator ++(demo &d,int dummy)
{
int temp;
temp=d.i;
d.i++;
return temp;
} // this is for post-increment
demo operator ++(demo &d,int dummy)
{
demo temp;
temp.i=d.i;
d.i++;
return temp;
} // this is for post-increment
int operator –(demo &d)
{
return d.i;
}
int operator –(demo &d,int dummy)
{
int no=d.i;
d.i–;
return no;
}
For more reading about technology news in singapore and seo to online marketing do view more about other pages.