Consider the following C++ code such that I took it from "Sololearn". I'm a beginner and I would be grateful if any one could answer. It is on operator-overloading topic.
class MyClass {
public:
int var;
MyClass() {}
MyClass(int a)
: var(a) { }
MyClass operator+(MyClass &obj) {
MyClass res;
res.var= this->var+obj.var;
return res;
}
};
When they use it in main environment they use the following code
int main() {
MyClass obj1(12), obj2(55);
MyClass res = obj1+obj2;
cout << res.var;
}
My questions:
Why does MyClass has two constructors? The most important question for me is that "operator+" has been defined like a function but when we use it in main its usage is not like a function. Why? Another different question is that is it true to say that the + used in this line MyClass res = obj1+obj2; is from the Operator+?