+ 2
How to easily visualize the operator overloading ?
In c++ language.
4 odpowiedzi
+ 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.
+ 2
How do you mean, visualize?
+ 1
Like a real time example.
+ 1
Thanks dude...