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

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

By Zarko Gajic, About.com

Example 3.2 An application object

If we review our experience with RAD objects (eg example 1.3 step 7) we see that three steps are needed before we have an object we can use:
  1. Define the class. We stipulate the required data and methods (the class’s ‘signature’) and define the implementation of the methods. The class definition provides a set of instructions for instantiating an object.
  2. Declare a name to use as a reference to access the object. This sets aside memory on the stack for a reference to the object.
  3. Create the object. This allocates and initialises memory for the object on the heap, and assigns the address of the object to the reference.
Adapting these steps to create our own classes and objects is the focus of this example.

Ex 3.2 step 1 Defining the TItem Class

We can use RAD generated objects as guidelines for creating our own objects. For instance, if we look at the program from example 3.1, we see that Delphi derived the form we created, TfrmCount, from TForm. It then listed the data (gpbItems … bmbReset) and the methods (procedures btnItemsClick … bmbResetClick).
type
   TfrmCount = class(TForm) // derived from TForm
     gpbItems: TGroupBox; // data fields
     lblTotal: TLabel;
     btnItems: TButton;
     btnDisplay: TButton;
     bmbReset: TBitBtn;
     procedure btnItemsClick(Sender: TObject) ; // methods
     procedure btnDisplayMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
     procedure btnDisplayMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
     procedure bmbResetClick(Sender: TObject) ;

   private // visibility specifier
     { Private declarations }
   public
     { Public declarations }
end;
We will define an application class, TItem, to track the number of items for the conveyor belt problem above. From example 3.1, we see that TItem will need a (private) data field, which we will call Count, and (public) methods to add 1 to Count (a procedure), to get the value of Count (a function) and to zero the value of Count (a procedure).

Because object orientation is about encapsulation and keeping data and the inner workings of classes private, we create this new class in a separate unit. So the existing unit accompanying frmCount will contain the user interface and will communicate with the separate unit containing the object that is keeping count of the items.

Open the project file from example 3.1. Save the unit file as OODriverU. Save the project file as OODriverP.

We need a second unit (without a form) for the TItem class definition. Select File | New | Unit from the menu . This unit has no accompanying form and Delphi provides only the 2 unit name and the skeleton for the interface and implementation sections:

unit Unit2;
interface
implementation
end.
With RAD classes Delphi generates the class definition and the skeleton for each method. When creating our own classes we can do this manually. We add TItem’s class declaration to the interface section and its three methods (AddItem, GetCount and ZeroCount) to the implementation section. We save it as ItemU .pas
unit ItemU;

interface

type
   // derived from TObject
4    TItem = class(TObject)
5    private
     // data usually private
6      FCount: integer;
7    public
     // Public access methods
8      procedure AddItem;
9      function GetCount: integer;
10      procedure ZeroCount;
11    end;

12 implementation

13 { TItem }

14 procedure TItem.AddItem;
15 begin
16    Inc(FCount) ;
17 end;

18 function TItem.GetCount: integer;
19 begin
20    Result := FCount;
21 end;

22 procedure TItem.ZeroCount;
23 begin
24    FCount := 0;
25 end;

26 end.
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Programmer Defined Classes and Objects - Delphi OOP Part 3 / Chapter 6

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

All rights reserved.