+ 2

How to easily visualize the operator overloading ?

In c++ language.

30th Oct 2019, 6:48 AM
Somesh
Somesh - avatar
4 Réponses
+ 3
To illustrate what this is even about, let's look at two existing types: #include <iostream> #include <string> using namespace std; int main() { int a=1, b=2; string A="1", B="2"; cout << a+b << endl; cout << A+B; return 0; } If you run the code, you'll see that something else happens, although you're using the same operator +: The ints are arithmetically added, the strings are glued together instead. When you define your own types writing classes, the operators won't work on instances of them. Let's say you want to write a dietary cooking app and add up ingredients. You want, that the calories of your incredients are added, and also the carbs, fats and proteins. By defining the operators for your own type, you can accomplish that Food + Food will lead to the result you expect.
30th Oct 2019, 10:57 AM
HonFu
HonFu - avatar
+ 2
How do you mean, visualize?
30th Oct 2019, 8:32 AM
HonFu
HonFu - avatar
+ 1
Like a real time example.
30th Oct 2019, 8:41 AM
Somesh
Somesh - avatar
+ 1
Thanks dude...
30th Oct 2019, 11:17 AM
Somesh
Somesh - avatar