tk3 (home, dev, source, bugs, help)

RPGCodeObject Oriented Coding — Methods and Using

special methods

There are several reserved functions that have special purposes.

method CDate(month!, day!, year!)

The constructor is a function that is called when a new variable of type CDate is created (you still don't know a way to create such variables, see Using Classes for this information). This function is always named the same as the class itself. Its argument list is provided by the creator when the variable - or object (an instance of a class), more specifically - is created.

method ~CDate()

This function - the deconstructor - was not required in the implementation of CDate, but takes the format ~className and never has arguments. As the name insinuates, this function is called when an object of type CDate is destroyed. Note that the member variables still have their values when this function is called so you can use them in deinitialization tasks, they will not be killed until the call finishes.

method release()

The release function is automatically a member of every class - you need never code it. Calling the release function calls an object's deconstructor, kills its members variables, then the object itself.

method getType()

As with the last method, you should never provide code for getType, rather every class automatically has it as a member. This function returns the type of the object as a human readable, capital, string.

using classes

Now that you know how to create classes, you'll need to know how to use them in your code. The most important task will be, of course, to create an object. This is accomplished by using the class' name as a function.

method theClass([constructor arguments])

This function returns an object of type theClass, calling its constructor with the arguments provided if it requires any. Armed with this function, you can now use the CDate class at a basic level:

// Create a new date: January 31st, 2005
p = CDate(1, 31, 2005)

// Display the date
p->display()

// Free the date
p->release()

As you see, the -> operator is used for calling class methods.

You'll note that object names do not contain a type declaration character. You can, however, append a ! when referencing an object to obtain a unique number representing that object, and you should include it when passing objects as parameters.

This knowledge may seem like all you need to operate your classes, but some potential problems exist.


previous, forward