Chapter 6
Classes

In the Delphi approach to Object Oriented Programming, everything revolves around the concept of 'Classes'. A class can be seen as a pointer to an object, or a pointer to a record, with methods associated with it.

The dierence between objects and classes is mainly that an object is allocated on the stack, as an ordinary record would be, and that classes are always allocated on the heap. In the following example:

Var  
  A : TSomeObject; // an Object  
  B : TSomeClass;  // a Class

The main dierence is that the variable A will take up as much space on the stack as the size of the object (TSomeObject). The variable B, on the other hand, will always take just the size of a pointer on the stack. The actual class data is on the heap.

From this, a second dierence follows: a class must always be initialized through its constructor, whereas for an object, this is not necessary. Calling the constructor allocates the necessary memory on the heap for the class instance data.

Remark: In earlier versions of Free Pascal it was necessary, in order to use classes, to put the objpas unit in the uses clause of a unit or program. This is no longer needed as of version 0.99.12. As of this version, the unit will be loaded automatically when the -MObjfpc or -MDelphi options are specied, or their corresponding directives are used:

{$mode objfpc}  
{$mode delphi}

In fact, the compiler will give a warning if it encounters the objpas unit in a uses clause.

 6.1 Class denitions
 6.2 Class instantiation
 6.3 Methods
  6.3.1 Declaration
  6.3.2 invocation
  6.3.3 Virtual methods
  6.3.4 Class methods
  6.3.5 Message methods
  6.3.6 Using inherited
 6.4 Properties