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 MyVarFUNCTION
Squared(
THIS
AS
FOO_CLASS)
AS
INTEGERFUNCTION
Cubed(
THIS
AS
FOO_CLASS)
AS
INTEGERFUNCTION
TimesX(
THIS
AS
FOO_CLASS, XAS
INTEGER)
AS
INTEGEREND
TYPE
DIM
MeAS
FOO : Initialize(
&
Me,3
)
DIM
YouAS
FOO : Initialize(
&
You,4
)
CLS
"================="
"Me.Squared "
, Me.Squared(
&
Me)
"Me.Cubed "
, Me.Cubed(
&
Me)
"Me.TimesX "
, Me.TimesX(
&
Me,100
)
"================="
"You.Squared "
, You.Squared(
&
You)
"You.Cubed "
, You.Cubed(
&
You)
"You.TimesX "
, You.TimesX(
&
You,100
)
"================="
"Changing .MyVar in Me and You"
Me.MyVar=
6
You.MyVar=
7
"================="
"Me.Squared "
, Me.Squared(
&
Me)
"Me.Cubed "
, Me.Cubed(
&
Me)
"Me.TimesX "
, Me.TimesX(
&
Me,100
)
"================="
"You.Squared "
, You.Squared(
&
You)
"You.Cubed "
, You.Cubed(
&
You)
"You.TimesX "
, You.TimesX(
&
You,100
)
"================="
SUB
Initialize(
THIS
AS
FOO_CLASS, bAS
INTEGER)
WITH
THIS
.MyVar=
b .Squared=
Squared .Cubed=
Cubed .TimesX=
TimesXEND
WITH
END
SUB
FUNCTION
Squared(
THIS
AS
FOO_CLASS)
FUNCTION
=
THIS.MyVar*
THIS.MyVarEND
FUNCTION
FUNCTION
Cubed(
THIS
AS
FOO_CLASS)
FUNCTION
=
THIS.MyVar*
THIS.MyVar*
THIS.MyVarEND
FUNCTION
FUNCTION
TimesX(
THIS
AS
FOO_CLASS, X)
FUNCTION
=
THIS.MyVar*
XEND
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
aAS
ANGLE : Angle(
&
a,36.2345
)
DIM
bAS
ANGLE : Angle(
&
b,12.45
)
DIM
cAS
ANGLE : Angle(
&
c,180.453
)
COS
(
b.Radians(
&
b)
)
'Print the Cos of b
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.
(
&
a)
' Print a in radians
a.SetRadians(
&
a, a.Radians#(
&
a)
+
3.14
)
'Add 3.14 radians
(
&
a)
(
&
a)
(
&
a)
(
&
a)
(
&
c)
PAUSE
'======================================================
' REUSABLE CLASSES
'======================================================
CONST
PI=
3.141592653589793
TYPE
ANGLE AngleddAS
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, radAS
DOUBLE
)
SUB
SetDMS(
THIS
AS
ANGLE_CLASS, DegAS
INTEGER, _ MminAS
INTEGER, SecAS
DOUBLE
)
END
TYPE
SUB
Angle(
THIS
AS
ANGLE_CLASS, bAS
DOUBLE
)
WITH
THIS
.Angledd=
b .Radians=
Class_Radians .Grads=
Class_Grads .Degrees=
Class_Degrees .DMS=
Class_DMS .SetDMS=
Class_SetDMS .SetRadians=
Class_SetRadiansEND
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.AngleddEND
FUNCTION
FUNCTION
Class_DMS$(
THIS
AS
ANGLE_CLASS)
DIM
degAS
DOUBLE
DIM
mminAS
DOUBLE
DIM
secAS
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
/
PIEND
SUB