Instance Constructors

Instance constructor is a class method that implements the actions required to initialize a class instance.

constructor-declaration:

attributesopt  constructor-modifiersopt  constructor-declarator  constructor-body

constructor-modifiers:

constructor-modifier

constructor-modifiers   constructor-modifier

constructor-modifier:

Public

Protected

Friend

Private

constructor-declarator:

Constructor constructor-name (  formal-parameter-listopt  )  constructor_initializeropt ;

constructor_initializeropt

: Inherited constructor-name (  argument-listopt  )

: constructor-name  (  argument-listopt  )

constructor-body:

method-localsopt Begin   method-body   End Constructor   constructor-nameopt   ;

constructor-name:

identifier

Instance constructor declaration may contain a parameter list subject to the same rules as the method parameters. The parameter list of an instance constructor determines its signature. Each of the types referenced in the formal parameter list of an instance constructor must be at least as accessible as the constructor itself.

Instance constructors are not inherited by derived classes. Thus, a class has no constructors other than those actually declared in the class. Using the Inherited keyword in constructor header results in invoking the specified base class constructor when a derived class object is created. If the Inherited keyword is not present, it is possible to specify a constructor of the current class instead.

If a class contains no instance constructor declarations, a default instance constructor is automatically provided.

Default Constructor

A default constructor is automatically generated by the compiler if the class contains no instance constructor declarations. This constructor simply invokes an parameterless instance constructor of the parent class. If the parent class does not have a parameterless instance constructor, a compile error occurs. Access level for the default constructor as generic for abstract classes and as public for all other classes.

Example

Class A
    //Default constructor
    Public Constructor Create();
    Begin
        
    End Constructor;
    
    //Constructor with parameter
    Public Constructor Create(i: integer);
    Begin
        
    End Constructor;
    
    //Additional constructor with parameters
    //Default constructor is invoked during initialization
    Public Constructor CreateEx(i: integer; j: double): Create();
    Begin
        
    End Constructor;
End Class;

Class B: A
    //Default constructor
    //Constructor of the CreateEx basic class is invoked during initialization 
    Public Constructor B(): Inherited CreateEx(-1, -1);
    Begin
        
    End Constructor;
End Class;

Sub Test();
Var
    Obj: A;
    Obj1: B;
Begin
    Obj := New A(); //Invoke default constructor A.Create()
    Obj := New A.Create(); //Explicit invocation of default constructor A.Create()
    Obj := New A(1); //Invoke constructor with parameter A.Create(i: Integer)
    Obj1 := New B(); //Invoke default constructor B.Create()
End Sub;

See also:

Classes