
-----------------------------------
Catalyst
Thu Apr 10, 2003 3:06 pm

[Tutorial] Classes
-----------------------------------
Classes

Classes are a very useful way to organize information in programming.

Your basic class organisation looks like this:

class ClassName

%Variables and Procedures/Functions go here

end ClassName

Classes can be thought as objects. Each instance  of a class contains all the variables declared in the class and all the internal functions. 

To get started lets make a simple class:


class Car

%% This tells us what can be used outside the class
%% if not listed here it cannot be used outside the class
export setCarAttributes,putCarAttributes

%% These are the variables in the class
var name:string
var Length,cost:int

%% These procedures modify the data in the class and output it
proc setCarAttributes (nam:string,l,c:int)

name:=nam
Length:=l
cost:=c

end setCarAttributes

proc putCarAttributes 

put "Car name: ",name
put "Car Length: ",Length
put "Car Cost: ",cost

end putCarAttributes

end Car


If you've tried this code you've seen it doesnt do anything
to use the class you need to create an instance of it

This is done using a pointer to the class
A pointer is a variable that is used to access the class
Here is an example:


var BMW:pointer to Car 
%% can be shorten to var BMV:^Car



To create the instance you must use the new command:



new Car,Volvo
%% new ClassName,VariablePointer



Now to access the things you have exported you must use the -> notation
For example:


var Volvo : ^Car

new Volvo

Volvo -> setCarAttributes ("Volvo", 100, 20000)
Volvo -> putCarAttributes 


This tutorial is far from complete so if anyone has any questions, please post or pm me.

-----------------------------------
Dan
Thu Apr 10, 2003 3:52 pm


-----------------------------------
thanks for the totural, this can be very usfull for a lot of resions. also this whode help poleop figher out how to use your 3d enginon  :wink: . 

+ 25 bits

-----------------------------------
Tony
Thu Apr 10, 2003 5:33 pm


-----------------------------------
thx Catalyst, very useful tutorial.

just some things to add:

Classes are a good way of organizing your code into modules that you can ad to your programs. Actually if anyone can tell me the difference between turing's modules and classes, it'd be appretiated.

anyway, they work the same way. I think :?

Take Draw for example. Draw is a module (or class) that contains a set of functions such as .box and .line Its like a program of its own, but you can access parts of it from YOUR program.

Point is, C++ uses a lot of classes since they're well supported and it might be a good idea to write a class for something you use a lot. Such as Catalyst's 3D engine. I'm sure its writen as a class.

-----------------------------------
Catalyst
Thu Apr 10, 2003 6:12 pm


-----------------------------------
modules dont need instances, you can just use the procs from the modules without dealing with new and pointers

for examples:


module Draw2
    export FillBack
    proc FillBack (c : int)
        drawfillbox (0, 0, maxx, maxy, c)
    end FillBack
end Draw2

Draw2.FillBack (2)

i think that y the pic module doesnt go over 1000 since its all in an array
(i could be wrong)

-----------------------------------
Martin
Sun Apr 13, 2003 12:16 am


-----------------------------------
Hope you're not offended if I add to this.

Take the class that the turing help file gives you, Stack


class stackClass    % Template for creating individual stacks
   export push, pop
        
   var top : int := 0
   var contents : array 1 .. 100 of string
        
   procedure push (s : string)
         top := top + 1
         contents (top) := s
   end push
        
   procedure pop (var s : string)
         s := contents (top)
         top := top - 1
   end pop
end stackClass


This is the basic setup of a class.

Now, the first thing that we should ask ourselves is, 'What the hell is a stack anyway?'
A stack is well, a stack. It is Last in, first out (LIFO); that is, the last element added to it is the first one that is removed. Picture a pile of dirty dishes. As people finish their meals, they pile them up on the table. More than likely, the top one will be cleaned, then put away, and the second from the top will be cleaned, and so on. This is what a stack is. To push something onto the stack, you are basically putting a new plate on the pile to be cleaned. To pop something from the stack, you have cleaned the plate and have put it away.
In the stackClass, you can have up to 100 elements of type string. A stack can be anything; integers, real numbers, strings...and it does not have to be limited in size to 100, this is just the number that whatever programmer at holtsoft chose to use.

Now, why use a class for this? Well, instead of just having one pile of dirty dishes, you now have n piles. Each pile has basically the same properties (They are all dirty dishes waiting to be cleaned), but has different values in each one. That is what a class is for. Classes are used to create objects, and, in this case, the object is a stack. This has many applications (classes, and stacks). Take the icons on your desktop. Each one is essentially the same, that is, they do the same basic function: Link to a target. They each have their own unique properties as well, however, such as location, the target's location, picture, name, etc. When Windows was created, the programmers did not know how many icons you would have on your desktop, nor did they know what each icon would do, so they were created using classes. 

I'm getting really tired (it's 1am), so I'll finish this tomorrow, talking about objects and such.

-----------------------------------
Catalyst
Sun Apr 13, 2003 12:50 am


-----------------------------------
im not offended at all, as i said the tutorial i made is far from complete

-----------------------------------
rizzix
Sun Jun 29, 2003 10:12 pm


-----------------------------------
thx Catalyst, very useful tutorial.
Actually if anyone can tell me the difference between turing's modules and classes, it'd be appretiated.


No they are two completly different concepts.
You can think of a module as a container for code that has it's own scope.
When u call a procedure/function in a module, u are basically calling code form a foreign namespace. No benefits here since, any modification to variables in the global scope of the module stick on.

Classes are different.
Think of classes as templates to create objects.
To create an object, means to create an instance of a class.
Each object instance has it's own state. Which means it has it's own scope. The only common thing between objects of a perticular class are static vairables/methods (this i not supported by turing!).

This idea of oop (object oriented programming) is to have different objects intereact with each other.

Correctly implemeted OOP programs _always_ include the follow concepts:

1] polymorphism
2] encapsulation
3] inheritance
4] abstraction

to go into more detail on these topics wil take forever.
Have a visit at the Java Tutorials. They probably give out a better clue on all this..

-----------------------------------
Tony
Sun Jun 29, 2003 10:28 pm


-----------------------------------
yeah, I figured out the difference already,

thx for the explanation though :)

-----------------------------------
UBC_Wiskatos
Tue Jul 01, 2003 2:23 pm


-----------------------------------
So there are pointers to classes in Turing? I'm a bit confused, since from what I see, it always allocates the class dynamically (to the heap). Do you have to do garbage collection (That is, if you go in C++, CClass *foo = new Class, somewhere down the line you have to say delete Class or else it'll stay in memory)?

Maybe I'm wrong and it allocates the class to the stack, but from what I see, the Turing code is equivalent to this:

class CFoo
{
public:
void Test();
}

------

CFoo *pTest;
pTest = new CFoo;

pTest->Test();

or

(*pTest).Test();

Now... What is the equivalent of doing:

CFoo myClass;
myClass.Test();

-----------------------------------
rizzix
Tue Jul 01, 2003 2:30 pm


-----------------------------------
yea


free p_str


wher p_str is a pointer.
turing pointers are not really pointers. but more like refrences..  :?

-----------------------------------
Catalyst
Tue Jul 01, 2003 2:31 pm


-----------------------------------
the pointers in turing are superficially like the ones in c++ but they are very different. They are only used with classes. Yes, turing collects its own garbage. (there are some commands deep in the language that are similar to c-type pointers tho)

-----------------------------------
UBC_Wiskatos
Tue Jul 01, 2003 2:32 pm


-----------------------------------
yea


free p_str


wher p_str is a pointer.
turing pointers are not really pointers. but more like refrences..  :?

I see, I don't know, it's just the syntax is strinkingly similar to initializing a class to the heap, as you make a pointer, then you have a New operator, then you use the -> operator to access the class. Meh, whatever.  :P

-----------------------------------
UBC_Wiskatos
Tue Jul 01, 2003 2:34 pm


-----------------------------------
the pointers in turing are superficially like the ones in c++ but they are very different. They are only used with classes. Yes, turing collects its own garbage. (there are some commands deep in the language that are similar to c-type pointers tho)

Ah, ok! That's sort of what I thought... Thanks a lot!

-----------------------------------
Drake
Tue Jun 07, 2005 7:52 pm

Classes (Duh!)
-----------------------------------
Your tutorial is very useful.  It's a little confusing to me, but I'm pretty sure that if I actually sit down and really work on it, it will all make sense.  

What I can't for the life of me figure out is how to connect classes and modules between different files in turing.  For example, I am trying ot make my own RPG.  I tried doing it all in one file, and I ran into quite a few problems (namely things got -really- messy, very fast).  So I decided that I would split everything up into smaller, more managable bits.  This has obviously gone disastrously because I have -no- idea how to do what I want to do.  I went around looking through all the classes that Turing comes with (Draw and others, for example) and those make sense.  It's just calling those files up in the main program that I have trouble with.  Also, the help files that turing comes with, up until this point, were pretty good about giving me what I wanted.  I just go around in circles now, looking for the answers.

Maybe I'm just an idiot, but it just doesn't make any sense to me how to get this to work correctly. :(

-----------------------------------
Delos
Tue Jun 07, 2005 9:33 pm


-----------------------------------
If you're thinking of making an RPG, you need to very comfortable working with classes, modules, and parameters.  Of course procedures and the oft-hailed functions are a given.  If you can understand the importance of following statement (a quote by Cervantes) you're on a good path:

[paraphrased]
All local constructs should be oblivious of the outside world, but entirely versed in their own arguments.

Also, a quote I love:

[again paraphrased]
Throw some extraneous code in there, that'll confuse 'em!

Don't bother looking into Turing's predefs, they'll just confuse you.  Look around for Catalyst's Particle Engine.  It's entirely object oriented, so you can get a feel for how classes should work.  Keep in mind that a class is best used when you want to have multiple instances of an object present...

-----------------------------------
Drake
Tue Jun 07, 2005 9:46 pm


-----------------------------------


Well, duh!  I'm lazy, so making them actually aware of anything else would be too much extra work.



Kind of not quite what I am trying to do here.  But in the end I will do something like that.  :)

And on a happier note, I actually got the darn thing to work right.  In fact, it works better than I expected it to (long story...).  But now I have another problem: I can only import one thing per file, and I'm already importing the module that controls the health and such.  I also want to get in some GUI buttons.  But every time I try to put in another import, it says there is an error.  What am I doing wrong, or is this just another one of those things that I'm just supposed to accept because Turing isn't smart enough to do it?

-----------------------------------
Cervantes
Wed Jun 08, 2005 2:44 pm


-----------------------------------
What am I doing wrong, or is this just another one of those things that I'm just supposed to accept because Turing isn't smart enough to do it?
Actually, Alan Turing was a genius.  :)
Instead of importing stuff on seperate lines (a la following:

import BSprite in "BSprite.tu"
import DanSprite in "DanSprite.tu"

), we do it like this:

import BSprite in "Bsprite.tu", DanSprite in "DanSprite.tu"


-----------------------------------
Drake
Wed Jun 08, 2005 5:48 pm


-----------------------------------
Heh, yeah.  I figured that out a few hours after I posted my last post.  Seems to be happening a lot to me recently.  Thanks, though! :D

-----------------------------------
lyam_kaskade
Thu Jun 09, 2005 9:41 pm


-----------------------------------
Hmmmm... I wonder what Alan Turing would think of Turing (the language)?

Let's take a look shall we?


Wow I must be bored...

-----------------------------------
[Gandalf]
Thu Jun 09, 2005 9:50 pm


-----------------------------------
Good comic :lol:

I only realized there was a new version of Turing (for windows) a little while ago since all my "Turing" results came from Alan Turing :).
