BCX Object Oriented Programming

THIS

THIS is a BCX reserved word. BCX automatically changes non-quoted spellings of THIS to This and, more importantly, BCX will automatically change

THIS.Something

to

This->Something

allowing users to reference UDT members in a BASIC-like way. This should work with any ANSI-C compiler. This feature helps give BCX some Object Oriented Programming benefits.

BCX also generates a class definition using the UDT's name and places it in the #defines section of the resulting C file. This allows embedded functions to self reference their UDT. In the example below, BCX will emit #define FOO_CLASS struct _FOO* under User Defined Constants.


 TYPE FOO
  Marker AS INTEGER
  FUNCTION TimesX(This AS FOO_CLASS, X AS LONG) AS LONG
 END TYPE


 '===============================================================
 ' Semi-OOP implementation for BCX by Kevin Diggins Dec 01, 2003
 '===============================================================
  
 TYPE FOO
   MyVar
   FUNCTION Squared(THIS AS FOO_CLASS) AS INTEGER
   FUNCTION Cubed(THIS AS FOO_CLASS) AS INTEGER
   FUNCTION TimesX(THIS AS FOO_CLASS, X AS INTEGER) AS INTEGER
 END TYPE
 
 DIM Me AS FOO : Initialize(&Me, 3)
 DIM You AS FOO : Initialize(&You, 4)
 
 CLS
 
 PRINT "================="
 PRINT "Me.Squared " , Me.Squared(&Me)
 PRINT "Me.Cubed " , Me.Cubed(&Me)
 PRINT "Me.TimesX " , Me.TimesX(&Me, 100)
 PRINT "================="
 PRINT "You.Squared " , You.Squared(&You)
 PRINT "You.Cubed " , You.Cubed(&You)
 PRINT "You.TimesX " , You.TimesX(&You, 100)
 PRINT "================="
 
 PRINT "Changing .MyVar in Me and You"
 
 Me.MyVar = 6
 You.MyVar = 7
 
 PRINT "================="
 PRINT "Me.Squared " , Me.Squared(&Me)
 PRINT "Me.Cubed " , Me.Cubed(&Me)
 PRINT "Me.TimesX " , Me.TimesX(&Me, 100)
 PRINT "================="
 PRINT "You.Squared " , You.Squared(&You)
 PRINT "You.Cubed " , You.Cubed(&You)
 PRINT "You.TimesX " , You.TimesX(&You, 100)
 PRINT "================="
 
 SUB Initialize(THIS AS FOO_CLASS, b AS INTEGER)
   WITH THIS
     .MyVar = b
     .Squared = Squared
     .Cubed = Cubed
     .TimesX = TimesX
   END WITH
 END SUB
 
 FUNCTION Squared(THIS AS FOO_CLASS)
   FUNCTION = THIS.MyVar * THIS.MyVar
 END FUNCTION
 
 FUNCTION Cubed(THIS AS FOO_CLASS)
   FUNCTION = THIS.MyVar * THIS.MyVar * THIS.MyVar
 END FUNCTION
 
 FUNCTION TimesX(THIS AS FOO_CLASS, X)
   FUNCTION = THIS.MyVar * X
 END FUNCTION
 

The following example also demonstrates the BCX Semi-OOP approach to C++ classes.

  
 $COMMENT
 
 ********************************************************************************
  
  THIS IS my first code using the NEW OOP syntax. I picked a simple
  example so I could get used TO the syntax, but I wanted something
  useful also so folk could see where a CLASS IS most useful.
  
  I noted that the class name IS CASE sensitive,(ANGLE), so I used a
  mixed CASE "Angle" FOR the constructor instead of "Initialize" LIKE
  the example IN the help file. Also I choose TO name the class
  functions WITH a prefix of Class_ TO distinguish them from other
  functions.
  
  I will expand ON THIS example, adding complexity, AND see where I get.
  
  Garvan O'Keeffe
 
 ********************************************************************************
  
 $COMMENT
  
 'Create 3 objects & set their initial values(angles in decimal degrees)
  
 DIM a AS ANGLE : Angle(&a, 36.2345)
 DIM b AS ANGLE : Angle(&b, 12.45)
 DIM c AS ANGLE : Angle(&c, 180.453)
  
 PRINT COS(b.Radians(&b))  'Print the Cos of b
 PRINT SIN(c.Radians(&c))  'Print the Sin of c
  
 a.SetDMS(&a, 41, 30, 24) ' Set value of a to 41 deg. 30 min. 24 sec.
  
 PRINT a.Radians#(&a)      ' Print a in radians
  
 a.SetRadians(&a, a.Radians#(&a) + 3.14)   'Add 3.14 radians
  
 PRINT a.Radians#(&a)
 PRINT a.Grads#(&a)
 PRINT a.Degrees#(&a)
 PRINT a.DMS$(&a)
 PRINT c.DMS$(&c)
 PAUSE
  
 '======================================================
 ' REUSABLE CLASSES
 '======================================================
  
 CONST PI = 3.141592653589793
  
 TYPE ANGLE
   Angledd AS DOUBLE
   FUNCTION Radians#(THIS AS ANGLE_CLASS)
   FUNCTION Grads(THIS AS ANGLE_CLASS) AS DOUBLE
   FUNCTION Degrees(THIS AS ANGLE_CLASS) AS DOUBLE
   FUNCTION DMS$(THIS AS ANGLE_CLASS)
   SUB SetRadians(THIS AS ANGLE_CLASS, rad AS DOUBLE)
   SUB SetDMS(THIS AS ANGLE_CLASS, Deg AS INTEGER, _
   Mmin AS INTEGER, Sec AS DOUBLE)
 END TYPE
  
 SUB Angle(THIS AS ANGLE_CLASS, b AS DOUBLE)
   WITH THIS
     .Angledd = b
     .Radians = Class_Radians
     .Grads = Class_Grads
     .Degrees = Class_Degrees
     .DMS = Class_DMS
     .SetDMS = Class_SetDMS
     .SetRadians = Class_SetRadians
   END WITH
 END SUB
  
 FUNCTION Class_Radians#(THIS AS ANGLE_CLASS)
   FUNCTION = THIS.Angledd * PI/180
 END FUNCTION
  
 FUNCTION Class_Grads(THIS AS ANGLE_CLASS) AS DOUBLE
   FUNCTION = THIS.Angledd * 400/360
 END FUNCTION
  
 FUNCTION Class_Degrees(THIS AS ANGLE_CLASS) AS DOUBLE
   FUNCTION = THIS.Angledd
 END FUNCTION
  
  
 FUNCTION Class_DMS$(THIS AS ANGLE_CLASS)
   DIM deg AS DOUBLE
   DIM mmin AS DOUBLE
   DIM sec AS DOUBLE
   DIM mRetStr$
   deg = INT(THIS.Angledd)
   mmin = INT((THIS.Angledd-deg) * 60)
   sec =((THIS.Angledd-deg) * 60 - mmin) * 60
   mRetStr$ = STR$(deg) + CHR$(248, 32) + STR$(mmin) + DQ$ _
   + RTRIM$(RTRIM$(LEFT$(STR$(sec), 7), 48), 46) + CHR$(39)
   mRetStr$ = " " + STRIM$(mRetStr$)
   FUNCTION = mRetStr$
 END FUNCTION
  
  
 SUB Class_SetDMS(THIS AS ANGLE_CLASS, d%, m%, s#)
   THIS.Angledd = d% + m%/60 + s#/3600
 END SUB
  
  
 SUB Class_SetRadians(THIS AS ANGLE_CLASS, r#)
   THIS.Angledd = r# * 180/PI
 END SUB