Skip to content

A matter of Class

Every class in FreePascal has surely at least one constructor and one destructor, inherited by default from the TObject class, because of the hierarchical system in which FreePascal classes are built.
While they two must be public, that is accessible from outside, any other method can be public or private; just like the variables (called properties) we need to write.
[A good programming wants to minimize the public properties, by managing them through public methods]
As well some special methods (the constructors by default and others by choice) can be defined and declared as class methods: all remaining ones are obviously object methods.

Writing a class is a part of job to use it: we must define its scope, which means in this case the "zone" where the class is visible.
Here we need that it could be disposable to Unit1 (the main one), so that we add in Unit1 a command to make it see the Employee.pas file.

We already know that the command is uses, but we put it after the TForm class definition, to distinguish it from the topper command in which we recall all libraries needed to make Unit1 work.
The Employee class's code is shown in the next page.

When we have to manage simple projects like the present one, we're able to do all job in mind: it's quite easy to build this kind of boxes.
But when your ability grow, the projects complexity too; and you're in troubles managing something like 10 classes, remembering which is directly connected to which, and who is who.
A problem so important that has pulled the world community of computing to create a new language to model and design the software, by analyzing it in its major aspects.

The UML (Unified Modeling Language) has become the standard in sector.
We only touch on it in the BoUML article: it would require some articles to show its parts, and that's what probably we'll do in future.

In the whole project (that you can download from here) we distribute not only the Lazarus files, but the UML ones too: in the folder you'll find the Simple Class file (extension: prj). Use BoUML to open it.

UML-Simple-Class
UML - Simple Class

And here beside the class diagram, where the Employee class is not only shown but explained too in its constituent parts:

the name, at the top;
the properties;
the methods.

Signs + and - mean respectively public and private.

 

If you followed and tested our previous proposals about procedural programming, then you've surely acquired confidence with code: that's why we won't explain the code in Unit1.
Just to bind the two units here used: when you add an employee you automatically create a new object, inserted into a TList's object.
When you delete a single employee from the list, automatically the list reorder itself destroying the deleted object; finally when you cloe the application, the related event delete every active object.

Finally, look at the form in the first picture: at the right and bottom you find a large button, with caption Ask the class how many objects has.
By pressing it you'll talk directly to the Employee class, obtaining the number of active or instantiated objects through the TEmployee.CountEmployees() function.