OOP Basys and its AmigaE Implementations (Part 2)
OOP has some exalting features and put inside programmer's hands a bunch of tools for fast development of complex applications. All this has a price : you have to plan carefully object's features and behaviors BEFORE beginning writing code. This attention, which may appear stupid, can assure a large saving of time in the final creation and will permit to inherit the object with great advantages.
First of all, we'll describe the object structure, in E we'll write :
OBJECT person name:PTR TO CHAR surname:PTR TO CHAR phone:PTR TO CHAR ENDOBJECT
as you can see, we have defined a standard E OBJECT, which is normally used to create structures. As we have said before, an object is just some data organized inside a structure and some particular functions for their manipulation, so there's nothing strange if an object in E starts right with definition of an OBJECT structure, in the same way like we used to do.
Among most important functions linked to an object, we find the "constructor", used to initialize an object during its creation, and the "destructor", which minds to free resources alloocated by the object. Usually, the constructor has the same name of the object it intend to build, so in our example, it will be called "person()". The destructor, on the other hand, must have the fixed name "end()" and should NEVER be invoked, because E calls it when we decide to kill an object. Remember to close all objects created during execution of the program, before exiting, otherwise, you should live some memory allocated.
Now we'll create our person object's constructor and destructor.
Constructor will mind to allocate a 25 chars string for each field. Usually, you should not give some rigid limits to our objects, but for now we will just have to describe a constructor and we'll do it in not just a perfect way, as time goes by, we'll improve it. To create a function linked to an object in E you just have to write PROC procname() OF objname, and so we'll do now creating object's constructor (note : it will be called exactly as the object).
PROC person() OF person self.name:=String(25) self.surname:=String(25) self.phone:=String(25) ENDPROC
as you may notice, "internal" object variables are referenced with the suffix "self.".
Now we can create the destructor, called end(), and which will free all resources allocated by the object :
PROC end() OF person DisposeLink(self.name) DisposeLink(self.surname) DisposeLink(self.phone) ENDPROC
Now that our object is able to initialize and dispose itself, we can begin to write routines needed to handle internal data. We'll begin with creating two methods : set() and get() which, respectively, will mind to write and read a specified information inside the object. At the beginning of our code, write this line to create some numeric constants :
ENUM MODE_NAME=1, MODE_SURNAME, MODE_PHONE
and then we write the following set() method :
PROC set(s:PTR TO CHAR, mode=MODE_NAME) OF person SELECT mode CASE MODE_NAME StrCopy(self.name, s) CASE MODE_SURNAME StrCopy(self.surname, s) CASE MODE_PHONE StrCopy(self.phone, s) ENDSELECT ENDPROC
and the get() method :
PROC get(mode=MODE_NAME) OF person DEF s:PTR TO CHAR SELECT mode CASE MODE_NAME s:=self.name CASE MODE_SURNAME s:=self.surname CASE MODE_PHONE s:=self.phone ENDSELECT ENDPROC s
With this last procedure, we can test our object and see it working, to do this, we need a main() procedure and an instance of the object called "person". Following code creates an object person, using the set() method insert some data and then read them all with the get() method. The NEW and END commands, in deep source description and so on, will follow in the next article.
PROC main() DEF p:PTR TO person NEW p.person() -> Note the use of constructor with the NEW p.set("Fabio") -> Here we write the person's name p.set("Rotondo", MODE_SURNAME) -> Here we write the person's surname p.set("(ITA)-(0)321 - 459676", MODE_PHONE) -> Here we set the phone number. WriteF("Name:\s\nSurname:\s\nPhone:\s\n", p.get(), p.get(MODE_SURNAME), p.get(MODE_PHONE)) END p -> Killing the object, automatically we'll free also all resources CleanUp(0) -> You need this command to clear all, Wouter says that ENDPROC -> End of program
continued in next issue...
Written By: Fabio Rotondo e-mail: fsoft@intercom.it C.so Vercelli 9 28100 Novara ITALY tel: (ITA) - (0)321 459676