Delphi Programming

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

Learning Object Oriented Programming with Delphi

By Zarko Gajic, About.com

Line 7 derives a new class, called TForm1, from TForm. TForm is a standard Delphi class and is the template for the blank form that appears when we start a new application. The standard TForm class has a lot of capability built into it. For instance, it ‘knows’ (through its methods) how to create itself and how to show itself on the screen. It also has properties like a Caption, a Height and a Width. Because of line 7 we inherit all this capability from TForm as a starting point for our own class, TForm1. At the moment we still have only the unadorned form and don’t extend it in any way. In a little while, when we add some Components and event handlers, we will see how Delphi uses its RAD facility to add these objects and methods to the type declaration of TForm1.

The var (variable) declaration (lines 13 & 14): The previous subsection, the type declaration, provides the additional classes that we define in our program. However, a type is a template only and not an object. In this section, the var declaration, we declare what objects we will create from the available classes. This sets aside memory on the stack for references to these objects.

The only object so far is the form that Delphi creates automatically when we start a new project. It is called Form1, and is of type TForm1 (line 14). To help us keep the distinction between the type and the object, Delphi has the convention of starting type names with a ‘T’. Thus, TForm1 is the type and Form1 is the object.

The implementation section

The third part of a unit is the implementation section. This is where we write our program code. Because we have not written any routines yet the implementation section is empty except for the compiler directive {$R *.DFM} (line 16). At first glance, the curly brackets make this compiler directive look like a comment, and so presumably we should be able to delete it. But don’t be fooled! The opening symbol here is the compound symbol {$ which means that it is not a conventional comment but is instead a special instruction to the compiler. {$R *.DFM} is a resource directive and tells the compiler to include the form file (the .dfm file) in the application. So do not delete this line. An optional local uses clause and unit level constant and variable declarations, not present here, can appear followed by the necessary method definitions. (Ex 1.3 step 2, line 17 shows a local uses clause.)

Unlike the interface section, which makes communication with other units possible, the implementation section is private to the unit. It describes the inner workings of all the classes declared in the type declaration.

Ex 1.1 Summary

The unit file has a name for identifying itself, an interface section for communicating with other units and an implementation section giving the private details of the unit’s functioning. The interface section contains the global uses clause, the type declaration, and the global constant and variable declarations. The implementation section contains the local uses clause, the local constant and variable declarations, and the method definitions.

Delphi automatically creates a skeleton of the new TForm1 class we are creating. It uses the standard TForm class as the basis of the new TForm1 class by deriving TForm1 from TForm.

Next Chapter:
Extending an Empty Application; Analysing the RAD-generated code; A UML Class Diagram; Object Interaction; A Second Form in a Program, ...

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

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

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.