PureBasic and the Object-Oriented Programming

Synthesis and notation

Before I present the selected Class implementation, I’m going to spend a little time summarizing the work made under a formal notation frame. The implementation of an object involve the following elements:

  • An Interface,
  • A Class (concrete / abstract) including the methods definition,
  • A Constructor provided with a routine initializating attributes,
  • A Destructor.

The following table summarizes what is our object in PureBasic.

  • The word Class refers to the name of the Class (ex: Methd_Class)
  • The word Mother refers to the name of the Mother Class during an inheritance (ex: Methd_ MotherClass)
  • The expressions between embraces {} are to be used during an inheritance

Interface

Interface <Interface> {Extends <MotherInterface>}
Method1()
[Method2()]
[Method3()]

EndInterface
Classe

Structure <Class> {Extends <MotherClass>}
*Methods
[Attribute1]
[Attribute2]

EndStructure

Procedure Method1(*this.Class, [arg1]…)

EndProcedure

Procedure Method2(*this.Class, [arg1]…)

EndProcedure

Structure <Mthds_Class> {Extends <Mthds_MotherClass>}
*Method1
*Method2

EndStructure

Procedure Init_Mthds_Class(*Mthds.Mthds_Class)
{Init_Mthds_MotherClass(*Mthds)}
*Mthds\Method1 = @Method1()
*Mthds\Method2 = @Method2()

EndProcedure

Mthds_Class.Mthds_Class
Init_Mthds_Class(@Mthds_Class)

Constructor

Procedure Init_Mbers_Class(*this.Class, [var1]…)
{Init_Mbers_MotherClass(*this)}
[*this\Attibute1 = var1]

EndProcedure

Procedure New_Class([var1]…)
Shared
Mthds_Class
*this.Class = AllocateMemory(SizeOf(Class))
*this\Methods = @Mthds_Class
Init_Mbers_Class(*this, [var1]…)
ProcedureReturn *this
EndProcedure

Destructor Procedure Free_Class(*this)
FreeMemory(*this)
EndProcedur

Here is an example of a code where the inheritance is used:

Ex:POO_Inheritance.pb

Contents

[1-2-3-4-5-6-7-8-9]

Top of the page