Object Oriented Programming (OOP) is the idea that you can treat your custom types like objects. An object has things and does things.
As an example, let’s use a rock. Let’s say we want to simulate a rock. A rock HAS a size and a color. It also DOES things, like rolling and gathering moss. I’m sure that if we really wanted to think about it we could find more things that a rock either HAS or DOES, but for now, we will leave it at that. Here is a small chart:
- ROCK
- HAS
- color
- size
- DOES
- roll
- gather moss
Code: Select all
Type ROCK
'HAS
color as uinteger
size as integer
'DOES
Declare Sub roll()
Declare Sub gatherMoss()
End Type
Sub ROCK.roll()
'Code here
End Sub
Sub ROCK.gatherMoss()
'Code here
End Sub
Code: Select all
Dim pebble as ROCK
pebble.size = 1 'It’s a small rock
pebble.color = RGB(200,200,200) 'Grey-ish in color
pebble.roll()
pebble.gatherMoss()
0
Code: Select all
Type PERSON
Private:
bigHairyMole as Long 'It’s REALLY big…
spiderVanes as Single 'As in relationship status…
Declare Sub removeMole(Doctor as String)
Declare Sub cryInPillow()
Public:
face as byte 'because of the teeth
name as string 'Gotta have one of these
myCash as single 'Sweet money
Declare Sub read(text as String)
Declare Sub giveToCharity(money as Single)
End Type
'Put the code for all your subs here
Code: Select all
Sub PERSON.giveToCharity(money as Single)
this.myCash = this.myCash – money
this.cryInPillow()
End Sub
Before I go too much further, let me cover a few points of syntax. First, to supply the code for a sub or function that are declared inside a type, you use the type name followed by a dot, then the sub or function name. Also THIS was used in the above examples. This is used to refer to things which are part of the type. This is not strictly required, unless you have two subs, functions, or variables of the same name, one of which is in the type. I use it all the time anyway because it makes code easier to read for me.
Now here is an example of code to use the person type:
Code: Select all
Dim Imortis as PERSON
Imortis.name = “Imortis Inglorian”
Imortis.myCash = 10.00
Imortis.giveToCharity(10.00) 'This works fine
Imortis.removeMole(“Dr. Gupta”) 'This won’t work… Too public…
Next up is some of the more advanced OOP topics.
Properties are what I call Getters and Setters. They allow you to get and set values. Now, you may ask, why would you want to do that when you could easily get and set values using “=”? The answer is simple: Presentation. It makes it look better when used in code. Tell me which looks better, this:
Code: Select all
Player.x =1
Player.y =1
Code: Select all
Player.coordinates(1,1)
Properties are set up very similar to subs and functions in types:
Code: Select all
Type Player
_x as integer
_y as integer
Declare Property X() as integer
Declare Property X(value as integer)
Declare Property Y() as integer
Decalre Property Y(value as integer)
End Type
Property Player.X() as integer
Return this._x
End Property
Property Player.X(value as integer)
this._x = value
End Property
Property Player.Y() as integer
Return this._y
End Property
Property Player.Y(value as integer)
this._y = value
End Property
Code: Select all
Dim me as Player
me.X = 10
me.Y = 10
Print me.X
Print me.Y
Next let’s talk about operator overloading. This is the ability to change the way +, -, *, /, =, and others work for your type.
Let's say you want to add the ages of two people. First we need a person type:
Code: Select all
Type PERSON
name as String
age as Ubyte 'Who is going to live over 255 years?
End Type
Code: Select all
Dim Imortis as PERSON
Dim bigsofty as PERSON
Dim combinedAges as Integer
combinedAges = Imortis.age + bigsofty.age
Code: Select all
Type PERSON
name as String
age as Ubyte 'Who is going to live over 255 years?
Declare Operator Cast() as Integer
End Type
Operator PERSON.Cast() as Integer
Return Int(this.age)
End Operator
Code: Select all
Dim Imortis as PERSON
Dim bigsofty as PERSON
Dim combinedAges as Integer
combinedAges = Imortis + bigsofty
Next up is the Constructor. A constructor in a type is a chunk of code that runs as soon as the variable is DIMed. This is useful for many things. A good example of wanting to do this is with videogames. In most games the player starts with 3 to 5 lives.
Code: Select all
Type PLAYER
lives as Integer
x as Integer
y as Integer
Declare Constructor()
End Type
Constructor PLAYER()
this.lives = 3
End Constructor
Dim pacMan as PLAYER
Print pacMan.lives
The Destructor is like the Constructor except it happens when you delete a variable or when it goes out of scope. It is particularly useful when you make extensive use of pointers, Allocate, and New. But that is a totally different tutorial. For now, just know that a destructor happens when a variable is removed from scope, or when it is deleted using Delete, or deallocate.
Well, that’s all I got. If you have any questions, just ask and I’ll see If I can clarify.