RosettaCodeData/Task/Accumulator-factory/FreeBASIC/accumulator-factory.freebasic
2016-12-05 23:44:36 +01:00

49 lines
1.3 KiB
Text

' FB 1.05.0 Win64
' uses overloaded methods to deal with the integer/float aspect (long and single are both 4 bytes)
Type Bar
Public:
Declare Constructor(As Long)
Declare Constructor(As Single)
Declare Function g(As Long) As Long
Declare Function g(As Single) As Single
Private:
As Single sum_ '' can't be altered by external code
End Type
Constructor Bar(i As Long)
sum_ = i
End Constructor
Constructor Bar(s As Single)
sum_ = s
End Constructor
Function Bar.g(i As Long) As Long
sum_ += i
Return sum_ '' would round down to a Long if non-integral Singles had been added previously
End Function
Function Bar.g(s As Single) As Single
sum_ += s
Return sum_
End Function
Function foo Overload(i As Long) As Bar '' returns a Bar object rather than a pointer to Bar.g
Dim b As Bar = Bar(i)
Return b
End Function
Function foo Overload(s As Single) As Bar '' overload of foo to deal with Single argument
Dim b As Bar = Bar(s)
Return b
End Function
Dim x As Bar = foo(1) '' assigns Bar object to x
x.g(5) '' calls the Long overload of g on the Bar object
foo(3) '' creates a separate Bar object which is unused
print x.g(2.3) '' calls the Single overload of g on the Bar object and should print 1 + 5 + 2.3 = 8.3
Print
Print "Press any key to quit"
Sleep