Advanced Inheritance 579
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
On line 79, the class PartsListis declared to be a friend to the PartNode
class.
This listing places the friend declaration in the public section, but this is not required; it
can be put anywhere in the class declaration without changing the meaning of the state-
ment. Because of this statement, all the private member data and functions are available
to any member function of class PartsList.
On line 157, the implementation of the member function GetFirst()reflects this
change. Rather than returning pHead->GetPart, this function can now return the other-
wise private member data by writing pHead->itsPart. Similarly, the Insert()function
can now write pNode->itsNext = pHead, rather than writing pNode->SetNext(pHead).
Admittedly, these are trivial changes, and a good enough reason does not exist to make
PartsLista friend of PartNode, but they do serve to illustrate how the keyword friend
works.
Declarations of friendclasses should be used with extreme caution. If two classes are
inextricably entwined, and one must frequently access data in the other, good reason
might exist to use this declaration. But use it sparingly; it is often just as easy to use the
public accessor methods, and doing so enables you to change one class without having to
recompile the other.
OUTPUT
ANALYSIS