Constructor (object-oriented Programming) - Object Pascal

Object Pascal

In Object Pascal, the constructor is similar to a factory method. The only syntactic difference to regular methods is the keyword constructor in front of the name (instead of procedure or function). It can have any name, though the convention is to have Create as prefix, such as in CreateWithFormatting. Creating an instance of a class works like calling a static method of a class: TPerson.Create('Peter').

program OopProgram; interface type TPerson = class private FName: string; public property Name: string read FName; constructor Create(AName: string); end; implementation constructor TPerson.Create(AName: string); begin FName := AName; end; var Person: TPerson; begin Person := TPerson.Create('Peter'); // allocates an instance of TPerson and then calls TPerson.Create with the parameter AName = 'Peter' end;

Read more about this topic:  Constructor (object-oriented Programming)

Famous quotes containing the words object and/or pascal:

    I do not object to people looking at their watches when I am speaking. But I strongly object when they start shaking them to make certain they are still going.
    William Norman, 1st Baron Of Ulverston Birkett (1883–1962)

    Nothing is so insufferable to man as to be completely at rest, without passions, without business, without diversion, without study. He then feels his nothingness, his forlornness, his insufficiency, his dependence, his weakness, his emptiness. There will immediately rise from the depth of his heart weariness, gloom, sadness, fretfulness, vexation, despair.
    —Blaise Pascal (1623–1662)