Difference between revisions of "Object Oriented Programming with Free Pascal and Lazarus"

From Free Pascal wiki
Jump to navigationJump to search
Line 18: Line 18:
  
  
== A real-world analogy ==  
+
== Objects - a real-world analogy ==  
 
Consider the analogy of a blood sample collected in a hospital or a doctor's office.
 
Consider the analogy of a blood sample collected in a hospital or a doctor's office.
  

Revision as of 16:31, 15 July 2008

There are many excellent tutorials in Pascal, but this tutorial attempts to take the beginner further, into Object-Oriented Programming, which is an extension to standard Pascal, offered by Turbo-pascal, Delphi and FreePascal/Lazarus.

An Object is an extension of the standard Pascal record structure

Standard text-based Pascal programming is good for creating applications which, like traditional Unix applications, do one thing and do it very well. The 'one thing' that a program does might be a very complicated action, and might offer the user several menu-driven options, but it is essentially restricted to obeying commands that the user types at the keyboard and listing its responses on the terminal or a printer.

In order to provide a Graphical User Interface (GUI) it is usual to invoke some sort of Object-Oriented Programming method (often making use of C or one of its variants, or Visual Basic, or one of the OO variants of Pascal such as FreePascal with or without Lazarus).

In a GUI the user is presented with a screen with a large number of images arranged in a structured way, consisting of a set of tools or Widgets that are associated with various actions such as

  • selecting from a menu,
  • opening or saving files,
  • connecting to the Internet,
  • performing numerical computations, etc.

The user is expected to move a mouse or other pointer/selection tool over the screen, to select actions which are to be performed in response to a mouse button click or a key-press.

While systems to deal with a complex graphical user interface could be written in standard Pascal or almost any other programming language, it is much easier to use an object-oriented system, in which each graphical object on the screen can have all its properties and the procedures and functions associated with its use kept together in a common structure.


Objects - a real-world analogy

Consider the analogy of a blood sample collected in a hospital or a doctor's office.

The physical sample is certainly an object; it has, associated with it, a lot of information, documents and other physical objects.

  • Sample tube, of a type determined by the test that the physician wants performed.
  • Local rule (or method, standard operating procedure) to direct the nurse or technician collecting the sample
    • which type of tube to use,
    • how to process the sample
    • how to store it before transfer to the lab.
  • Label on the tube with details of the
    • sample identifier
    • patient's name and birthdate
    • date and time of collection
    • tests required.
  • Request form that accompanies the sample to the lab, directing
    • sample ID
    • ID of requesting physician
    • what test the physician requests and
    • giving fuller details of the patient
    • the possible diagnosis for which confirmation is sought.

A copy of the request form is placed in the patient's notes, to remind the physician to expect results within an appropriate time.

  • In the lab - local methods to determine
    • how the sample is to be analysed,
    • what machines to use,
    • how the machines are to be calibrated and operated,
    • how the results are to be stored, formatted and
    • reported back to the physician.

The actual results are a record that the physician uses to assist the diagnosis, and a copy of the results is filed in the patient notes.

The physical sample might be retained for reference, confirmation or further tests, or might be disposed of by pouring down the sink or by incineration; there will be a method to describe this.

There is no need for a physician to spell out all the details and instructions every time a sample is collected; indeed, he may have very little knowledge of how the sample is processed in the lab. The details of the various processes are inherited from previous sample collections and analyses - there will be a generic plan for the whole sequence, and together we could think of the blood sample, all its documents and data, and the underlying methods, as a complex object.

In the physician's mind, the blood sample is seen as almost the same entity as its results, and to the nurses and technicians the sample, the tube, the label and the storage conditions again form a single entity.

Programming Example

Now let us consider the creation of a simple Form with a few controls for an application in FreePascal/Lazarus.

ObjectInspector-TForm.png
BlankForm.png

On invoking the Lazarus IDE, the programmer is presented with a blank template Form design, on which he is encouraged to place various controls or objects.

Note that the pre-made blank Form is already an Object, with its own properties such as position (Top and Left), size (Height and Width), colour, default font for adding text etc.



If a Button control is placed on the Form (type TButton), it will have its own series of properties, which can be examined in the Object Inspector window.

Several of the properties have names similar to those for the Form; this is because many properties are Inherited from some common Ancestor class, which lays out how properties are to be defined and handled by the descendant classes.

As well as properties, the Object Inspector offers a tab called Events, which gives access to Event Handlers which are methods instructing the application how to deal with things such as a mouse click on a button (OnClick) or some change in the position, size or other properties (OnChange).

The physical image of the Button on the Form, together with all its properties and Event Handler methods, should be regarded as a single entity or Object in Pascal.

ObjectInspector-TButton.png
FormWithButton.png
Source FormWithButton1.png

Object-Oriented Extensions to standard Pascal

The Pascal record structure is extended by defining an

Object

A Object is a special kind of record. The record contains all the fields that are declared in the object's definition (just like a conventional record), but now procedures and functions can be declared as if they were part of the record and are held as pointers to the methods associated with the object's type. Here is a typical definition for a small object:

 (example of an Object declaration, preferably taken from the LCL)

Objects can ”inherit” fields and methods from ”parent” objects. This means that these fields and methods can be used as if they were included in the objects declared as a ”child” object.

Furthermore, a concept of visibility is introduced: fields, procedures and functions can be declared as public or private. By default, fields and methods are public, and are exported outside the current unit. Fields or methods that are declared private are only accessible in the current unit: their scope is limited to the implementation of the current unit.

The formal definition of an Object from the Freepascal Manual is given below:

ObjectDefinition.png

Class

Objects are not used very often by themselves in FreePascal and Lazarus; instead, Classes are used very widely. A Class is defined in almost the same way as an Object, but is a pointer to an Object rather than the Object itself. Technically, this means that the Class is allocated on the Heap of a program, whereas the Object is allocated on the Stack.

Here is a simple example of a typical Class declaration:

(example of Class declaration from the LCL)

ToDo

override
virtual
dynamic
inherited
private
protected
public
published