The Object Pascal programming language.pptx

download The Object Pascal programming language.pptx

of 14

Transcript of The Object Pascal programming language.pptx

  • 7/28/2019 The Object Pascal programming language.pptx

    1/14

  • 7/28/2019 The Object Pascal programming language.pptx

    2/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

  • 7/28/2019 The Object Pascal programming language.pptx

    3/14

    A Simple programProgramHello;{$APPTYPE CONSOLE}

    const

    Min = 1;

    Max = 10;

    type

    mytype = Integer;

    var

    I :MyType;

    begin

    forI := Min toMax do

    WriteLn('Hello World!');

    end.

  • 7/28/2019 The Object Pascal programming language.pptx

    4/14

    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

  • 7/28/2019 The Object Pascal programming language.pptx

    5/14

    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 ...

  • 7/28/2019 The Object Pascal programming language.pptx

    6/14

    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 TextFileArrays

    Strings

  • 7/28/2019 The Object Pascal programming language.pptx

    7/14

    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

  • 7/28/2019 The Object Pascal programming language.pptx

    8/14

    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, maintainedmostly 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

  • 7/28/2019 The Object Pascal programming language.pptx

    9/14

    A Class Exampletype TSomething = class (TSomeClass, ITheOne,ITheOther)

    private

    W :Integer;

    H :Integer;protected

    function GetWidth() :Integer; virtual; abstract;

    procedure SetWidth(Value :Integer); override;

    public

    propertyWidth:Integer readGetWidthwriteSetWidth;

    propertyHeight:Integer readHwriteH;

    classfunctionArea(X :TSomething) :Integer;

    end;

  • 7/28/2019 The Object Pascal programming language.pptx

    10/14

    Constructors and Destructors Constructors and destructor declared with the constructor

    keyword, this way multiple constructors and destructorscan 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. (Thedefault constructor is Create, but it has no such afunction)

    No garbage collection is done, both constructors anddestructors have to be called by hand

    Objects sometimes use ownership to destroy othersautomatically

  • 7/28/2019 The Object Pascal programming language.pptx

    11/14

    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 theoverride directive, or you can choose to hide it with thereintroduce directive.

    Procedures and functions declared with message (messagehandlers)directive has a special inheritance

  • 7/28/2019 The Object Pascal programming language.pptx

    12/14

    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

  • 7/28/2019 The Object Pascal programming language.pptx

    13/14

    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

  • 7/28/2019 The Object Pascal programming language.pptx

    14/14

    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 classconstructors will be called.

    Class functions and procedures can also be calledusing class referencies (like static member functions injava and c++)