Next: 4.2 Fields
Up: 4. Objects
Previous: 4. Objects
Free Pascal supports object oriented programming. In fact, most of the compiler is
written using objects. Here we present some technical questions regarding
object oriented programming in Free Pascal.
Objects should be treated as a special kind of record. The record contains
all the fields that are declared in the objects definition, and pointers
to the methods that are associated to the objects' type.
An object is declared just as you would declare a record; except that you
can now declare procedures and fuctions as if they were part of the record.
Objects can ''inherit'' fields and methods from ''parent'' objects. This means
that you can use these fields and methods as if they were included in the
objects you declared as a ''child'' object.
Furthermore, you can declare fields, procedures and functions as public
or private. By default, fields and methods are public, and are
exported outside the current unit. Fields or methods that are declared
private are only accessible in the current unit.
The prototype declaration of an object is as follows:
object types
As you can see, you can repeat as many private and public
blocks as you want.
Method definitions are normal function or procedure declarations.
You cannot put fields after methods in the same block, i.e. the following
will generate an error when compiling:
Type MyObj = Object
Procedure Doit;
Field : Longint;
end;
But the following will be accepted:
Type MyObj = Object
Public
Procedure Doit;
Private
Field : Longint;
end;
because the field is in a different section.
Remark:
Free Pascal also supports the packed object. This is the same as an object, only
the elements (fields) of the object are byte-aligned, just as in the packed
record.
The declaration of a packed object is similar to the declaration
of a packed record :
Type
TObj = packed object;
Constructor init;
...
end;
Pobj = ^TObj;
Var PP : Pobj;
Similarly, the {$PackRecords } directive acts on objects as well.
root
1999-06-10