Object: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Object
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Back to: [[Data type|data types]] \| [[Reserved words|reserved words]]. | |
| 2 | + | ||
| 3 | + | The reserved word **object** is used to construct complex data types that contain both functions, procedures and data. Object allows the user to perform Object-Oriented Programming (OOP). It is similar to [[Class|class]] in the types it can create, but by default objects are created on the [[Stack|stack]], while class data is created on the [[Heap|heap]]. However, object created types can be created on the heap by using the [[New|new]] procedure. Object was introduced in [[Turbo Pascal]], while [[Class|class]] was introduced in [[Delphi]]. Object is maintained for backward compatibility with Turbo Pascal and has largely been superseded by [[Class|class]]. | |
| 4 | + | ||
| 5 | + | Example skeleton of the creation of the data type object: | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | type | |
| 9 | + | TTest = object | |
| 10 | + | private | |
| 11 | + | { private declarations } | |
| 12 | + | public | |
| 13 | + | { public declarations } | |
| 14 | + | end; | |
| 15 | + | ``` | |
| 16 | + | ||
| 17 | + | Example skeleton of the create of a packed version of the data type object: | |
| 18 | + | ||
| 19 | + | ``` pascal | |
| 20 | + | type | |
| 21 | + | TTest = packed object | |
| 22 | + | private | |
| 23 | + | { private declarations } | |
| 24 | + | public | |
| 25 | + | { public declarations } | |
| 26 | + | end; | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | Example with constructor and destructor: | |
| 30 | + | ||
| 31 | + | ``` pascal | |
| 32 | + | type | |
| 33 | + | TTest = object | |
| 34 | + | private | |
| 35 | + | {private declarations} | |
| 36 | + | total_errors : Integer; | |
| 37 | + | public | |
| 38 | + | {public declarations} | |
| 39 | + | constructor Init; | |
| 40 | + | destructor Done; | |
| 41 | + | procedure IncrementErrors; | |
| 42 | + | function GetTotalErrors : Integer; | |
| 43 | + | end; | |
| 44 | + | ||
| 45 | + | procedure TTest.IncrementErrors; | |
| 46 | + | begin | |
| 47 | + | Inc(total_errors); | |
| 48 | + | end; | |
| 49 | + | ||
| 50 | + | function TTest.GetTotalErrors : Integer; | |
| 51 | + | begin | |
| 52 | + | GetTotalErrors := total_errors; | |
| 53 | + | end; | |
| 54 | + | ||
| 55 | + | constructor TTest.Init; | |
| 56 | + | begin | |
| 57 | + | total_errors := 0; | |
| 58 | + | end; | |
| 59 | + | ||
| 60 | + | destructor TTest.Done; | |
| 61 | + | begin | |
| 62 | + | WriteLn('Destructor not needed - nothing allocated on the heap'); | |
| 63 | + | end; | |
| 64 | + | ||
| 65 | + | var | |
| 66 | + | error_counter: TTest; | |
| 67 | + | ||
| 68 | + | begin | |
| 69 | + | error_counter.Init; // unlike C++, constructors must be explicitly called | |
| 70 | + | error_counter.IncrementErrors; | |
| 71 | + | error_counter.IncrementErrors; | |
| 72 | + | WriteLn('current errors:', error_counter.GetTotalErrors); | |
| 73 | + | error_counter.Done | |
| 74 | + | end. | |
| 75 | + | ``` | |
| 76 | + | ||
| 77 | + | **Output:**\ | |
| 78 | + | current errors:2\ | |
| 79 | + | Destructor not needed - nothing allocated on the heap | |
| 80 | + | ||
| 81 | + | ## Objects compared to other structured types | |
| 82 | + | ||
| 83 | + | ## See Also | |
| 84 | + | ||
| 85 | + | - [[Programming Using Objects]] | |
| 86 | + | ||
| 87 | + | --- | |
| 88 | + | *From the Free Pascal wiki, page "Object", as it stood on 2021-02-17 after a change by Jwdietrich, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
