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 Delphis 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 Delphis 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;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.
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.
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 214) and the implementation section (lines 1517).
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 units 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 214) provides the link between a particular unit and other units and consists of several subsections: the uses clause (lines 35), the type declarations (lines 612) and the global variable declarations (lines 1314). (Global constant declarations, which we dont have here, are similar to global variable declarations.)The uses clause (lines 35) : 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 wont discuss these individual units in detail, but its 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 612): 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. Well use this RAD generated code as an example later, when we define our own classes.
Line 6 in Delphis 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.

