home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Education
/
collectionofeducationcarat1997.iso
/
COMPUSCI
/
TOT11.ZIP
/
TOTDOC11.ZIP
/
CHAPT16.TXT
< prev
next >
Wrap
Text File
|
1991-02-11
|
17KB
|
367 lines
OOP
Principles
"The human mind treats a new idea the way the body treats a strange
protein; it rejects it."
P. B. Medawar, Biologist
Part 2 of the User's manual explains how, by using object oriented
programming, you can customize the Toolkit to meet your very specific
needs. In this chapter, some of the main features of OOP extensibility
are explained. If you are an experienced OOPer, you may want to skip
this chapter and proceed with the Toolkit specific text.
If you are new to OOP, be sure to read the Object Primer starting on
page 3.1 before you look at this chapter.
Understanding Inheritance
One of the most powerful and alluring aspects of object oriented pro-
gramming is the ability to modify and expand an object without changing
any of the object's source code. What's more, it's easy to do! The
basic approach is to create an object which inherits all the properties
of another object, i.e. you clone the object (sorry Compaq). You can
then change some of the properties of the cloned object so that it
functions the way you want it to.
This feature allows you to enhance and improve the Toolkit without
changing a single line of Toolkit source code. That way, when we
release a new and improved version, you don't have to wrestle with two
sets of source code - the code that you changed and the code that we
changed. All you need to do is "plug in" the new Toolkit and your
objects will automatically inherit the new features added to the Tool-
kit.
To illustrate object inheritance, let's solve a practical problem.
Imagine that you use the EquipOBJ object, but need to enhance it so
that it will report if a CD-ROM is installed. Rather than change the
EquipOBJ object, the approach is to create a new object which inherits
all the properties of EquipOBJ, and add the new CD-ROM method to it.
You may recall from chapter 3: Toolkit Basics that declaring an object
is very similar to declaring a record. To declare a object that inher-
its the properties of another object, you specify the name of the par-
ent object in parentheses after the object keyword. The following code
fragment declares a new object type, NewEquipOBJ, which inherits all
the properties of EquipOBJ:
NewEquipOBJ = object (EquipOBJ)
end; {NewEquipOBJ}
16-2 Extending the Toolkit
--------------------------------------------------------------------------------
If you create an object which is a descendant of a Toolkit object, you
must define two special methods known as the CONSTRUCTOR and the
DESTRUCTOR. Furthermore, these should be named INIT and DONE, respec-
tively. These special methods instruct Turbo Pascal to use special
memory techniques when creating and disposing of the object. The
NewEquipOBJ object declaration must be expanded to include the con-
structor and destructor methods as follows:
NewEquipOBJ = object (EquipOBJ)
constructor Init;
destructor Done;
end; {NewEquipOBJ}
Now we can add the new CD-ROM method to the object declaration. Assum-
ing that the new method is just a boolean function, it would be
declared as follows:
NewEquipOBJ = object (EquipOBJ)
constructor Init;
function CDROM: boolean;
destructor Done;
end; {NewEquipOBJ}
As well as adding new methods to the object, you can override or change
existing methods. For example, the GameAdapter function inherited from
EquipOBJ can be replaced with a new method by expanding the object
declaration as follows:
NewEquipOBJ = object (EquipOBJ)
constructor Init;
function CDROM: boolean;
function GameAdapter: boolean;
destructor Done;
end; {NewEquipOBJ}
Although you have only declared NewEquipOBJ with four methods, it
actually includes all the data and methods it has inherited from Equi-
pOBJ. The following table compares the data and methods declared in
EquipOBJ, with the data and methods in NewEquipOBJ. The non-bold items
are inherited.
EquipOBJ NewEquipOBJ
vMainInfo: integer; vMainInfo: integer;
vComputerID: byte; vComputerID: byte;
vRomDate: string[8]; vRomDate: string[8];
constructor Init; constructor Init;
function ComputerID:byte; function ComputerID:byte;
function ParallelPorts:byte; function ParallelPorts:byte;
function SerialPorts:byte; function SerialPorts:byte;
function FloppyDrives:byte; function FloppyDrives:byte;
OOP Principles 16-3
--------------------------------------------------------------------------------
function ROMDate:string; function ROMDate:string;
function SerialPrinter:boolean; function SerialPrinter:boolean;
function MathChip:boolean; function MathChip:boolean;
function GameAdapter:boolean; function GameAdapter:boolean;
destructor Done; function CDROM:boolean;
destructor Done;
Note: an inherited object may also include data items, provided
the identifiers (or variable names) are different from the ones
inherited from the parent object.
The object declaration defines a new object type and should be located
in the TYPE declaration section of your program or unit. The corre-
sponding detail of each object method must be listed either in the body
of your program, or in the implementation section of a unit.
Listed below is a unit, EXTDEM1.PAS, which fully defines the new object
NewEquipOBJ. One of the important points to note is that the construc-
tor and destructor methods must call the associated constructor and
destructor from the parent method. This ensures that the inherited data
is initialized and disposed of properly. These important statements are
highlighted in bold in the listing.
Unit ExtDem1;
INTERFACE
Uses DOS,CRT, totSYS;
TYPE
NewEquipOBJ = object (EquipOBJ)
constructor Init;
function CDROM: boolean;
function GameAdapter: boolean;
destructor Done;
end; {NewEquipOBJ}
IMPLEMENTATION
constructor NewEquipOBJ.Init;
{}
begin
EquipOBJ.Init;
end; {NewEquipOBJ.Init}
16-4 Extending the Toolkit
--------------------------------------------------------------------------------
function NewEquipOBJ.CDROM:boolean;
{If you know how to do this - please tell us!
For now, we'll assume one isn't installed}
begin
CDROM := false;
end; {NewEquipOBJ.CDROM}
function NewEquipOBJ.GameAdapter:boolean;
{}
begin
GameAdapter := paramstr(1) = '/G';
end; {NewEquipOBJ.GameAdapter}
dest