Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Learning Object Oriented Programming with Delphi

Delphi as a learning environment; Unit Sections.

By Zarko Gajic, About.com

This course uses Delphi to teach object orientation. Delphi’s roots lie in Pascal, and so it has a sound, structured foundation. It is also strongly object oriented and provides many OO characteristics such as class inheritance, static binding and dynamic binding, and reference semantics.

The module makes extensive use of graded, worked examples to give students hands-on experience in the implementation of OO code. This helps to bridge the gap between the seemingly simple OO principles and the ramifications of these principles in practice. Through the inductive sequencing of concepts and through the extensive use of worked examples, this module strongly supports independent study, and has been prepared with distance learning students in mind.

In the first two chapters of this module we use Delphi’s RAD facilities to start exploring various aspects of OO. Then we apply these same principles to code that we ourselves write. We start exploring how to define and instantiate classes by looking at the way Delphi builds up a ‘Form’, which is the name Delphi uses for a window in a graphical interface environment.

Example 1.1 A unit file

Delphi is structured around the concept of objects. A form is an object and the various Components we place on a form are also objects. To start investigating Delphi’s RAD classes and objects, open a new application (File | New | Application up to Delphi 7; File | New | VCL Forms Application in Delphi > 7). Using the menu sequence View | Units | Unit1, look 1 at the code for Unit1. Different versions of Delphi differ slightly: Delphi version 2006 looks like this:
1 unit Unit1;

2 interface

3 uses
4 Windows, Messages, SysUtils, Variants, Classes,
5 Graphics, Controls, Forms, Dialogs;

6 type
7 TForm1 = class(TForm)
8 private
9 { Private declarations }
10 public
11 { Public declarations }
12 end;

13 var
14 Form1: TForm1;

15 implementation

16 {$R *.dfm}

17 end.
Note: Delphi versions > 7 offer a choice of VCL Forms (which gives backward compatibility with earlier versions), Windows Forms and ASP.NET. Since the OO principles we are working with are effectively independent of which of these you use, we use VCL Forms to make it easier to move between the different versions.

Delphi programs can contain many unit files. The basic structure of a unit file comprises three sections, the unit name (as in line 1 above), the interface section (lines 2–14) and the implementation section (lines 15–17).

The unit name

By default, Delphi assigns the first unit the name Unit1 (line 1). It calls subsequent units Unit2, Unit3. and so on. When we save the file under a new name (eg with the File | Save As ... menu sequence), Delphi changes the unit’s name and maintains a cross reference to this file in the project (.dpr or .bdsproj) file. (More of this later.)

The interface section

The interface section (lines 2–14) provides the link between a particular unit and other units and consists of several subsections: the uses clause (lines 3–5), the type declarations (lines 6–12) and the global variable declarations (lines 13–14). (Global constant declarations, which we don’t have here, are similar to global variable declarations.)

The uses clause (lines 3–5) : In order to run, a Delphi program needs a lot of background information from various unit files that are a standard part of Delphi. For instance, if the file ‘SysUtils’ (line 4) were not in the ‘uses’ list, we would not be able to do any string operations. As part of its RAD support, Delphi automatically lists the additional units the user interface needs under the uses clause. We won’t discuss these individual units in detail, but it’s easy to find out more about them: in the editor, place the cursor on one of the names and then press [F1] for Help.

The type declaration (lines 6–12): In object oriented programming languages, a type or class defines a structure or template that we can use in our program to create (instantiate) an object we need. Here we are defining a user interface class and Delphi generates the code for the form class automatically through its RAD capability. We’ll use this RAD generated code as an example later, when we define our own classes.

Line 6 in Delphi’s RAD code above notifies the compiler that the type (class) declarations follow. In this program we declare only one class, TForm1, but we can declare additional classes as well either in the same unit or in different units.

Explore Delphi Programming

About.com Special Features

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Learning Object Oriented Programming with Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.