212k views
3 votes
Explain by details operator overloading in C++ with example

User Alizx
by
7.0k points

1 Answer

4 votes

Answer:

Operator overloading is used to extend the functionality of an operator like +(addition),-(subtraction),/(Division),*(Multiplication).

Syntax for operator overloading inside the class :-

You have to use operator keyword.

Function

return type operator <operator sign> (arguments)

{

---code

}

suppose you want to extend the functionality of ++ operator for any integer so that it increases the value by two.

void operator ++(int &a)

{

a=a+2;

}

So whenever this operator is used with an argument it will increase the value to that argument by 2.

int a=5;

++(a);

a will become 7;

User Nambi
by
6.6k points