Hi efgee,
This is the easiest O2 way to express classes with properties and methods:
[code=thinbasic]
'
'---------------------------------
'SIMPLE CLASSES
'=================================
'The methods are defined inside the class block.
'
basic
'-------------
class cuboid
'=============
protected
height as double
width as double
length as double
public
method surface() as double
return (length*width+length*height+width*height)*2
end method
method volume() as double
return length*width*height
end method
method setsize(l as double,w as double, h as double)
length=l : width=w : height=h
end method
end class
cuboid cu
cu.setsize 10,5,2
cr=chr 13
tab=chr 9
print "Volume:" tab cu.volume() cr "Surface:" tab cu.surface()
'----------
class brick
'==========
has cuboid 'dimensions and also ..
protected
density as double
material as string
public
method setmaterial(m as string)
material=m
density=1
if m="ceramic" then density=2
if m="concrete" then density=2.5
if m="ice" then density=0.95
end method
method weight() as double
return volume()*density
end method
end class
brick br
br.SetMaterial "concrete"
br.SetSize 20,10,7.5
print "Brick weight: " br.weight
[/code]
but you can also define methods outside the class declaration:
[code=thinbasic]
'
'---------------------------------
'SIMPLE CLASSES
'=================================
'The methods are defined inside the class block.
'
basic
'-------------
class cuboid
'=============
public
method surface() as double
method volume() as double
method setsize(l as double,w as double, h as double)
/
protected
height as double
width as double
length as double
end class
methods of cuboid
'================
method surface() as double
return (length*width+length*height+width*height)*2
end method
method volume() as double
return length*width*height
end method
method setsize(l as double,w as double, h as double)
length=l : width=w : height=h
end method
end methods
cuboid cu
cu.setsize 10,5,2
cr=chr 13
tab=chr 9
print "Volume:" tab cu.volume() cr "Surface:" tab cu.surface()
'----------
class brick
'==========
public
has cuboid 'dimensions and also ..
method setmaterial(m as string)
method weight() as double
/
protected
density as double
material as string
end class
methods of brick
'===============
method setmaterial(m as string)
material=m
density=1
if m="ceramic" then density=2
if m="concrete" then density=2.5
if m="ice" then density=0.95
end method
method weight() as double
return volume()*density
end method
end methods
brick br
br.SetMaterial "concrete"
br.SetSize 20,10,7.5
print "Brick weight: " br.weight
[/code]
Charles
Bookmarks