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

From Free Pascal wiki
Jump to navigationJump to search
(New page: There are excellent tutorials in Pascal, but this tutorial attempts to take the beginner further, into Object-Oriented Programming, which is an extension to standard Pasc...)
 
Line 4: Line 4:
  
  
== A real-world ananlogy ==
+
== 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.
Line 36: Line 36:
 
**reported back to the physician.  
 
**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 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.
 
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 form a single entity.
+
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 form a single entity.
  
Now let us consider the creation of a simple Form with a few controls for an application in FreePascal/Lazarus
 
  
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.
+
== Programming Example ==
  
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.  
+
Now let us consider the creation of a simple Form with a few controls for an application in FreePascal/Lazarus.
 +
 
 +
<div class="floatleft">[[Image:ObjectInspector-TForm.png]]</div>
 +
<div class="floatright">[[Image:BlankForm.png]]</div>
 +
 
 +
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).
 
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.
+
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.
 +
 
 +
<div class="floatleft">[[Image:ObjectInspector-TButton.png]]</div>
 +
<div class="floatright">[[Image:FormWithButton.png]]</div>
 +
 
  
  
 +
== ToDo Definitions: ==
  
ToDo Definitions:
 
 
  override
 
  override
 
  virtual
 
  virtual
 
  dynamic
 
  dynamic
 
  inherited
 
  inherited

Revision as of 12:55, 15 July 2008

There are 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


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
    • patient's name and birthdate
    • date and time of collection
    • tests required.
  • Request form that accompanies the sample to the lab, directing
    • 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 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


ToDo Definitions:

override
virtual
dynamic
inherited