CHAPTER 8
|
Table of Contents
Learning C++:
An Index of Entry Points
2. The A reference document on the basic elements of C++.
3. The Patterns
|
Section IV. Lists: Working With
Encapsulation
Two general approaches seem to exist for solving these problems. Note
that the key issue is access to the contracts in the list. One answer
would be to have the class 'ListOfContracts' do all the work. That is, we
could declare the functions 'DisplayInfo', 'ChangeInfo' and 'DisplayAll'
as member functions of 'ListOfContracts'. This would work because any
member function of 'ListOfContracts' has access to the array. However,
many software engineers would be uncomfortable forcing a class whose
purpose is to handle list processing to handle changes to individual
elements of the list.
A second solution would be to add a new member function to the
'ListOfContracts' class. This function would receive a contract ID and
return either a contract (if one exists with the desired ID) or some kind
of failure indicator. If we do this, we will also need a member function
to replace a contract after it has been changed. This solution handles the
first two problems because it provides access to individual contracts but
does nothing to fix 'DisplayAll'. We could provide a member function that
returns the contracts one at a time when some function in the main program
says 'next'. This would work but is getting quite complex.
How about a compromise! The 'DisplayAll' function does seem to be
appropriate as a member function of 'ListOfContracts', since it involves
the whole list. Therefore, the second solution could be used for the
'DisplayInfo' and ChangeInfo' functions, while 'DisplayAll' becomes a
member function of 'ListOfContracts'. In other words, 'DisplayInfo' and
ChangeInfo' would remain part of the main program (with 'ListOfContracts'
providing functions to return and replace contracts), while
'ListOfContracts' would take on the responsibility of displaying all the
contracts. Some software engineers might argue that we have arbitrarily
split the functionality here but that is the approach we shall use.
It seems then that we have discovered three new responsibilities for
the 'ListOfContracts' class:
Here is a modified CRC card:
![]()
|
|