# Object Pascal

Paradigm Imperative, Structured, Object-oriented, Functional (Delphi dialect only), Component-based, Event-driven, Generic Anders Hejlsberg and Niklaus Wirth while at Apple 1986; 35 years ago static and dynamic (dynamic typing through Variants, array of const and RTTI), strong, safe .p, .pp, .pas {{#property:P856}} Apple, Turbo Pascal, Free Pascal (using objfpc or delphi mode), Delphi, Delphi.NET, Delphi Web Script, Oxygene Pascal, Smalltalk C#, Java

Object Pascal is a high-level, strongly typed language that supports structured and object-oriented design. Its benefits include easy-to-read code, quick compilation, and the use of multiple unit files for modular programming. Object Pascal has special features that support Delphi’s component framework and RAD environment.

## History

Object Pascal is an extension of the Pascal language that was developed at Apple Computer by a team led by Larry Tesler in consultation with Niklaus Wirth, the inventor of Pascal. It is descended from an earlier object-oriented version of Pascal called Clascal, which was available on the Lisa computer.

Object Pascal was needed in order to support MacApp, an expandable Macintosh application framework that would now be called a class library. Object Pascal extensions and MacApp itself were developed by Barry Haynes, Ken Doyle, and Larry Rosenstein, and were tested by Dan Allen. Larry Tesler oversaw the project, which began very early in 1985 and became a product in 1986.

An Object Pascal extension was also implemented in the Think Pascal IDE. The IDE includes the compiler and an editor with Syntax highlighting and checking, a powerful debugger and a class library.

Apple dropped support for Object Pascal when they moved to IBM's PowerPC architecture in 1994. MacApp 3.0, for this platform, was re-written in C++.

## Pascal Tokens

Tokens are the basic lexical building blocks of source code: they are the ’words’ of the language: characters are combined into tokens according to the rules of the programming language. There are five classes of tokens:

1. Reserved words. These are words which have a fixed meaning in the language. They cannot be changed or redefined.
2. Identifiers.These are names of symbols that the programmer defines. They can be changed and re-used. They are subject to the scope rules of the language.
3. Operators. These are usually symbols for mathematical or other operations: +, -, * and so on.
4. Separators. This is usually white-space.
5. Constants.Numerical or character constants are used to denote actual values in the source code, such as 1 (integer constant) or 2.3 (float constant) or ’String constant’ (a string: a piece of text).

Free Pascal supports the use of nested comments. The following constructs are valid comments:

(* This is an old style comment *)

{ This is a Turbo Pascal comment }
// This is a Delphi comment. All is ignored till the end of the line.


## Structured statements

Structured statements are built from other statements. Use a structured statement when you want to execute other statements sequentially, conditionally, or repeatedly.

  begin
I := SomeConstant;
while I > 0 do
begin
I := I - 1;
end;
end;


=== With statements ===A with statement is a shorthand for referencing the fields of a record or the fields, properties, and methods of an object.

with obj do statement or with obj 1 , ..., obj n do statement

While statements
while not Eof(InputFile) do
begin
Process(Line);
end;


## Pascal Procedures and Functions

In Pascal, a routine can assume two forms: a procedure and a function. In theory, a procedure is an operation you ask the computer to perform, a function is a computation returning a value. This difference is emphasized by the fact that a function has a result, a return value, while a procedure doesn't. Both types of routines can have multiple parameters, of given data types.

  procedure Hello;
begin
ShowMessage ('Hello world!');
end;
function Double (Value: Integer) : Integer;
begin
Double := Value * 2;
end;
// or, as an alternative
function Double2 (Value: Integer) : Integer;
begin
Result := Value * 2;
end;


== Reference Parameters == Pascal routines allow parameter passing by value and by reference. Passing parameters by value is the default: the value is copied on the stack and the routine uses and manipulates the copy, not the original value. Passing a parameter by reference means that its value is not copied onto the stack in the formal parameter of the routine (avoiding a copy often means that the program executes faster). Instead, the program refers to the original value, also in the code of the routine. This allows the procedure or function to change the value of the parameter. Parameter passing by reference is expressed by the var keyword. Here is an example of passing a parameter by reference using the var keyword:

  procedure DoubleTheValue (var Value: Integer);
begin
Value := Value * 2;
end;


In this case, the parameter is used both to pass a value to the procedure and to return a new value to the calling code. When you write:

  var
X: Integer;
begin
X := 10;
DoubleTheValue (X);


The value of the X variable becomes 20, because the function uses a reference to the original memory location of X, affecting its initial value. Passing parameters by reference makes sense for ordinal types, for old-fashioned strings, and for large records. Delphi objects, in fact, are invariably passed by value, because they are references themselves. For this reason passing an object by reference makes little sense (apart from very special cases), because it corresponds to passing a "reference to a reference."

### Constant Parameters

  function DoubleTheValue (const Value: Integer): Integer;
begin
Value := Value * 2;      // compiler error
Result := Value;
end;


## Variants Have No Type

In general, you can use variants to store any data type and perform numerous operations and type conversions. Notice that this goes against the general approach of the Pascal language and against good programming practices. A variant is type-checked and computed at run time. The compiler won't warn you of possible errors in the code, which can be caught only with extensive testing.

  var
V: Variant;
you can assign to it values of several different types:
V := 10;
V := 'Hello, World';
V := 45.55;


## Object-Oriented Concepts

In Pascal, there are two structural data types used to implement a real world object −

• Object types
• Class types

Before we go in detail, let's define important Pascal terms related to Object-Oriented Pascal.

• Object − An Object is a special kind of record that contains fields like a record; however, unlike records, objects contain procedures and functions as part of the object. These procedures and functions are held as pointers to the methods associated with the object's type.
• Class − A Class is defined in almost the same way as an Object, but there is a difference in way they are created. The Class is allocated on the Heap of a program, whereas the Object is allocated on the Stack. It is a pointer to the object, not the object itself.
• Instantiation of a class − Instantiation means creating a variable of that class type. Since a class is just a pointer, when a variable of a class type is declared, there is memory allocated only for the pointer, not for the entire object. Only when it is instantiated using one of its constructors, memory is allocated for the object. Instances of a class are also called 'objects', but do not confuse them with Object Pascal Objects. We will write 'Object' for Pascal Objects and 'object' for the conceptual object or class instance.
• Member Variables − These are the variables defined inside a Class or an Object.
• Member Functions − These are the functions or procedures defined inside a Class or an Object and are used to access object data.
• Visibility of Members − The members of an Object or Class are also called the fields. These fields have different visibilities. Visibility refers to accessibility of the members, i.e., exactly where these members will be accessible. Objects have three visibility levels: public, private and protected. Classes have five visibility types: public, private, strictly private, protected and published. We will discuss visibility in details.
• Inheritance − When a Class is defined by inheriting existing functionalities of a parent Class, then it is said to be inherited. Here child class will inherit all or few member functions and variables of a parent class. Objects can also be inherited.
• Parent Class − A Class that is inherited by another Class. This is also called a base class or super class.
• Child Class − A class that inherits from another class. This is also called a subclass or derived class.
• Polymorphism − This is an object-oriented concept where same function can be used for different purposes. For example, function name will remain same but it may take different number of arguments and can do different tasks. Pascal classes implement polymorphism. Objects do not implement polymorphism.
• Overloading − It is a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation. Pascal classes implement overloading, but the Objects do not.
• Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted).
• Encapsulation − Refers to a concept where we encapsulate all the data and member functions together to form an object.
• Constructor − Refers to a special type of function which will be called automatically whenever there is an object formation from a class or an Object.
• Destructor − Refers to a special type of function which will be called automatically whenever an Object or Class is deleted or goes out of scope.

### Visibility of the Object Members

Visibility indicates the accessibility of the object members. Pascal object members have three types of visibility −

Visibility Accessibility
Public The members can be used by other units outside the program unit
Private The members are only accessible in the current unit
Protected The members are available only to objects descended from the parent object

By default, fields and methods of an object are public and are exported outside the current unit.

## Sample "Hello World" programs

### Apple's Object Pascal

   program ObjectPascalExample;
type
THelloWorld = object
procedure Put;
end;   var
HelloWorld: THelloWorld;   procedure THelloWorld.Put;
begin
WriteLn('Hello, World!');
end;   begin
New(HelloWorld);
HelloWorld.Put;
Dispose(HelloWorld);
end.


### Turbo Pascal's Object Pascal

Still supported in Delphi and Free Pascal. FPC also packages its own substitutes for the libraries/units. Delphi doesn't. 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.

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.


### Delphi and Free Pascal's Object Pascal

  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.


Note that the object construct is still available in Delphi and Free Pascal (Delphi-compatible mode).

### Oxygene Object Pascal

   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.


### DWScript's Object Pascal (aka Smart Pascal)

  type
THelloWorld = class
procedure Put;
begin
PrintLn('Hello, World!');
end
end;   var HelloWorld := THelloWorld.Create;   HelloWorld.Put;


Note that the method implementation can also be made in a distinct location as in other Object Pascal dialects.