Tutorial: PureBasic and the OOP
Tutorial: PureBasic and the OOP
Hi everybody!
Because a tutorial doesn’t exist to clarifies this purpose in PureBasic, find here a tutorial dealing with OOP implementation in PureBasic
Tutorial: PureBasic and the OOP
This tutorial does not explain the reason to be of the object-oriented programming but simply how we can implement it in a language as PureBasic.
If the tutorial resumes the definitions of the OO concepts, it is to be in agreement with the reader on the concept definitions that will be employed.
Nevertheless let me try to suggest in some lines the interest of OO for those who wish it:
For me, the object concept brings a concrete answer to the developer in the way of conceptualize its code.
To conceptualize, understand that we group the alike elements, to work at first on the common similarity (object classes) and postpone as far as possible the differences (objects themselves).
This approach suggests coding only the minimum because we limit the redundancy.
A classic example to illustrate:
We can describe a rectangle and a circle in an independent way and thus create so many different codes.
Nevertheless both are forms: thus they have some similarities.
The purpose of the object-oriented approach is to bring the developer to implement only once the similarity. The advantages are normally: fewer codes, more light, a code more strong in front of modifications …
This example is simple and, of course, procedural approach also allows to reduce a such redundancy.
Simply the object-oriented programming suggests structuring its procedural programming to have a better answer to this requirement.
Every programmer tries to simplify his codes and the object-oriented is a technique to achieve this goal.
But to completely feel the advantage of the object representation, I can only advise you to learn also about the design patterns.
These last ones do not deal as a language in particular (but it must however be object-oriented). They process common programming problems and give an adapted solution in term of limiting redundancies.
To my taste, they give all the sense to the OOP.
But don’t imagine that OOP has got only advantages. Some running performances are lost because of many memory allocations and functions call.
Well! I hope simply that my english translation will be good enough and made the tutorial also pleasant and profitable for you in the reading, than it was for me in its writing…
Because a tutorial doesn’t exist to clarifies this purpose in PureBasic, find here a tutorial dealing with OOP implementation in PureBasic
Tutorial: PureBasic and the OOP
This tutorial does not explain the reason to be of the object-oriented programming but simply how we can implement it in a language as PureBasic.
If the tutorial resumes the definitions of the OO concepts, it is to be in agreement with the reader on the concept definitions that will be employed.
Nevertheless let me try to suggest in some lines the interest of OO for those who wish it:
For me, the object concept brings a concrete answer to the developer in the way of conceptualize its code.
To conceptualize, understand that we group the alike elements, to work at first on the common similarity (object classes) and postpone as far as possible the differences (objects themselves).
This approach suggests coding only the minimum because we limit the redundancy.
A classic example to illustrate:
We can describe a rectangle and a circle in an independent way and thus create so many different codes.
Nevertheless both are forms: thus they have some similarities.
The purpose of the object-oriented approach is to bring the developer to implement only once the similarity. The advantages are normally: fewer codes, more light, a code more strong in front of modifications …
This example is simple and, of course, procedural approach also allows to reduce a such redundancy.
Simply the object-oriented programming suggests structuring its procedural programming to have a better answer to this requirement.
Every programmer tries to simplify his codes and the object-oriented is a technique to achieve this goal.
But to completely feel the advantage of the object representation, I can only advise you to learn also about the design patterns.
These last ones do not deal as a language in particular (but it must however be object-oriented). They process common programming problems and give an adapted solution in term of limiting redundancies.
To my taste, they give all the sense to the OOP.
But don’t imagine that OOP has got only advantages. Some running performances are lost because of many memory allocations and functions call.
Well! I hope simply that my english translation will be good enough and made the tutorial also pleasant and profitable for you in the reading, than it was for me in its writing…
Last edited by Dräc on Fri Sep 21, 2007 11:22 am, edited 1 time in total.
Initially I was about to write: "Where's the tutorial?!"
Now I just saw the big letters are supposed to be a link... :roll:
I didn't read your pages in detail but it looks like you did some really
great work there! Well done layout, too. Very cool!
Now I just saw the big letters are supposed to be a link... :roll:
I didn't read your pages in detail but it looks like you did some really
great work there! Well done layout, too. Very cool!
Good programmers don't comment their code. It was hard to write, should be hard to read.
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
- Psychophanta
- Addict
- Posts: 4969
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Lípetsk, Russian Federation
- Contact:
Dräc, THANKS a lot
I enjoy it
I enjoy it
http://www.zeitgeistmovie.com
While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB
While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB
Very nice work. I tried OOP myself with instances in linkedlist, but your solution is more sophisticated. I altered your example a little bit with my ideas, perhaps you could improve your code with it.
Greetings
Leo
Greetings
Leo
Code: Select all
; POO_Heritage.pb [http://drac.site.chez.tiscali.fr/Tutorials Programming PureBasic/indexTutorials.htm#POO]
; Dräc - 2005
;
; Cet exemple montre comment une Classe concrète (Rect1) hérite d'une Classe abstraite (Form)
; Elle montre aussi comment accéder aux attributs d'un objet: soit par des méthodes, soit par un pointeur sur l'objet.
;
; This example shows how a concrete Class (Rect1) inherits from an abstract Class (Form)
; It shows also how to access to object attributes: either by Methods, or by pointer on the object.
Interface Form
Draw()
Cut()
Get_var1()
Get_var2()
EndInterface
Structure Form_
*Methods.l
var1.l
var2.l
EndStructure
Procedure Draw_Form(*this.Form_)
Debug "Draw_Form "
EndProcedure
Procedure Cut_Form(*this.Form_)
Debug "Cut_Form"
EndProcedure
Procedure Get_var2_Form(*this.Form_)
ProcedureReturn *this\var2
EndProcedure
Structure Methods_Form
Draw.l
Cut.l
Get_var1.l
Get_var2.l
EndStructure
Procedure Init_Methods_Form(Ptr_Methods.l)
*Methods.Methods_Form=Ptr_Methods
*Methods\Draw=@Draw_Form()
*Methods\Cut=@Cut_Form()
*Methods\Get_var2=@Get_var2_Form()
EndProcedure
Methods_Form.Methods_Form
Init_Methods_Form(@Methods_Form)
; Ici la méthode Get_var1() n'est pas implémentée: la Classe Form est une Classe Abstraite
; On n'a donc pas besoin de déclarer de Constructeur ni de Destructeur de cette Classe
; Here the method Get_var1() is not implemented: the Class Form is an Abstract Class
; Thus we don't need to declare a Constructor or a Destructor of this Class
Procedure Init_New_Form(*this.Form_, var1.l, var2.l)
*this\var1=var1
*this\var2=var2
EndProcedure
Procedure.l New_Form(var1.l, var2.l)
Shared Methods_Form
*this.Form_ = AllocateMemory(SizeOf(Form_))
*this\Methods=@Methods_Form
Init_New_Form(*this, var1, var2)
ProcedureReturn *this
EndProcedure
Procedure Free_Form(*this)
FreeMemory(*this)
EndProcedure
; ----------------------------------
Interface Rect1 Extends Form
Erase()
Get_var4()
EndInterface
Structure Rect1_ Extends Form_
var3.l
var4.l
rectname.s
EndStructure
Procedure Draw_Rect1(*this.Rect1_)
Debug "Draw_Rect1 "+ *this\rectname
EndProcedure
Procedure Erase_Rect1(*this.Rect1_)
Debug "Erase_Rect1"
EndProcedure
Procedure Get_var1_Rect1(*this.Rect1_)
ProcedureReturn *this\var1
EndProcedure
Procedure Get_var4_Rect1(*this.Rect1_)
ProcedureReturn *this\var4
EndProcedure
Structure Methods_Rect1 Extends Methods_Form
Erase.l
Get_var4.l
EndStructure
Procedure Init_Methods_Rect1(Ptr_Methods.l)
Init_Methods_Form(Ptr_Methods)
*Methods.Methods_Rect1=Ptr_Methods
*Methods\Draw=@Draw_Rect1()
*Methods\Erase=@Erase_Rect1()
*Methods\Get_var1=@Get_var1_Rect1(); La Classe concrète Rect1 se charge de donner l'implémentation de Get_var1()
*Methods\Get_var4=@Get_var4_Rect1(); The concrete Class Rect1 takes care to give the implementation of Get_var1() method
EndProcedure
Methods_Rect1.Methods_Rect1
Init_Methods_Rect1(@Methods_Rect1)
Procedure Init_New_Rect1(*this.Rect1_, var1.l, var2.l, name.s, var4.l)
Init_New_Form(*this, var1,var2)
*this\rectname=name
*this\var4=var4
EndProcedure
Procedure.l New_Rect1(var1.l, var2.l, name.s, var4.l)
Shared Methods_Rect1
*this.Rect1_ = AllocateMemory(SizeOf(Rect1_))
Debug *this
*this\Methods=@Methods_Rect1
Init_New_Rect1(*this, var1, var2, name, var4)
Debug *this
ProcedureReturn *this
EndProcedure
Procedure Free_Rect1(*this)
FreeMemory(*this)
EndProcedure
; ----------------------------------
Rect.Rect1 = New_Rect1(1, 2, "leo",6)
Rect2.Rect1 = New_Rect1(3, 4, "hannie",7)
Form2.Form = New_Form(8,9)
Debug ">> Method Test"
Rect\Draw()
Rect\Cut()
Rect\Erase()
Rect2\Draw()
Form2\Draw()
Form2\Cut()
Debug""
Debug ">> Access Test"
*Rect.Rect1_= Rect
Debug *Rect\var1
Debug Rect\Get_var1()
Debug *Rect\var4
Debug Rect\Get_var4()
Debug *Rect\rectname
*Rect2.Rect1_= Rect2
Debug *Rect2\var1
Debug Rect2\Get_var1()
Debug *Rect2\var4
Debug Rect2\Get_var4()
Debug *Rect2\rectname
*Form2.Form_=Form2
Debug *Form2\var2
Debug Form2\Get_var2()
Debug""
Debug ">> Destruction Test"
Free_Rect1(Rect)
Free_Rect1(Rect2)
Free_Form(Form2)
;Rect\Draw(); --> Impossible car l'objet Rect n'existe plus! / Impossible because Rect objet doesn't exist!
Thanks a lot to everybody.
@Leo: Good improvement, leading to a more homogeneous code.
I will up-date the tutorial in this way… (including certainly an explicit representation of the constructor variable arguments which I had pushed aside until now.)
Precisely concerning linkedlist, the tutorial section called “Allocation” is planned to deal with them soon, just because it is another mean to perform instanciation
@Leo: Good improvement, leading to a more homogeneous code.
I will up-date the tutorial in this way… (including certainly an explicit representation of the constructor variable arguments which I had pushed aside until now.)
Precisely concerning linkedlist, the tutorial section called “Allocation” is planned to deal with them soon, just because it is another mean to perform instanciation
i'm not a native english speaker so i tend to stumble over some of the phrasing... is it just me?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )