'' bluatigro 25 jul 2018
'' animal oop
type tanimal
protected :
dim as string naam
dim as double weight
public :
declare constructor ()
declare sub setname( n as string )
declare function getname() as string
declare sub setweight( kg as double )
declare function getweight() as double
declare sub speek()
end type
constructor tanimal()
naam = "unkown"
weight = 0.0
end constructor
sub tanimal.setname( n as string )
naam = n
end sub
function tanimal.getname() as string
return naam
end function
sub tanimal.setweight( kg as double )
weight = kg
end sub
function tanimal.getweight() as double
return weight
end function
sub tanimal.speek()
print "animal " + naam + " speeks ."
end sub
type tcat extends tanimal
public :
declare sub speek()
end type
sub tcat.speek()
print "cat " + naam + " miauw ."
end sub
type tdog extends tanimal
public :
declare sub speek()
end type
sub tdog.speek()
print "dog " + naam + " woof ."
end sub
sub speek( q as tanimal )
q.speek
end sub
dim as tanimal animal
dim as tcat cat
dim as tdog dog
animal.setname "it"
cat.setname "kitty"
dog.setname "spike"
speek animal
speek cat
speek dog
print
animal.speek
cat.speek
dog.speek
sleep
'sub speek( q as tanimal )' works with 'tanimal' references to 'cat' and 'dog'.
For the Sub to retrieve the the real type of the passed objects, you must activate the polymorphism so that at run-time, the matched derived 'speek()' is called.
Two lines to be modified:
- 'tanimal' must 'Extends Object'.
- 'speek()' must be declared as 'Virtual' in 'tanimal'.
type tanimal extends object '' *****
protected :
dim as string naam
dim as double weight
public :
declare constructor ()
declare sub setname( n as string )
declare function getname() as string
declare sub setweight( kg as double )
declare function getweight() as double
declare virtual sub speek() '' *****
end type
constructor tanimal()
naam = "unkown"
weight = 0.0
end constructor
sub tanimal.setname( n as string )
naam = n
end sub
function tanimal.getname() as string
return naam
end function
sub tanimal.setweight( kg as double )
weight = kg
end sub
function tanimal.getweight() as double
return weight
end function
sub tanimal.speek()
print "animal " + naam + " speeks ."
end sub
type tcat extends tanimal
public :
declare sub speek()
end type
sub tcat.speek()
print "cat " + naam + " miauw ."
end sub
type tdog extends tanimal
public :
declare sub speek()
end type
sub tdog.speek()
print "dog " + naam + " woof ."
end sub
sub speek( q as tanimal )
q.speek
end sub
dim as tanimal animal
dim as tcat cat
dim as tdog dog
animal.setname "it"
cat.setname "kitty"
dog.setname "spike"
speek animal
speek cat
speek dog
print
animal.speek
cat.speek
dog.speek
sleep
Const CATS_AVG_WEIGHT=5.0
Const DOGS_AVG_WEIGHT=10.0
' ### Declaration ###
type tanimal extends object '' *****
protected :
dim as string _name
dim as double _weight
public :
Declare Constructor (n As String="unkown",w As Double=0.0)
Declare Property name() As String
Declare Property name(n As String)
declare Property weight( kg as double )
declare Property weight() as Double
declare virtual sub speek() '' *****
end Type
type tcat extends tanimal
public :
declare sub speek() Override
Declare Constructor (n As String="Kitty",w As Double=CATS_AVG_WEIGHT)
end Type
type tdog extends tanimal
public :
declare sub speek() Override
Declare Constructor (n As String="Spike",w As Double=DOGS_AVG_WEIGHT)
end Type
' ######################
' ### Implementation ###
' ######################
' ### Modul-Level ###
sub speek( q as tanimal Ptr )
q->speek
end Sub
' ### Animals ###
Constructor tanimal(n As String,w As Double)
_name = n
_weight = w
Print "name: ";n," weight: ";w
end Constructor
Property tanimal.name() As String
Return _name
End Property
Property tanimal.name(n As String)
_name=n
End Property
Property tanimal.weight() as Double
Return _weight
End Property
Property tanimal.weight( kg as double )
_weight=kg
End Property
sub tanimal.speek()
print "animal " + _name + ": ."
end Sub
' ### Cats ###
constructor tCat(n As String,w As Double)
base(n,w)
End Constructor
sub tcat.speek()
print "cat " + _name + ": miauw ."
end Sub
' ### Dogs ###
constructor tdog(n As String,w As Double)
base(n,w)
end Constructor
sub tdog.speek()
print "dog " + _Name + ": woof ."
end sub
' ####################
' ### Main program ###
' ####################
Dim As tanimal Ptr animal(20)
Randomize Timer
For i As Integer= LBound(animal) To UBound(animal)
Dim a As tanimal Ptr
Dim r As Integer,wr As Double
Dim cats As Integer,dogs As Integer
wr = (Rnd * 5) - 2
r = Int(Rnd * 4) + 1
If r=1 Then
cats+=1
a=New TCat("kitty "&cats,CATS_AVG_WEIGHT+wr)
ElseIf r=2 Then
dogs+=1
a=New TDog("spike "&dogs,DOGS_AVG_WEIGHT+wr)
ElseIf r=3 Then
a=New TAnimal("it")
Else
a=0 'Animal born dead
EndIf
animal(i)=a
Next
Sleep
cls
For i As Integer=LBound(animal) To UBound(animal)
If animal(i) Then
Print I,
speek animal(i)
Else
Print i &" is dead"
End If
Next
Sleep
cls
For i As Integer=LBound(animal) To UBound(animal)
If animal(i) Then
Print I,
animal(i)->speek
Else
Print "animal "& i &" is dead"
End If
Next
Print "Press button to kill all animals..."
Sleep
cls
For i As Integer=LBound(animal) To UBound(animal)
Delete animal(i)
Next
Print "all Animals killed! Game over!"
sleep