Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Classes
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Catalyst




PostPosted: Thu Apr 10, 2003 3:06 pm   Post subject: [Tutorial] Classes

Classes

Classes are a very useful way to organize information in programming.

Your basic class organisation looks like this:
code:

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:

code:

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:
code:


var BMW:pointer to Car
%% can be shorten to var BMV:^Car



To create the instance you must use the new command:

code:


new Car,Volvo
%% new ClassName,VariablePointer



Now to access the things you have exported you must use the -> notation
For example:

code:

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.
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Thu Apr 10, 2003 3:52 pm   Post subject: (No subject)

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
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Tony




PostPosted: Thu Apr 10, 2003 5:33 pm   Post subject: (No subject)

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 Confused

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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Catalyst




PostPosted: Thu Apr 10, 2003 6:12 pm   Post subject: (No subject)

modules dont need instances, you can just use the procs from the modules without dealing with new and pointers

for examples:

code:

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




PostPosted: Sun Apr 13, 2003 12:16 am   Post subject: (No subject)

Hope you're not offended if I add to this.

Take the class that the turing help file gives you, Stack

code:

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




PostPosted: Sun Apr 13, 2003 12:50 am   Post subject: (No subject)

im not offended at all, as i said the tutorial i made is far from complete
rizzix




PostPosted: Sun Jun 29, 2003 10:12 pm   Post subject: (No subject)

tony wrote:
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




PostPosted: Sun Jun 29, 2003 10:28 pm   Post subject: (No subject)

yeah, I figured out the difference already,

thx for the explanation though Smile
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
UBC_Wiskatos




PostPosted: Tue Jul 01, 2003 2:23 pm   Post subject: (No subject)

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




PostPosted: Tue Jul 01, 2003 2:30 pm   Post subject: (No subject)

yea

code:

free p_str


wher p_str is a pointer.
turing pointers are not really pointers. but more like refrences.. Confused
Catalyst




PostPosted: Tue Jul 01, 2003 2:31 pm   Post subject: (No subject)

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




PostPosted: Tue Jul 01, 2003 2:32 pm   Post subject: (No subject)

rizzix wrote:
yea

code:

free p_str


wher p_str is a pointer.
turing pointers are not really pointers. but more like refrences.. Confused


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. Razz
UBC_Wiskatos




PostPosted: Tue Jul 01, 2003 2:34 pm   Post subject: (No subject)

Catalyst wrote:
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




PostPosted: Tue Jun 07, 2005 7:52 pm   Post subject: 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. Sad
Delos




PostPosted: Tue Jun 07, 2005 9:33 pm   Post subject: (No subject)

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...
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: