It is impossible to show some capabilities of OOP from a very short code.
Example of code showing an OOP subset only:
Code: Select all
Type Animal Extends Object
Declare Abstract Property greeting () As String
End Type
Type Cat Extends Animal
Declare Virtual Property greeting () As String Override
End Type
Virtual Property Cat.greeting () As String
Return "Meow!"
End Property
Type Dog Extends Animal
Declare Virtual Property greeting () As String Override
End type
Virtual Property Dog.greeting () As String
Return "Woof!"
End Property
Type BigDog Extends Dog
Declare Virtual Property greeting () As String Override
End type
Virtual Property BigDog.greeting () As String
Return "Woooooof!"
End Property
'Using directly the derived-type instances
Dim c As Cat, d As Dog, b As BigDog
Print c.greeting, d.greeting, b.greeting,
Print
'Using the polymorphism (by overriding) on a collection of base-type pointers
Dim pa(0 To 2) As Animal Ptr => {@c, @d, @b}
For i As Integer = Lbound(pa) To Ubound(pa)
Print pa(i)->greeting,
Next i
Print
'Dowcasting/Dereferencing the base-type pointers
Dim pc As Cat Ptr => Cast(Cat Ptr, pa(0)), pd As Dog Ptr => Cast(Dog Ptr, pa(1)), pb As BigDog Ptr => Cast(BigDog Ptr, pa(2))
Print pc->greeting, pd->greeting, pb->greeting,
Print
Sleep
Shorter version with only 2-level inheritance and polymorphism:
Code: Select all
Type Animal Extends Object
Declare Abstract Property greeting () As String
End Type
Type Cat Extends Animal
Declare Virtual Property greeting () As String Override
End Type
Virtual Property Cat.greeting () As String
Return "Meow!"
End Property
Type Dog Extends Animal
Declare Virtual Property greeting () As String Override
End type
Virtual Property Dog.greeting () As String
Return "Woof!"
End Property
Dim c1 As Cat, d1 As Dog, c2 As Cat, d2 As Dog
'Using the polymorphism (by overriding) on a collection of base-type pointers
Dim pa(0 To 3) As Animal Ptr => {@c1, @d1, @c2, @d2}
For i As Integer = Lbound(pa) To Ubound(pa)
Print pa(i)->greeting
Next i
Sleep