You are on page 1of 14

Language overview

What Turbo Pascal was Hight level language Strongly typed Complex structures Block structured Modularized Limited support of objects in later versions Whats new in Object Pascal Full support of Object Oriented programming Exception handling Integrated language features to support programming under windows

A Simple program
Program Hello; {$APPTYPE CONSOLE} const Min = 1; Max = 10; type mytype = Integer; var I :MyType; begin for I := Min to Max do WriteLn('Hello World!'); end.

Lexical elements
Keywords (program, begin, end, if, then, else, ...)
Reserved symbols(:=, <, >, =, <>, +, - *, /, ;, :, [, ], ...) Identifiers (Read, ReadLn, Write, WriteLn, ...)

Directives (virtual, absolute, overload, override)


Numeric constants (0, 3, $FF, 3,14e+10, ...) Character and string literals (a, #61, Hello, #20b#20) Comments (//, (* *), { }) Directives ({$I-}, {$L X.obj}, {$IF}) Whitespaces

Syntactic elements
Declarations Type declarations after type Variable declarations after var Contant declarations after const Procedure and function declarations Expressions Statements Block statement Assignment If statement While statement Repeat Statement ...

Predifined types
Integer types (Integer/Cardinal, ShortInt/Byte,

SmallInt/Word, LongInt/LongWord) Floating Point Types (Real, Single, Double, Extended, Real48) Separate character type Enumerations and Sets File types, and TextFile Arrays Strings

String types
All string types can contain the #0 character Short string types Can only be 8bit strings Has a predifiend capacity of 255 characters or less The 0th character containts the length of the string the others are its content This is the old Pascal string Long string types 2 type: AnsiString, and WideString Can contain up to 2 GB characters Copy on write semantics, and reference counting Length and SetLength can be used Compatible with the Pchar and PWideChar types

Objects and Classes


2 keyword for declaring objects: object, and class Both declare objects Object types Delcared with the object keyword First appeared in Turbo Pascal 7, now deprecated, maintained mostly for backward compatibility Works like the Record types and c++ structs Created with New, destroyed with dispose Class types Declared with the class keyword This is the preferred object type Object Pascal Instances of Class Types are accessed trough references like in Java Constructors creates the object, and destructors destroys it All class types has a base class, when not stated, its the Tobject class

A Class Example
type TSomething = class (TSomeClass, ITheOne, ITheOther) private W :Integer; H :Integer; protected function GetWidth() :Integer; virtual; abstract; procedure SetWidth(Value :Integer); override; public property Width:Integer read GetWidth write SetWidth; property Height:Integer read H write H; class function Area(X :TSomething) :Integer; end;

Constructors and Destructors


Constructors and destructor declared with the constructor

keyword, this way multiple constructors and destructors can be written to the same class Constructors can be virtual The default destructor is Destroy, Destoy called automatically if a Constructor fails to create an object. (The default constructor is Create, but it has no such a function) No garbage collection is done, both constructors and destructors have to be called by hand Objects sometimes use ownership to destroy others automatically

Inheritance
Object Pascal does not support multiple inheritance, only

through interfaces (like java) The inherited keyword used to access the super class The Self variable is the current object Virtual Dynamic, and abstract methods can be written. To override a virtual/dynamic method, you have to use the override directive, or you can choose to hide it with the reintroduce directive. Procedures and functions declared with message (message handlers)directive has a special inheritance

Message handlers
Uses the message directive
Created especially to provide an easy and convinient

way of handling windows messages The inherited keyword refers to the inherited message handler When no message handler available the default handler is called The Dispatch function of Tobject is used to dispatch the message to its handler

Properties
Syntactic sugar for calling getter/setter funtions
Functions used to read and write cannot be

overloeaded Can take parameters in the form of array indices Read, write, default, store and nodefault directives Redeclare without type to change permissions and visibility

Class references
The type of a class is class of type
Each class type can be used as class reference and can

be passed to functions Constructors can be called using class references, in case of virtual constructors, the derived class constructors will be called. Class functions and procedures can also be called using class referencies (like static member functions in java and c++)

You might also like