Overloaded operators: Difference between revisions

Overloaded operators en FreeBASIC
(Overloaded operators en FreeBASIC)
Line 174:
16
</pre>
 
=={{header|FreeBASIC}}==
Operators can be overloaded by default, so the <code>Overload</code> keyword is not needed when declaring custom operators. At least one of the operator's parameters must be of a user-defined type (after all, operators with built-in type parameters are already defined).
 
The following example overloads the member operators <code>Cast</code>(Cast) and <code>*=</code>(Multiply And Assign) for objects of a user-defined type.
 
<lang freebasic>Type Rational
As Integer numerator, denominator
Declare Operator Cast () As Double
Declare Operator Cast () As String
Declare Operator *= (Byref rhs As Rational)
End Type
 
Operator Rational.cast () As Double
Return numerator / denominator
End Operator
 
Operator Rational.cast () As String
Return numerator & "/" & denominator
End Operator
 
Operator Rational.*= (Byref rhs As Rational)
numerator *= rhs.numerator
denominator *= rhs.denominator
End Operator
 
Dim As Rational r1 = (2, 3), r2 = (3, 4)
r1 *= r2
Dim As Double d = r1
Print r1, d</lang>
 
 
=={{header|jq}}==
2,141

edits