Defines the method used to create an object from a class
1 keyword Constructor(type Class declaration
  Constructor Name; {Overload;}
2 type Class declaration
  Constructor Name(Arguments); {Overload;}
The Constructor keyword defines a constructor procedure Name for a class.
When creating an object, you call a Constructor method of the class, not the object:
objectName := ClassName.Create(parms);
The Name for the constructor is normally Create, but is not restricted to this. It is very wise to keep to this name.
An object may be constructed with or without arguments (see the example).
Constructors may be defined in the public or published sections of the class definition.
You may have multiple constructors, but by doing so, you can only define one of these as Published. With multiple constructors, each must be suffixed with the Overload directive, as required by delphi.
When implementing the constructor procedure, normally called Create, you should make it a habit of calling the parent constructor, for example
constructor Create;

This ensures that the resulting object is a safe instantiated instance of this parent class, even if the parent is TObject, which does nothing in its constructor. The sample code illustrates this plain variety of Inherited plus the version where the parent constructor has arguments.
Related commands
ClassStarts the declaration of a type of object class
DestructorDefines the method used to destroy an object
FunctionDefines a subroutine that returns a value
InheritedUsed to call the parent class constructor or destructor method
ObjectAllows a subroutine data type to refer to an object method
ProcedureDefines a subroutine that does not return a value
TObjectThe base class type that is ancestor to all other classes
 Download this web site as a Windows program.

Example code : Examples of constructors with and without arguments
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;


  Forms, Dialogs, Classes, Controls, StdCtrls;

  // Define a parent class, base on TObject by default
  TFruit = class
    name   : string;
    Constructor Create; overload;  // This constructor uses defaults
    Constructor Create(name : string); overload;

  // Define a descendant types
  TApple = class(TFruit)
    diameter : Integer;
    Constructor Create(name : string; diameter : Integer);

  // The class for the form used by this unit
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);

  Form1: TForm1;

{$R *.dfm} // Include form definitions

// Create a fruit object - parameterless version
constructor TFruit.Create;
  // Execute the parent (TObject) constructor first
  inherited;  // Call the parent Create method

  // Now set a default fruit name := 'Fruit';

// Create a fruit object - parameterised version
constructor TFruit.Create(name: string);
  // Cannot execute the parent constructor - parms differ

  // And save the fruit name := name;

// Create an apple object
constructor TApple.Create(name: string; diameter : Integer);
  // Execute the parent (TFruit) constructor first
  inherited Create(name);  // Call the parent method

  // Now save the passed apple diameter
  self.diameter := diameter;

// Main line code
procedure TForm1.FormCreate(Sender: TObject);
  fruit  : TFruit;
  banana : TFruit;
  apple  : TApple;

  // Create 3 different fruit objects
  fruit  := TFruit.Create;
  banana := TFruit.Create('Banana');
  apple  := TApple.Create('Pink Lady', 12);

  // See which of our objects are fruits
  if fruit  Is TFruit then ShowMessage(  +' is a fruit');
  if banana Is TFruit then ShowMessage( +' is a fruit');
  if apple  Is TFruit then ShowMessage(  +' is a fruit');

  // See which objects are apples
  if fruit  Is TApple then ShowMessage(   +' is an apple');
  if banana Is TApple then ShowMessage(  +' is an apple');
  if apple  Is TApple then ShowMessage(   +' is an apple');
  Fruit is a fruit
  Banana is a fruit
  Pink Lady is a fruit
  Pink Lady is an apple
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page