It is currently Thu Jul 18, 2019 8:04 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 35 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Tutorial: PureBasic and the OOP
PostPosted: Mon May 09, 2005 9:56 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Oct 09, 2004 12:10 am
Posts: 150
Location: Toulouse (France)
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…


Last edited by Dräc on Fri Sep 21, 2007 11:22 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 09, 2005 10:46 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 16, 2003 3:47 pm
Posts: 1310
Location: England
Dräc,

Those pages are beautiful. The Articles are well written, too.

It must have taken you ages!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 09, 2005 11:16 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sun Apr 27, 2003 4:41 pm
Posts: 1661
Location: Germany
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!

_________________
Good programmers don't comment their code. It was hard to write, should be hard to read.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 10, 2005 12:26 am 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
Nice work!!!! *gets reading glasses on* :D

_________________
--Kale

Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 10, 2005 7:46 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Jan 24, 2004 6:56 pm
Posts: 768
Good work! In my opinion, this should be packaged as a PDF and bundled with every PB download!

Thanks!

_________________
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 10, 2005 8:15 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Apr 27, 2005 9:41 pm
Posts: 150
Location: Finland
Thanks! This is one of tutorials ive been missing... :D


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 10, 2005 8:52 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13595
Location: France
This is a very good paper ! (BTW, some french words are still left: constructeur, destructeur, also POO (for Programmation Orienté Objet - don't get me wrong :wink:) in the main title should be changed to OOP).


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 10, 2005 11:48 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Apr 25, 2003 4:57 pm
Posts: 573
Wow! will try to read all of this...
- This is a great contribution.

_________________
Ta - N


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 10, 2005 6:03 pm 
Offline
Enthusiast
Enthusiast

Joined: Sun Aug 29, 2004 2:25 pm
Posts: 149
Location: United Kingdom
This is great, i've never really understood OOP but it looks like if i read your site, i might ;)

Very good job :)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 10, 2005 11:48 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4568
Location: Spa, relaxing and thinking, and learning...
Dräc, THANKS a lot :D
I enjoy it

_________________
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 13, 2005 7:17 pm 
Offline
User
User

Joined: Sat Aug 02, 2003 8:48 pm
Posts: 21
Location: Netherlands, Wijk bij Duurstede
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

Code:
; 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!



Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 13, 2005 11:05 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Oct 09, 2004 12:10 am
Posts: 150
Location: Toulouse (France)
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat May 14, 2005 9:29 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Oct 09, 2004 12:10 am
Posts: 150
Location: Toulouse (France)
Leo,
Just a little remark on your example,
Rigorously, an object from the class Form should not be created because it is an abstract class.
Be careful that Form2/Get_var1() is not defines…


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat May 14, 2005 1:42 pm 
Offline
Moderator
Moderator

Joined: Sat Dec 27, 2003 3:55 am
Posts: 3321
Location: Great Southern Land
Awesome.

Thank you!


dell_jockey wrote:
Good work! In my opinion, this should be packaged as a PDF and bundled with every PB download!


Agreed. Or chm'd ordoc'ed or something and zipped.

Very nice indeed!

_________________
@}--`--,-- A rose by any other name ..


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat May 14, 2005 7:34 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 6061
i'm not a native english speaker so i tend to stumble over some of the phrasing... is it just me?

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 35 posts ]  Go to page 1, 2, 3  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye