OOP in BBC Basic?

Here you can suggest additions and modifications to BB4W or BBCSDL
Ivan
Posts: 33
Joined: Tue 07 May 2019, 16:47

OOP in BBC Basic?

Post by Ivan »

Object oriented programming in Basic - why not evolve Basic to a new level?

I tried some Python examples and really loved the easy to read and understand at least at my programming level. Although i'm not to keen with Pythons syntax.

If I understand correctly the distance fra struct, private and local variables is not that far from objects with attributes and methods?

OOP.jpg
OOP.jpg (110.35 KiB) Viewed 193 times
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. Project for the last three month: 3D-drawing program (currently 950 lines).

RichardRussell
Posts: 232
Joined: Tue 15 Oct 2019, 09:10

Re: OOP in BBC Basic?

Post by RichardRussell »

Ivan wrote:
Sun 08 Mar 2020, 13:36
Object oriented programming in Basic - why not evolve Basic to a new level?
Why not indeed, which is why it has already been done:

Code: Select all

        INSTALL @lib$+"CLASSLIB"
 
        DIM Wheel{diameter, _set_diameter, _get_diameter, _get_circumference}
        DEF Wheel._set_diameter (diam) Wheel.diameter = diam : ENDPROC
        DEF Wheel._get_diameter = Wheel.diameter
        DEF Wheel._get_circumference = PI * Wheel.diameter
        PROC_class(Wheel{})
 
        DIM Bicycle{frontwheel{}=Wheel{}, rearwheel{}=Wheel{}, @bicycle}
        DEF Bicycle.@bicycle Bicycle.frontwheel.diameter = 25 : ENDPROC
        PROC_class(Bicycle{})
 
        PROC_new(mybike{}, Bicycle{})
 
        PRINT FN(mybike.frontwheel._get_circumference)
        PROC(mybike.frontwheel._set_diameter) (26.5)
        PROC(mybike.rearwheel._set_diameter) (27.0)
        PRINT FN(mybike.frontwheel._get_circumference)
        PRINT FN(mybike.rearwheel._get_circumference)
 
        PROC_discard(mybike{})
 
        END
In fact the OOP libary (CLASSLIB.BBC) is supplied as standard with BBC BASIC for Windows, although it's not mentioned in the main Help Documentation because it's a somewhat minority interest. Also, currently, CLASSLIB is not supplied with BBC BASIC for SDL 2.0 because although it would be entirely possible to adapt it to 64-bit operation it's quite a large task, and when I last tried I found it too daunting.
If I understand correctly the distance fra struct, private and local variables is not that far from objects with attributes and methods?
Quite, and in the early days when C++ was just a 'wrapper' around standard C that's exactly how it worked! And it's what persuaded me to write CLASSLIB because I realised that if it could be done in C it could be done in BBC BASIC.

If you're enthusiastic about Object Oriented Programming it may be the push I need to look again at porting CLASSLIB to BBCSDL.
If you have a comment about the style or tone of this message please report it to the moderators by clicking the exclamation mark icon, rather than complaining on the public forum.

Ivan
Posts: 33
Joined: Tue 07 May 2019, 16:47

Re: OOP in BBC Basic?

Post by Ivan »

I had missed that library. I will dive into OOP and practize.

I tried to convert the Python example to BBC Basic and think it is ok...

Is it possible in any way to break the line by the colon in methods?


INSTALL @lib$+"CLASSLIB"

REM class structure containing the members and methods
DIM Shape{x, y, text$, _set_size, _info, _get_area, _get_perimeter}

REM define the methods
DEF Shape._set_size (x,y) Shape.x = x : Shape.y = y : Shape.text$ = "Calculate area and perimeter:" : ENDPROC
DEF Shape._info = Shape.text$ : ENDPROC
DEF Shape._get_area = Shape.x * Shape.y : ENDPROC
DEF Shape._get_perimeter = 2 * Shape.x + 2 * Shape.y : ENDPROC

REM Register class
PROC_class(Shape{})



REM Program code:

REM instance of the class
PROC_new(Rectangle{}, Shape{})

PROC(Rectangle._set_size)(4.0, 3.0)
PRINT FN(Rectangle._info)
PRINT "Area of rectangle is "; FN(Rectangle._get_area)
PRINT "Perimeter of rectangle is "; FN(Rectangle._get_perimeter)

REM release the object from memory
PROC_discard(Rectangle{})

END
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. Project for the last three month: 3D-drawing program (currently 950 lines).

RichardRussell
Posts: 232
Joined: Tue 15 Oct 2019, 09:10

Re: OOP in BBC Basic?

Post by RichardRussell »

Ivan wrote:
Mon 09 Mar 2020, 10:20
Is it possible in any way to break the line by the colon in methods?
Yes. Exactly the same considerations apply as to 'regular' BBC BASIC procedures and functions: single line methods can be placed anywhere, but multi-line methods must be placed at the end of the program (typically after the END or QUIT statement). From a code 'elegance' point of view there is some advantage in single-line methods since you can group them together with the declaration of the class structure, whereas with multi-line methods the structure declaration and the method definitions may have to be separated.

BBC BASIC will accept a 'dummy' single-line method definition, with no implementation, in the body of the program with the 'real' definition moved to after the END, if you think that improves the clarity at all:

Code: Select all

      INSTALL @lib$+"CLASSLIB"

      REM class structure containing the members and methods
      DIM Shape{x, y, text$, _set_size, _info, _get_area, _get_perimeter}

      REM define the methods
      DEF Shape._set_size (x,y) : REM Full definition at end of program
      DEF Shape._info = Shape.text$ : ENDPROC
      DEF Shape._get_area = Shape.x * Shape.y : ENDPROC
      DEF Shape._get_perimeter = 2 * Shape.x + 2 * Shape.y : ENDPROC

      REM Register class
      PROC_class(Shape{})

      REM Program code:

      REM instance of the class
      PROC_new(Rectangle{}, Shape{})

      PROC(Rectangle._set_size)(4.0, 3.0)
      PRINT FN(Rectangle._info)
      PRINT "Area of rectangle is      "; FN(Rectangle._get_area)
      PRINT "Perimeter of rectangle is "; FN(Rectangle._get_perimeter)

      REM release the object from memory
      PROC_discard(Rectangle{})

      END

      DEF Shape._set_size (x,y)
      Shape.x = x
      Shape.y = y
      Shape.text$ = "Calculate area and perimeter:"
      ENDPROC
If you have a comment about the style or tone of this message please report it to the moderators by clicking the exclamation mark icon, rather than complaining on the public forum.

Ivan
Posts: 33
Joined: Tue 07 May 2019, 16:47

Re: OOP in BBC Basic?

Post by Ivan »

Thanks

For me it's easier to read and comment although I would prefer to have the class in one piece.

Since you moved "the attributes I think its named" to the end of the program I assume BBC Basic always starts at the first line.
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. Project for the last three month: 3D-drawing program (currently 950 lines).