Try to understand more about class constructor and have build this simple example and a Copy Feature but its Not using this and callocate Statements
Code: Select all
' -- class example 2 by frank bruebach, freebasic
' -- constructor copy
'
Type Superhero
_name As String
Declare Constructor ()
Declare Constructor (names As String)
Declare Constructor (other As Superhero)
Declare Function Calculate(value1 As Integer, value2 As Integer) As Integer
Declare Operator Cast () As String
Declare Sub Cleanup()
End Type
Constructor Superhero ()
_name = "Batman"
End Constructor
Constructor Superhero (names As String)
_name = names
End Constructor
Constructor Superhero (other As Superhero)
_name = other._name
End Constructor
Function Superhero.Calculate(value1 As Integer, value2 As Integer) As Integer
Return value1 + value2
End Function
Operator Superhero.Cast () As String
Return _name & " is my Hero"
End Operator
Sub Superhero.Cleanup()
' Add cleanup tasks here if needed
' This method will be called automatically
' before the object is destroyed
End Sub
Print "Creating myHero"
Dim myHero As Superhero
Print "Creating copiedHero"
Dim copiedHero As Superhero = myHero
Print "Original Hero: "; myHero
Print "Copied Hero: "; copiedHero
Dim result As Integer = myHero.Calculate(5, 7)
Print "Calculation Result: "; result
' Call cleanup method explicitly before destroying the objects
myHero.Cleanup()
copiedHero.Cleanup()
Sleep()