next up previous contents index
Next: Fields Up: Objects Previous: Objects

Declaration

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

syntdiag2022

syntdiag2026

syntdiag2030

syntdiag2034

syntdiag2038

syntdiag2042

syntdiag2046

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:
listing1964
But the following will be accepted:
listing1966
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 :
listing1969
Similarly, the {$PackRecords } directive acts on objects as well.


next up previous contents index
Next: Fields Up: Objects Previous: Objects

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998