0

Plz explain operator overloading with an easy c++ program

i was searching about operator overloading on google and found out that it means redefining an existing operator (built in) ..but when i searched for examples on google most of them were complex and were using concepts like templates that i dont know yet ....if you can explain it with an easy example pls do so...

26th Sep 2018, 3:06 PM
Teddy
Teddy - avatar
1 Answer
+ 1
Uh yeah templating isn't what you want. let's say you have a Square class. you want the symbol "+" to mean add all the sides together for some reason, it's not recommended. But let's say so for our sake. This means you want at least 2 squares, so Square1+Square2. In C++ the syntax is something like: int operator +(&Square){return this->perimeter + Square.perimeter} This means pass in a reference of another square (Square2) and add it to Square1 (that's what "this" is). we then want to return a value because it's addition! That's why we have "int operator".
30th Sep 2018, 12:54 AM
Justin Petluk
Justin Petluk - avatar