animal OOP try

General FreeBASIC programming questions.
bluatigro
Posts: 598
Joined: Apr 25, 2012 10:35
Location: netherlands

animal OOP try

Postby bluatigro » Jul 25, 2018 6:03

is this what shoot happen ?

Code: Select all

'' 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
fxm
Posts: 9256
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: animal OOP try

Postby fxm » Jul 25, 2018 8:06

'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'.

Code: Select all

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
bluatigro
Posts: 598
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: animal OOP try

Postby bluatigro » Jul 25, 2018 9:53

i tryed that :
error :
90 as.exe missing
fxm
Posts: 9256
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: animal OOP try

Postby fxm » Jul 25, 2018 10:03

Weird!
'as.exe' must be in the 'bin/win32' directory from 'fbc.exe' path.

Otherwise the code works.
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: animal OOP try

Postby RockTheSchock » Jul 25, 2018 19:28

I took your idea to show some more oop features.

Code: Select all

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

Return to “General”

Who is online

Users browsing this forum: MSN [Bot] and 1 guest