Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Object Pascal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Sample "Hello World" programs== {{unreferenced section|date=June 2013}} Here are several "Hello World" programs in different Object Pascal versions. ===Apple version=== <syntaxhighlight lang="objectpascal"> program ObjectPascalExample; type THelloWorld = object procedure Put; end; var HelloWorld: THelloWorld; procedure THelloWorld.Put; begin ShowMessage('Hello, World!'); end; begin New(HelloWorld); HelloWorld.Put; Dispose(HelloWorld); end. </syntaxhighlight> ===Turbo Pascal version=== Still supported in Delphi and Free Pascal. FPC also packages its own substitutes for the libraries/units. Delphi does not. The Free Pascal 1.0 series and the FPC textmode IDE are the largest open codebases in this dialect. Free Pascal 2.0 was rewritten in a more Delphi-like dialect, and the textmode IDE and related frameworks (Free Vision) are the only parts in the TP version of Object Pascal. ====Stack based allocation==== <syntaxhighlight lang="objectpascal"> program ObjectPascalExample; type THelloWorld = object procedure Put; end; procedure THelloWorld.Put; begin WriteLn('Hello, World!'); end; var HelloWorld: THelloWorld; { allocated on the stack and can be used without explicit allocation. } begin HelloWorld.Put; end. </syntaxhighlight> ====Heap based allocation==== <syntaxhighlight lang="objectpascal"> program ObjectPascalExample; type PHelloWorld = ^THelloWorld; THelloWorld = object procedure Put; end; procedure THelloWorld.Put; begin WriteLn('Hello, World!'); end; var HelloWorld: PHelloWorld; { this is a typed pointer to a THelloWorld } begin New(HelloWorld); HelloWorld^.Put; Dispose(HelloWorld); end. </syntaxhighlight> Another example: <syntaxhighlight lang="objectpascal"> program ObjectPascalExample; type PHelloWorld = ^THelloWorld; THelloWorld = object procedure Put; end; procedure THelloWorld.Put; begin WriteLn('Hello, World!'); end; var HelloWorld: PHelloWorld; { this is a typed pointer to a THelloWorld } HelloWorld2: ^THelloWorld; { this is exactly the same with different syntax } HelloWorld3: ^THelloWorld; HelloWorld4: PHelloWorld; begin { This works in a similar way as the code above, note the allocation and de-allocation, though, many people get confused. In the past there was a wrong example with wrong comments here... } New(HelloWorld); { one instance } HelloWorld4 := HelloWorld; { this is valid - a pointer copy } HelloWorld2 := HelloWorld; { this is valid - a pointer copy } New(HelloWorld3); { a second instance } HelloWorld4 := HelloWorld3; { this is valid - a pointer copy } HelloWorld2 := HelloWorld3; { this is valid - a pointer copy } Dispose(HelloWorld); { it allocates only two instances } Dispose(HelloWorld3); { so it must release only two instances } end. </syntaxhighlight> This works based on pointer copy, unless there is a specific allocation for a deeper copy. ===Delphi and Free Pascal version=== <syntaxhighlight lang="delphi"> program ObjectPascalExample; type THelloWorld = class procedure Put; end; procedure THelloWorld.Put; begin Writeln('Hello, World!'); end; var HelloWorld: THelloWorld; { this is an implicit pointer } begin HelloWorld := THelloWorld.Create; { constructor returns a pointer to an object of type THelloWorld } HelloWorld.Put; HelloWorld.Free; { this line deallocates the THelloWorld object pointed to by HelloWorld } end. </syntaxhighlight> Note that the object construct is still available in Delphi and Free Pascal. ===Oxygene version=== <syntaxhighlight lang="objectpascal"> namespace ObjectPascalExample; interface type ConsoleApp = class class method Main; end; THelloWorld = class method Put; end; implementation method THelloWorld.Put; begin Console.WriteLine('Hello, World!'); end; class method ConsoleApp.Main; begin var HelloWorld := new THelloWorld; HelloWorld.Put; end; end. </syntaxhighlight> ===DWScript (Smart Pascal) version=== <syntaxhighlight lang="objectpascal"> type THelloWorld = class procedure Put; begin PrintLn('Hello, World!'); end end; var HelloWorld := THelloWorld.Create; HelloWorld.Put; </syntaxhighlight> The method implementation can also be made in a distinct location as in other Object Pascal dialects.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)