Rules for overloading an operator:
- The operator being overloaded should have at least one operand of a user-defined data type.
- Overloaded operators must either be a member function or a friend function of the class.
- Overloaded operators must follow the same syntax and precedence rules as the built-in operators.
- Overloaded operators cannot change the number of operands, their types, or their intrinsic meanings.
- Some operators, such as the conditional and the assignment operators, can be overloaded as member functions and as friend functions, but others, such as the scope resolution operator and the conditional operator, can only be overloaded as member functions.
In this program, the "Distance" class has a conversion operator 'operator float()', which allows an object of the "Distance" class to be converted to a float value. The 'd1' object of the "Distance" class is assigned to the "d2" variable of the 'float' data type, which invokes the conversion operator. The "showDist()" function displays the distance in feet and inches, and the "d2" variable is displayed as the distance in meters.
No comments:
Post a Comment
If you have any doubts, please let me know