next up previous contents index search.gif
Next: 4.2 Fields Up: 4. Objects Previous: 4. Objects

4.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 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

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \begin{displaymath...
...t\ visibility\ specifier}
\end{rep} \lit*{end}
\end{displaymath}\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{heritage} \lit*( \synt{object\ type\ identifier} \lit* )\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{component\ l...
...isplaymath}\<[b] \synt{method\ definition} \\ \> \end{displaymath}\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{field\ definition} \synt{identifier\ list} \lit*: \synt{type} \lit*;\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{method\ defi...
... \\
\synt{desctuctor\ header}
\)\lit*; \synt{method\ directives} \end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{method\ dire...
...ymath}\begin{displaymath}
\lit*{abstract} \lit*;
\end{displaymath}\end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{object\ visibility\ specifier}
\(
\lit*{private} \\
\lit*{public}
\)\end{syntdiag}
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