OOP test

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

OOP test

Postby bluatigro » Mar 29, 2020 11:05

i want to explore the OOP of FB

error :
i m verry old

Code: Select all

'' bluatigro 29 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 4 , 2 ) )
  this.d = val( mid( t , 1 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
private :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as integer weight
public :
  declare constructor ( l as string , d as TDate , w as integer )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as integer
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal( l as string , d as TDate , w as integer )
  this.latin = l
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as integer
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

dim as TAnimal human = TAnimal( "homo sapiens" , TDate( 6 , 1 , 1960 ) , 118 )
print "latin : " + human.getLatin()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
sleep
 
badidea
Posts: 2057
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: OOP test

Postby badidea » Mar 29, 2020 11:14

Two things:
1. FreeBASIC Date is weird, it returns MM-DD-YYY (day and month swapped), see constructor TDate()
2. TDate( 6 , 1 , 1960 ) --> TDate( 1960 , 1 , 6 )
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 29, 2020 11:33

update :
better date reading
my birthday is now good
added name to TAnimal

error :
thie code does not run and gives cryptic errorcode

Code: Select all

'' bluatigro 29 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 1 , 2 ) )
  this.d = val( mid( t , 4 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
private :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as integer weight
  dim as string naam
public :
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as integer
  declare function getName() as string
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal( l as string , n as string _
  , d as TDate , w as integer )
  this.latin = l
  this.naam = n
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as integer
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

dim as TAnimal human = TAnimal( "homo sapiens" _
, "bluatigro" , TDate( 1960 , 1 , 6 ) , 118 )
print "latin : " + human.getLatin()
print "name : " + human.getName()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
sleep
 
fxm
Posts: 9636
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: OOP test

Postby fxm » Mar 29, 2020 11:38

Where is the body of the member procedure 'TAnimal.getName() as string'?
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 29, 2020 11:50

update :
it is working good now

Code: Select all

'' bluatigro 29 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 1 , 2 ) )
  this.d = val( mid( t , 4 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
private :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as integer weight
  dim as string naam
public :
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as integer
  declare function getName() as string
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal( l as string , n as string _
  , d as TDate , w as integer )
  this.latin = l
  this.naam = n
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as integer
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.getName() as string
  return this.naam
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

dim as TAnimal human = TAnimal( "homo sapiens" _
, "bluatigro" , TDate( 1960 , 1 , 6 ) , 118 )
print "latin : " + human.getLatin()
print "name : " + human.getName()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
sleep
 
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 29, 2020 12:18

update :
OOP extends added

error :
the IDE recognises 'extends' FB not

insructions please

i down loaded FB 1.07 but it has not IDE

Code: Select all

'' bluatigro 29 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 1 , 2 ) )
  this.d = val( mid( t , 4 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
private :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as integer weight
  dim as string naam
public :
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as integer
  declare function getName() as string
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal( l as string , n as string _
  , d as TDate , w as integer )
  this.latin = l
  this.naam = n
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as integer
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.getName() as string
  return this.naam
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

type TMammel extends TAnimal
  dim as integer nippelMax
public :
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  declare function getNippelMax() as integer
end type
constructor TMammel( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  ''super( i , n , d , w )
  this.nippelMax = nr
end constructor
function TMammel.getNippelMax() as integer
  return this.nippelMax
end function

dim as TAnimal human = TAnimal( "homo sapiens" _
, "bluatigro" , TDate( 1960 , 1 , 6 ) , 118 )
print "latin : " + human.getLatin()
print "name : " + human.getName()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
sleep
 
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 29, 2020 12:23

the IDE does not recognize 'inplements'

how do i make intefaces and abstract classes in FB ?
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 29, 2020 12:43

updatw :
'extends' is now reconized by FB

error :
UDT.constructor missing

what is a UDT.constructor ?

Code: Select all

'' bluatigro 29 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 1 , 2 ) )
  this.d = val( mid( t , 4 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
protected :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as integer weight
  dim as string naam
public :
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as integer
  declare function getName() as string
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal( l as string , n as string _
  , d as TDate , w as integer )
  this.latin = l
  this.naam = n
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as integer
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.getName() as string
  return this.naam
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

type TMammel extends TAnimal
  dim as integer nippelMax
public :
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  declare function getNippelMax() as integer
end type
constructor TMammel( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  dim as TAnimal a = TAnimal( l , n , d , w )
  this.latin = a.getLatin()
  this.naam = a.getName()
  this.birthDay = a.getBirthDay()
  this.weight = a.getWeight()
  this.nippelMax = nr
end constructor
function TMammel.getNippelMax() as integer
  return this.nippelMax
end function

dim as TMammel human = TMammel( "homo sapiens" _
, "bluatigro" , TDate( 1960 , 1 , 6 ) , 118 , 2 )
print "latin : " + human.getLatin()
print "name : " + human.getName()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
print "nippel max : " + str( human.getNippelMax() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
sleep
 
Last edited by bluatigro on Mar 29, 2020 13:13, edited 1 time in total.
paul doe
Posts: 1221
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: OOP test

Postby paul doe » Mar 29, 2020 12:52

bluatigro wrote:...
how do i make intefaces and abstract classes in FB ?
...

By reading the manual, under the section 'User Defined Types'.

There is no such thing as a proper interfaces yet (neither do classes, for that matter). However, by using the types you can fudge them to some extent. FreeBasic only supports single inheritance, so take that into account when you define types intended to be used as interfaces/base classes.
fxm
Posts: 9636
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: OOP test

Postby fxm » Mar 29, 2020 13:01

error 188: Missing UDT.constructor(byref as const UDT) implementation (base UDT without default constructor requires manual initialization), found 'end' in 'end type'

Define a 'constructor TAnimal ()':

Code: Select all

constructor TAnimal ()
end constructor

And also:

Code: Select all

constructor TMammel( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  Base( l , n , d , w )
  this.nippelMax = nr
end constructor
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 29, 2020 15:04

@fxm :
thanks for example

update :
it works good now

Code: Select all

'' bluatigro 29 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 1 , 2 ) )
  this.d = val( mid( t , 4 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
protected :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as integer weight
  dim as string naam
public :
  declare constructor ()
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as integer
  declare function getName() as string
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal()
end constructor
constructor TAnimal( l as string , n as string _
  , d as TDate , w as integer )
  this.latin = l
  this.naam = n
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as integer
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.getName() as string
  return this.naam
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

type TMammel extends TAnimal
  dim as integer nippelMax
public :
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  declare function getNippelMax() as integer
end type
constructor TMammel( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  base( l , n , d , w )
  this.nippelMax = nr
end constructor
function TMammel.getNippelMax() as integer
  return this.nippelMax
end function

dim as TMammel human = TMammel( "homo sapiens" _
, "bluatigro" , TDate( 1960 , 1 , 6 ) , 118 , 2 )
print "latin : " + human.getLatin()
print "name : " + human.getName()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
print "nippel max : " + str( human.getNippelMax() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
sleep
 
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 29, 2020 15:49

update :
type TBug added

Error :
line 189

Code: Select all

'' bluatigro 29 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 1 , 2 ) )
  this.d = val( mid( t , 4 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
protected :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as double weight
  dim as string naam
public :
  declare constructor ()
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as double
  declare function getName() as string
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal()
end constructor
constructor TAnimal( l as string , n as string _
  , d as TDate , w as integer )
  this.latin = l
  this.naam = n
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as double
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.getName() as string
  return this.naam
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

type TMammel extends TAnimal
private :
  dim as integer nippelMax
public :
  declare constructor()
  declare constructor ( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  declare function getNippelMax() as integer
end type
constructor TMammel()
end constructor
constructor TMammel( l as string , n as string _
  , d as TDate , w as integer , nr as integer )
  base( l , n , d , w )
  this.nippelMax = nr
end constructor
function TMammel.getNippelMax() as integer
  return this.nippelMax
end function

type TBug extends TAnimal
private :
  as integer canFly
public :
  declare constructor ()
  declare constructor ( l as string , n as string _
  , d as TDate , w as double , cf as integer )
  declare sub flying()
end type
constructor TBug()
end constructor
constructor TBug( l as string , n as string _
  , d as TDate , w as double , cf as integer )
  base( l , n , d , w )
  this.canFly = cf
end constructor
sub TBug.flying()
  if this.canFly then
    print "Bug " + this.getName() + " is flying ."
  else
    print "Bug " + this.getLatin() + " can not fly !!"
  end if
end sub


dim as TBug fly , centipede
fly = TBug( "Fly" , "bugsy" _
, TDate( 2019 , 12 , 20 ) , 0.001 , 1 )
centipede = TBug( "Centipede" , "Feeds" _
, TDate( 2020 , 1 , 24 ) , 0.002 , 0 )
dim as TMammel human = TMammel( "homo sapiens" _
, "bluatigro" , TDate( 1960 , 1 , 6 ) , 118 , 2 )
print "latin : " + human.getLatin()
print "name : " + human.getName()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
print "nippel max : " + str( human.getNippelMax() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
print "latin : " + fly.getLatin()
print "name : " + fly.getName()
print "age : " + str( fly.getAge() )
print "weight : " + str( fly.getWeight() )
print "flying : " ;
fly.flying()
if fly.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
print "latin : " + centipede.getLatin()
print "name : " + centipede.getName()
print "age : " + str( centipede.getAge() )
print "weight : " + str( centipede.getWeight() )                                            .getWeight() )
print "flying : " ;
centipede.flying
if centipede.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a

sleep
 
Xusinboy Bekchanov
Posts: 223
Joined: Jul 26, 2018 18:28

Re: OOP test

Postby Xusinboy Bekchanov » Mar 29, 2020 16:30

bluatigro wrote:update :
type TBug added

Error :
line 189

Code: Select all

print "weight : " + str( centipede.getWeight() )                                            .getWeight() )

Here it is superfluous: .getWeight() )
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: OOP test

Postby bluatigro » Mar 30, 2020 8:58

update :
line 189 now good

some integer changed in double

Code: Select all

'' bluatigro 30 mrt 2020
'' OOP test

type TDate
private :
  as integer y , m , d
public :
  declare constructor ()
  declare constructor ( a as integer , b as integer , c as integer )
  declare function getYear() as integer
  declare function getMonth() as integer
  declare function getDay() as integer
end type
constructor TDate()
'' set TDate on today .
  dim as string t = date
  this.y = val( mid( t , 7 , 4 ) )
  this.m = val( mid( t , 1 , 2 ) )
  this.d = val( mid( t , 4 , 2 ) )
end constructor
constructor TDate( y as integer , m as integer , d as integer )
  this.y = y
  this.m = m
  this.d = d
end constructor
function TDate.getYear() as integer
  return this.y
end function
function TDate.getMonth() as integer
  return this.m
end function
function Tdate.getDay() as integer
  return this.d
end function

type TAnimal
protected :
  dim as string latin
  dim as Tdate birthDay = Tdate()
  dim as double weight
  dim as string naam
public :
  declare constructor ()
  declare constructor ( l as string , n as string _
  , d as TDate , w as double )
  declare function getLatin() as string
  declare function getAge() as integer
  declare function getWeight() as double
  declare function getName() as string
  declare function getBirthDay() as TDate
  declare function isBirthDay() as integer
end type
constructor TAnimal()
end constructor
constructor TAnimal( l as string , n as string _
  , d as TDate , w as double )
  this.latin = l
  this.naam = n
  this.birthDay = d
  this.weight = w
end constructor
function TAnimal.getLatin() as string
  return this.latin
end function
function TAnimal.getAge() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  age = y2 - y1
  if m2 < m1 then
    age -= 1
  else
    if m2 = m1 then
      if d2 < d1 then
        age -= 1
      end if
    end if
  end if
  return age
end function
function Tanimal.getWeight() as double
  return this.weight
end function
function TAnimal.getBirthDay() as TDate
  return this.birthDay
end function
function TAnimal.getName() as string
  return this.naam
end function
function TAnimal.isBirthDay() as integer
  dim as Tdate today = TDate()
  dim as integer y1,y2,m1,m2,d1,d2,age
  y1 = this.getBirthDay().getYear()
  m1 = this.getBirthDay().getMonth()
  d1 = this.getBirthDay().getDay()
  y2 = today.getYear()
  m2 = today.getMonth()
  d2 = today.getDay()
  if y1 <> y2 then return 0
  if m1 <> m2 then return 0
  if d1 <> d2 then return 0
  return 1
end function

type TMammel extends TAnimal
private :
  dim as integer nippelMax
public :
  declare constructor()
  declare constructor ( l as string , n as string _
  , d as TDate , w as double , nr as integer )
  declare function getNippelMax() as integer
end type
constructor TMammel()
end constructor
constructor TMammel( l as string , n as string _
  , d as TDate , w as double , nr as integer )
  base( l , n , d , w )
  this.nippelMax = nr
end constructor
function TMammel.getNippelMax() as integer
  return this.nippelMax
end function

type TBug extends TAnimal
private :
  as integer canFly
public :
  declare constructor ()
  declare constructor ( l as string , n as string _
  , d as TDate , w as double , cf as integer )
  declare sub flying()
end type
constructor TBug()
end constructor
constructor TBug( l as string , n as string _
  , d as TDate , w as double , cf as integer )
  base( l , n , d , w )
  this.canFly = cf
end constructor
sub TBug.flying()
  if this.canFly then
    print "Bug " + this.getName() + " is flying ."
  else
    print "Bug " + this.getLatin() + " can not fly !!"
  end if
end sub


dim as TBug fly , centipede
fly = TBug( "Fly" , "bugsy" _
, TDate( 2019 , 12 , 20 ) , 0.001 , 1 )
centipede = TBug( "Centipede" , "Feeds" _
, TDate( 2020 , 1 , 24 ) , 0.002 , 0 )
dim as TMammel human = TMammel( "homo sapiens" _
, "bluatigro" , TDate( 1960 , 1 , 6 ) , 118 , 2 )
print "latin : " + human.getLatin()
print "name : " + human.getName()
print "age : " + str( human.getAge() )
print "weight : " + str( human.getWeight() )
print "nippel max : " + str( human.getNippelMax() )
dim as string a
if human.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
print "latin : " + fly.getLatin()
print "name : " + fly.getName()
print "age : " + str( fly.getAge() )
print "weight : " + str( fly.getWeight() )
print "flying : " ;
fly.flying()
if fly.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a
print "latin : " + centipede.getLatin()
print "name : " + centipede.getName()
print "age : " + str( centipede.getAge() )
print "weight : " + str( centipede.getWeight() )
print "flying : " ;
centipede.flying
if centipede.isBirthDay() then
  a = "yes"
else
  a = "no"
end if
print "has birth day : " + a

sleep
 

Return to “General”

Who is online

Users browsing this forum: No registered users and 8 guests