If we have three instances of a class SignedInt with properties Value and Sign, but no...
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
If we have three instances of a class SignedInt with properties Value and Sign, but no operator methods defined, is the statement C = A + B; valid?
Show answer & explanation
The + operator is not defined for custom classes by default. Without an explicit overload of operator+, the compiler cannot perform addition on SignedInt objects and will raise a compilation error. The copy constructor and assignment operator (implicitly generated) handle object copying, but they do not define what addition means for the class.
Step-by-step Derivation:
In C++, built-in operators like +, -, * only work natively on primitive types (int, float, double, etc.). For user-defined classes, you must explicitly overload the desired operator. Here's why each option fails: (B) Getter/setter functions don't automatically enable the + operator—they're just regular member functions. (C) The copy constructor is called during object assignment/copying, not during addition. (D) The copy constructor alone does not enable the + operation; you need operator+ overload. Therefore, the statement C = A + B; will not compile without overloading operator+ for SignedInt.