Well... suppose now you know how to write a new class for your own needs: how to create a related object?
As human beings we know how to make it (oops! :D), but in code how much difficult can it be?
It's as difficult as variable's declaration, with one step more.
We've seen that the T in TList, TObject, ect... means Type: here a special kind of type.
So think we have already written a class called TEmployee, which has all stuff required to manage the single employee as object; then here how to do:
Hw2Sw: TEmployee;
Finished? Not at all!
By using TList we saw the necessity to create the object, or in other words to make an instantiation of it, to avoid any access error during compile time.
Every class which could be instantiated must have a function or a procedure made to create the new object: in FreePascal every class has implicitly one, thanks to the inheritance from the major ancestor class: TObject!
But because of its abstraction it normally cannot fit into your specific needs, so that you must write one (or more) new creator inside the class: in jargon it's called constructor.
And obviously this new one must overload the first old one (from TObject), so by calling the Create method you're sure that your own will be executed instead of the default.
Here an example:
type TEmployee = class private //PRIVATE ZONE (Only the employee can SEE this zone) protected public //PUBLIC (Anyone can see this zone) //PUBLIC METHODS //Constructors (Create the object Employee): constructor Create(); overload; //Public Class Method constructor Create(Name_,Surname_ : string); overload; //Public Class Method //The Overload allows you to have different versions of the same named function/procedure with different arguments //OVERLOAD : SAME NAME OF FUNCTION WITH DIFFERENT ARGUMENTS IN THE SAME CLASS ... end;
We have totally two constructors (note this word before the two Create methods); the default one has been overloaded by our first, it has no arguments, while the second one has. This choice distinguishes two cases:
- you don't assign manually name and surname, and the software will insert the default ones (first constructor);
- you assign both of them (second constructor).
So, how to create an object now?
Hw2Sw := TEmployee.Create();
Beauty, but what happens exactly?
Here we ask the class to create an object called Hw2Sw!
This is quite important, and we'll go more deeply on next article; it could be bad to overload you with an excessive amount of informations.
As you probably noticed the basic example we'll use is the same adopted until now in the procedural programming proposal described in past articles: you can look at this as a kind of polymorphism, to grant the formal continuity while the beneath arguments change in substance.
Just one last thing. We said that an object is created by the default Create method of TObject, if not differently specified.
This is true for languages or development environments like FreePascal, Delphi, Java; but C++ originally doesn't consider this hierarchical construction so that you have to write at least one constructor in any case.
See you on next article, have fun!