1. Computing

Programmer Defined Classes and Objects - Delphi OOP Part 3 / Chapter 6

Declaring a Reference and Creating and Object

From , former About.com Guide

The type declaration (lines 3–11) is much like Delphi’s automatic form declaration. Line 4 states that we are defining a new class, TItem, which is derived from TObject, Delphi’s base class. TItem objects will have one data field, FCount of type integer (line 6), which we are making private to this unit (line 5).

TItem has three methods, all of which are public (line 7) and so are available to other units in this project. These public methods provide a means for other objects to interact with the private data field and so they are called access methods. Line 8 states that TItem has a procedure type method called AddItem that has no parameters. Line 9 declares a function type method, GetCount, that has no incoming parameters and that supplies an integer value. (An access method of this form is sometimes called a Getter method.) Line 10 declares another procedure type method, ZeroCount, that has no incoming parameters.

The methods themselves are defined in the implementation section (lines 12–26). Because procedure AddItem is one of TItem’s methods, and so is part of TItem, we use dot notation to include the TItem relationship in the procedure header (line 14). Line 16 increases the value of the (private) FCount variable declared in line 6 by one.

The ZeroCount method, lines 22–25, is similar. It has no incoming parameters and sets the value of FCount to 0. Note that FCount is not declared as a local variable in either function. It is declared as part of class TItem in line 6 and both methods manipulate this value. However, because FCount is declared as private (lines 5–6), other units do not have access to FCount.

The remaining method, function GetCount, returns the value of FCount through the (automatic) Result variable (line 20). The only way an outsider can manipulate the value of FCount is through the two public methods AddItems and ZeroCount. The only way an outsider can access this value is through public method GetCount.

Notice that we changed the name Count to FCount. By a Delphi convention, a class’s data fields are preceded by F… to provide a quick visual clue that they are local data fields, and to distinguish them from properties, which do not have the preceding F. We’ll see more of this in Part 4 where we introduce properties and look at the relationship between a property and a local data field.

In summary, our data, FCount, is now private and encapsulated (lines 5&6). The only way any other unit can access this data is through the public access methods AddItem, GetCount and ZeroCount (lines 7–11), which define strictly what operations may be carried out on the private data. To reinforce that we now have two separate units in the project, view the uses clause in the project file (menu sequence View | Units | OODriverP). We see that only one form is created since unit ItemU .pas does not have an accompanying form.

 program OODriverP;
 
 uses
    Forms,
    OODriverU in 'OODriverU.pas' {frmCount},
    ItemU in 'ItemU .pas';
 
 {$R *.res}
 
 begin
    Application.Initialize;
    Application.CreateForm(TfrmCount, frmCount) ;
    Application.Run;
 end. 

Ex 3.2 step 2 Declaring a Reference and Creating an Object

As we mentioned in the introduction to example 3.2, the second step in using an object is to create a name, or reference, for each object we need in the unit that will use these objects (ie in unit OODriverU, not in unit ItemU ). The third step is to create the object so that we can use it. We do both these steps here.

In OODriverU.pas, the unit associated with the form, we’ll instantiate an object of class TItem and then manipulate this object from event handlers attached to the buttons. To do this, first select the OODriverU tab in the edit window to display unit OODriverU.pas. We need to:

  • Add a reference to the other unit (lines 25–26 below) because this unit uses a class (TItem) that is defined in unit ItemU . We can either type this reference in directly or have Delphi enter it through the menu sequence File | Use Unit.
  • Declare a reference to the object we are going to create (ItemCount, lines 27–28).
  • Create the object in the OODriverU’s initialization section (lines 47–48). When we use components, Delphi creates the objects automatically. However, if we define a class ourselves, like TItem, we must ourselves create an object of this class before we can use it. Here it is convenient to create this object (ItemCount) in the initialization section so that it exists by the time the form appears. To do this we use the Create constructor method that TItem inherits from TObject (line 48). (We’ll write our own constructor in chap 5.)
  • Use the object in the OnClick event handlers through calls to its methods. Depending on the operation we need we call either the AddItem method (line 31) or the ZeroCount method (line 45). We use the GetCount method, which supplies an integer, to build the label’s Caption (line 36).

©2013 About.com. All rights reserved.