Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Purebasic OOP Example  (Read 4309 times)

0 Members and 1 Guest are viewing this topic.
Purebasic OOP Example
« on: June 13, 2012, 09:14:20 pm »
  • *
  • Reputation: +10/-0
  • Offline Offline
  • Gender: Female
  • Posts: 1469
So I've been busy as hell holding down two full time jobs and University at the moment, of course this doesn't mean things stop, they just crawl to a halt. I got home from work last night and had 20 minutes to spare, so I whipped up this example of how you can create and use objects in Purebasic (a procedural language).

I had several aims whilst creating this. Firstly it must not use interfaces (the way most people go about oop in purebasic), and secondly it must not use any fancy coding which will mess with the way purebasic operates. The reason for this is that most oop in purebasic creates memory that purebasic doesnt manage, forcing the user to de-allocate objects, but not other things. This causes confusion to new users.


Below is the code:
Show content
; Simple Purebasic OOP example

; Format for classes:
;   PROTOTYPES      - Only necessary for methods not in parent classes. Declared first so that the structure can read it.
;   STRUCTURE       - Variables, then methods, order doesnt really matter. - METHODNAME.PROTOTYPE
;   METHODS         - Methods with 'CLASSNAME_' before them to distinguish between them and procedures
;   CONSTRUCTOR     - At the bottom so it can set all of the methods appropriately
;

;{ Class Rectangle
Prototype Rectangle_size(*this, width.i, height.i);
Prototype Rectangle_move(*this, x.i, y.i);
 
  Structure Rectangle;
    x.i;
    y.i;
    width.i;
    height.i;
   
    size.Rectangle_size;
    move.Rectangle_move;
  EndStructure;
 
  Procedure Rectangle_size(*this.Rectangle, width.i, height.i);
    *this\width = width;
    *this\height = height;
  EndProcedure;
 
 
  Procedure Rectangle_move(*this, x.i, y.i)
    Debug "moving";
  EndProcedure;
 
  ; Constructor
  Procedure Rectangle_(*this.Rectangle);
    *this\size = @Rectangle_size();
    *this\move = @Rectangle_move();
  EndProcedure;
;}



;{ Class ColouredRectangle
  Structure ColouredRectangle Extends Rectangle;
    colour.l;
  EndStructure;
 
  ; Method override for size
  Procedure ColouredRectangle_size(*this.ColouredRectangle, width.i, height.i);
    *this\width = width;
    *this\height = height;
    Debug "Coloured sizing instead!";
  EndProcedure;
 
  ; Constructor
  Procedure ColouredRectangle_(*this.ColouredRectangle);
    Rectangle_(*this);
   
    *this\size = @ColouredRectangle_size();
  EndProcedure;
;}



; Rectangle myRect = new Rectangle()
Rectangle_(myRect.Rectangle);
myRect\size(myRect, 20,20);
Debug myRect\width;



; ColouredRectangle mycRect = new ColouredRectangle()
ColouredRectangle_(mycRect.ColouredRectangle);
mycRect\colour = RGB(20,12,20);
mycRect\size(mycRect,22,91);
Debug mycRect\colour;

I have no personal use for it, but hopefully it will come in handy to someone.
Logged
  • Elliott Parkinson
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.044 seconds with 36 queries.