Object-oriented programming in Lemick.  

Table of Contents

 

(C) Copyright 2001 Alex Iliasov
email:alex@users.kyrnet.kg

 



Lemick Basic will support all standard features of object-oriented programming language. Now it supports classes as abstract object descriptions, public and private class members, overloading of methods, contructors and destructors, automated creation of contructors if none provided by the programmer, automated complementation of contructors with calls to sub-constructors - contructors of included or inhereted classes; single and multiple inheritance: child class has all the members of the parent class(es) plus his own members. In Lemick it is said that class1 extends class2. Since extended class has all members of any of his parent classes, instances of this class could be used instead of parents's in any occasions: when calling methods or passing it as an argument of a function and etc.

User types defined through TYPE ... END TYPE statements are also classes, but only one method declaration (with an exception for it overloaded instances) is allowed - init() - the constructor; all members in such classes are always public.

Example of complex number class (just a draft):

'class implementing complex numbers
'it was boring for me to overload all functions and write all that
'stuff. anyway idea is clear
'mainly this is an example how overloading of built-in functions can be used
'this code also uses very intricate feature of Lemick Basic - special algorithm
'for garbage collection - look at overloaded '+' code

class cmplx
  public
  dim r as double,i as double
  sub init()
    r=0:i=0
  end sub
  sub init(a as double,b as double)
    r=a:i=b
  end sub
  function str() as string
    str="("+str(r)+","+str(i)+")"
  end function
  function inv() as cmplx
    i=-i
    inv=me
  end function
  function operator+(aaa as cmplx) as cmplx
    dim f as cmplx 'if you have to write the same, for example in C++, then
    f.r=r+aaa.r       'you will face the problem- compiler generates call
    f.i=i+aaa.i       'to destructor (for f variable) just befor the function returns,
    operator+=f       'so return value will be currupted. Lemick doesn't, this
  end function         'allows so simple implementation of overloaded '+'
  function operator+(aaa as double) as cmplx
    dim f as cmplx
    f.r=r+aaa
    operator+=f
  end function
  function operator*(aaa as cmplx) as cmplx
    dim f as cmplx
    f.r=r*aaa.r+i*aaa.i
    f.i=r*aaa.i-i*aaa.r
    operator*=f
  end function
  function re() as double
    re=r
  end function
  function
im() as double
    im=i
  end function
  function
len() as int
    len=len(str(me))
  end function
  function len(k as int) as int
    len=len(str(me))*k
  end function
end class

dim z as cmplx(2,2), w as cmplx(1,-6)
dim zzz(10000) as cmplx(1,1)
dim x as string
print "x=";x
print "z=",z
print "w=",w
print "w+z",w+z
print "z+w*z+5.7",z+w*z+5.7+z
print "z=",z
print "w=",w
print "Hello!"
print z.im
print z.re
print z.inv
print zzz(5)
print len(z)
print len(z,2)


 
     


Hosted by uCoz