5.1 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 elds that are declared in the objects denition, and pointers to the methods that are associated to the objects' type.

An object is declared just as a record would be declared; except that now,procedures and functions can be declared as if they were part of the record. Objects can "inherit" elds and methods from "parent" objects. This means that these elds and methods can be used as if they were included in the objects declared as a "child" object.

Furthermore, a concept of visibility is introduced: elds, procedures and functions can be delcared as public or private. By default, elds 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
-- --|---------object--|-------|------------------------end--------
     -packed -         heritage- -6----component list-------
                                - object visibility speci er -

-- --heritage -(- object type identi er-) --------------------------------

-- --            --------------------------------------------------
     component list --- eld de nition --| --method de  nition--|
                    6------------|   6---------------|

-- --           -          -  -    -  ------------------------------
       eld de  nition identi  er list  :  type  ;

-- --method de nition ----function header----;-method directives------------
                    - procedure header -|
                    -constructor header|
                      desctuctor header

-- --method directives-------------------------------------------------
                    | virtual  ;  -       - --||  call modi ers  ;
                    ------       abstract--;---|
                           reintroduce  ;

-- --object visibility speci  er|-private-----------------------------------
                         -protected--|
                           public
___________________________________________________________________

As can be seen, as many private and public blocks as needed can be declared. Method definitions are normal function or procedure declarations. Contrary to TP and Delphi, elds can be declared after methods in the same block, i.e. the following will generate an error when compiling with Delphi or Turbo Pascal, but not with FPC:

Type  
  MyObj = Object  
    Procedure Doit;  
    Field : Longint;  
  end;

Remark: Free Pascal also supports the packed object. This is the same as an object, only the elements (elds) 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 f$PackRecords g directive acts on objects as well.