Sams Teach Yourself C++ in 21 Days

(singke) #1
1234 was the first entry. Rejected!
(0)Quit (1)Car (2)Plane: 1
New PartNumber?: 2345
Model Year?: 93
(0)Quit (1)Car (2)Plane: 0

Part Number: 1234
Model Year: 94

Part Number: 2345
Model Year: 93

Part Number: 4434
Model Year: 93
Listing 16.6 shows a changed interface to PartsCatalogand the rewritten driver
program. The interfaces to the other classes are unchanged from Listing 16.5.
On line 253 of Listing 16.6,PartsCatalogis declared to derive privately from
PartsList. The interface to PartsCatalogdoesn’t change from Listing 16.5, although,
of course, it no longer needs an object of type PartsListas member data.
The PartsCatalog ShowAll()function on line 160 calls PartsList Iterate()with the
appropriate pointer to member function of class Part. ShowAll()acts as a public inter-
face to Iterate(), providing the correct information but preventing client classes from
calling Iterate()directly. Although PartsListmight allow other functions to be passed
to Iterate(),PartsCatalogdoes not.
The Insert()function on lines 164–284 has changed as well. Note, on line 269, that
Find()is now called directly because it is inherited from the base class. The call on line
271 to Insert()must be fully qualified, of course, or it would endlessly recurse into itself.
In short, when methods of PartsCatalogwant to call PartsListmethods, they can do
so directly. The only exception is when PartsCataloghas overridden the method and the
PartsListversion is needed, in which case the function name must be qualified fully.
Private inheritance enables the PartsCatalogto inherit what it can use, but still provides
mediated access to Insert()and other methods to which client classes should not have
direct access.

570 Day 16


ANALYSIS
Free download pdf