Difference between revisions of "Object"
(Create English version of German page) |
Jwdietrich (talk | contribs) (Integrating template for object types.) |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{Object}} | {{Object}} | ||
+ | Back to: [[Data type|data types]] | [[Reserved words|reserved words]]. | ||
− | The | + | 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]]. |
− | |||
+ | Example skeleton of the creation of the data type object: | ||
− | + | <syntaxhighlight lang=pascal> | |
− | <syntaxhighlight> | ||
type | type | ||
TTest = object | TTest = object | ||
Line 18: | Line 18: | ||
Example skeleton of the create of a packed version of the data type object: | Example skeleton of the create of a packed version of the data type object: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
type | type | ||
TTest = packed object | TTest = packed object | ||
Line 30: | Line 30: | ||
Example with constructor and destructor: | Example with constructor and destructor: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
type | type | ||
TTest = object | TTest = object | ||
Line 80: | Line 80: | ||
Destructor not needed - nothing allocated on the heap | Destructor not needed - nothing allocated on the heap | ||
− | [[ | + | == Objects compared to other structured types == |
+ | {{Template:Object Types}} | ||
+ | |||
+ | == See Also == | ||
+ | |||
+ | * [[Programming Using Objects]] | ||
+ | |||
+ | |||
+ | {{Data types}} |
Latest revision as of 23:02, 17 February 2021
│
Deutsch (de) │
English (en) │
français (fr) │
Back to: data types | reserved words.
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 in the types it can create, but by default objects are created on the stack, while class data is created on the heap. However, object created types can be created on the heap by using the new procedure. Object was introduced in Turbo Pascal, while class was introduced in Delphi. Object is maintained for backward compatibility with Turbo Pascal and has largely been superseded by class.
Example skeleton of the creation of the data type object:
type
TTest = object
private
{ private declarations }
public
{ public declarations }
end;
Example skeleton of the create of a packed version of the data type object:
type
TTest = packed object
private
{ private declarations }
public
{ public declarations }
end;
Example with constructor and destructor:
type
TTest = object
private
{private declarations}
total_errors : Integer;
public
{public declarations}
constructor Init;
destructor Done;
procedure IncrementErrors;
function GetTotalErrors : Integer;
end;
procedure TTest.IncrementErrors;
begin
Inc(total_errors);
end;
function TTest.GetTotalErrors : Integer;
begin
GetTotalErrors := total_errors;
end;
constructor TTest.Init;
begin
total_errors := 0;
end;
destructor TTest.Done;
begin
WriteLn('Destructor not needed - nothing allocated on the heap');
end;
var
error_counter: TTest;
begin
error_counter.Init; // unlike C++, constructors must be explicitly called
error_counter.IncrementErrors;
error_counter.IncrementErrors;
WriteLn('current errors:', error_counter.GetTotalErrors);
error_counter.Done
end.
Output:
current errors:2
Destructor not needed - nothing allocated on the heap
Objects compared to other structured types
Feature | Record | Adv Record | Object | Class |
---|---|---|---|---|
Encapsulation (combining data and methods + hiding visibility) | No | Yes | Yes | Yes |
Inheritance | No | No | Yes | Yes |
Class constructor and destructor | No | Yes | Yes | Yes |
Polymorphism (virtual methods) | No | No | Yes | Yes |
Memory allocation | Stack | Stack | Stack | Heap (Only) |
|
Managed Types only | Managed Types only | Managed Types only | All fields |
|
all fields zeros | all fields zeros | all fields zeros | returns nil |
Operator overload (global) | Yes | Yes | Yes | Yes |
Operator overload (in type only) | No | Yes | No | No |
Type helpers | No | Yes | No | Yes |
Virtual constructors, class reference | No | No | No | Yes |
Variant part (case) as c/c++ union | Yes | Yes | No | No |
Bitpacked (really packing) | Yes | Yes | No | No |
Modified from https://forum.lazarus.freepascal.org/index.php/topic,30686.30.html (original author: ASerge).
See Also
simple data types |
|
---|---|
complex data types |