Sams Teach Yourself C++ in 21 Days

(singke) #1
Advanced Inheritance 561

16


(0)Quit (1)Car (2)Plane: 1
New PartNumber?: 1234
Model Year?: 94
(0)Quit (1)Car (2)Plane: 1
New PartNumber?: 4434
Model Year?: 93
(0)Quit (1)Car (2)Plane: 1
New PartNumber?: 1234
Model Year?: 94
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

OUTPUT


Some compilers cannot compile line 264, even though it is legal C++. If your
compiler complains about this line, change it to
264: void ShowAll() { thePartsList.Iterate(&Part::Display); }
(Note the addition of the ampersand in front of Part::Display.) If this fixes
the problem, immediately call your compiler vendor and complain.

NOTE


Listing 16.5 reproduces the Part,PartNode, and PartsListclasses from Week 2
in Review.
A new class,PartsCatalog, is declared on lines 257–267. PartsCataloghas a
PartsListas its data member (line 265), to which it delegates list management. Another
way to say this is that the PartsCatalogis implemented in terms of this PartsList.
Note that clients of the PartsCatalogdo not have access to the PartsListdirectly. You
can see that PartsListis declared as a private member. The interface to this is through
the PartsCatalog, and as such, the behavior of the PartsListis dramatically changed.
For example, the PartsCatalog::Insert()method does not allow duplicate entries into
the PartsList.
The implementation of PartsCatalog::Insert()starts on line 269. The Partthat is
passed in as a parameter is asked for the value of its itsPartNumbermember variable.

ANALYSIS
Free download pdf