6.1 Class denitions

The prototype declaration of a class is as follows :

_________________________________________________________________________________________________________ Class types
-- ------------     ---------------------------------    -----------
     -       -|class  -      | -----            -----| end
      packed          heritage   6clascso vmipsiobnilietyntsp liestci  er|

-- --heritage -(- class type identi er-) ---------------------------------

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

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

-- --              -------------            ----- --
     method de nition  | -    -| - function header-|| ;
                    ---class    procedure header--|
                    ------codnessctrtuuccttoorr h heeaaddeerr-----|
- -----------------------------------------------------------------
     ---- virtual------------------ ;--|-call modi ers -;--|
      |          -;- abstract -|  |
      |---------override ---------|
      -message -|integer constant--
                --string constant---

-- --class visibility speci  er--private ------------------------------------
                        |protected -|
                        |-public --|
                        -published -|
___________________________________________________________________

As many private, protected, published and public blocks as needed can be repeated. Methods are normal function or procedure declarations. As can be seen, the declaration of a class is almost identical to the declaration of an object. The real dierence between objects and classes is in the way they are created (see further in this chapter). The visibility of the dierent sections is as follows:

Private 
All elds and methods that are in a private block, can only be accessed in the module (i.e. unit) that contains the class denition. They can be accessed from inside the classes' methods or from outside them (e.g. from other classes' methods)
Protected 
Is the same as Private, except that the members of a Protected section are also accessible to descendent types, even if they are implemented in other modules.
Public 
sections are always accessible.
Published 
Is the same as a Public section, but the compiler generates also type information that is needed for automatic streaming of these classes. Fields dened in a published section must be of class type. Array properties cannot be in a published section.

It is also possible to dene class reference types:

_________________________________________________________________________________________________________ Class reference type
-- --class of-classtype-----------------------------------------------
___________________________________________________________________

Class reference types are used to create instances of a certain class, which is not yet known at compile time, but which is specied at run time. Essentially, a variable of a class reference type contains a pointer to the VMT of the specied class. This can be used to construct an instance of the class corresponding to the VMT. The following example shows how it works:

Type  
  TComponentClass = Class of TComponent;  
 
Function CreateComponent(AClass : TComponentClass; AOwner : TComponent) : TComponent;  
 
begin  
  // ...  
  Result:=AClass.Create(AOwner);  
  // ...  
end;

More about instantiating a class can be found in the next section.