Notice that AddItem without parameters is declared as public (line 10 above) and so can be called directly by the user interface class (eg example 3.2 step 2 line 31). However AddItem with the parameter is declared as protected and so cannot be called by the user interface object. To see this, change example 3.3 step 2 line 49 to call AddItem:
procedure TfrmCount.btnBoxClick(Sender: TObject) ;This leads to a compilation error.
begin
// out of scope
ItemCount.AddItem(4) ;
end;
In Delphi the visibility specifiers private and protected apply to the unit. So in this case we can replace protected by private and the program will still work. However, if these two classes are defined in separate units the overloaded procedure AddItem (aNoToAdd) wont be accessible with private visibility and so must be protected. (We dont make it public because we want its visibility to be restricted to TItem and its descendants.)
Chapter summary
- Keeping user interface and application objects separate: separation of concerns
- Mimicking RAD user interface objects in programmer defined application classes
- Steps in using a programmer defined object:
- define the class,
- allocate a name (reference),
- instantiate the object,
- assign it to the name.
- Inheritance: deriving a new class from an existing one
- Packaging a class in a unit
- The Code Explorer and Project Browser
- Layout of objects in memory (available in the PDF for download)
- Defining and using a class and a subclass
- Reference semantics and object layout in memory
- Separation of concerns: user interface and domain classes
Problems
Problem 3.1 Study Chapters 6 and 7 Part 3
Identify the appropriate example(s) or section(s) of the chapter to illustrate each comment made in the summary above.
Problem 3.2 Double conveyor belts
Extend example 3.3 to work for a double conveyor belt system. One conveyor moves single items and boxes of six items, while the other moves single items only. Clicking down the Display button shows the totals for each conveyor, while releasing the button hides both
totals.
Problem 3.3 Different box sizes
Consider another variation on the conveyor belt problem we have been looking at. In a warehouse there is a temporary storage area with three reversible conveyor belts. One carries individual items, the second carries small boxes of four items, and the third carries
large boxes with twelve items.
That's it for the third part of the Delphi OOP online tutorial.
Download Part 3, Chapters 6-7 as a PDF document.
Download Solution to Homework Problems.
Forward to Part 4 / Chapter 8: Introduction to class inheritance...

