This section explains the second step in writing the DMTEST program: design a collection object to hold the CPerson data. You'll be adding more code to your PERSON.H and PERSON.CPP files. This process will be described in two main steps:
1.Add a class declaration to file PERSON.H.
2.Add code to PERSON.CPP.
A.Add a macro invocation.
B.Add code for member functions.
Each CPerson object represents one person in the database of names and phone numbers that you're building. You can use one of the collection classes from
the Microsoft Foundation Class Library to derive your own list class to manage a list of CPerson objects. The list class designed in this section is called CPersonList.
·To add the CPersonList object code to PERSON.H:
Add the following class declaration for CPersonList to file PERSON.H after the CPerson declaration:
// class CPersonList:
// This represents a list of all persons in a phone database. This
// class is derived from CObList, a list of pointers to CObject-type
// objects.
class CPersonList : public CObList
{
DECLARE_SERIAL( CPersonList )
public:
//Construction
CPersonList()
{ m_bIsDirty = FALSE; }
// Add new functions
CPersonList* FindPerson( const char * szTarget );
// SetDirty/GetDirty
// Mark the person list as "dirty" (meaning "modified"). This
// flag can be checked later to see if the database
// needs to be saved.
//
void SetDirty( BOOL bDirty )
{ ASSERT_VALID( this );
m_bIsDirty = bDirty; }
BOOL GetDirty()
{ ASSERT_VALID( this );
return m_bIsDirty; }
// Delete All will delete the Person objects as well as the
// pointers.
void DeleteAll();
protected:
BOOL m_bIsDirty;
};
C++ techniques are used to derive class CPersonList from the Microsoft Foundation Class CObList. The class declares a constructor, a member function for searching the list, member functions for flagging changes to the list and for testing that flag, and a member function for deleting the list. The class also invokes the DECLARE_SERIAL macro to do its part in serializing the data to and from the disk.
Note that CPersonList requires only a single constructor with no arguments. This constructor is defined inline, so you don't have to add a function definition for it to your PERSON.CPP file. The definition is taken care of as part of the declaration. For more information about the CPersonList constructor, see “Discussion: The CPersonList Class” on page 39.
This completes your PERSON.H file.
·To add the CPersonList object code to PERSON.CPP:
1.Add the following macro invocation to PERSON.CPP just below the similar IMPLEMENT_SERIAL macro for CPerson:
IMPLEMENT_SERIAL( CPersonList, CObList, 0 )
2.Add code for the member functions of each CPersonList at the end of PERSON.CPP:
// CPersonList::FindPerson
//
CPersonList* CPersonList::FindPerson( const char * szTarget )
{
ASSERT_VALID( this );
CPersonList* pNewList = new CPersonList;
CPerson* pNext = NULL;
// Start at front of list
POSITION pos = GetHeadPosition();
// Iterate over whole list
while( pos != NULL )
{
// Get next element (note cast)
pNext = (CPerson*)GetNext( pos );
// Add current element to new list if it matches
if ( _strnicmp( pNext -> GetLastName(), szTarget,
strlen( szTarget ) ) == 0 )
pNewList -> AddTail(pNext);
}
if ( pNewList -> IsEmpty() )
{
delete pNewList;
pNewList = NULL;
}
return pNewList;
}
// CPersonList::DeleteAll
// This will delete the objects in the list as the pointers.
//
void CPersonList::DeleteAll()
{
ASSERT_VALID( this );
POSITION pos = GetHeadPosition();
while( pos != NULL )
{
delete GetNext(pos);
}
RemoveAll();
}
CPersonList declares four member functions to manipulate the data stored in the list and in the member variable of CPersonList. The four member functions are FindPerson, SetDirty, GetDirty, and DeleteAll. SetDirty and GetDirty are declared inline, so you've already added their definitions with the class declaration in PERSON.H.
This completes your PERSON.CPP file.
At this point, your PERSON.H and PERSON.CPP files are complete. Check to be sure that you have added all the #include and other compiler directives. Compare your code to the full code listings presented in Listings 1 and 2 on page 66 and 69. In the continuation of the tutorial, you will create a third file containing a main program to test the data model.
To continue the tutorial, see “Test the Data Model” on page 49. For more information about the steps you just completed, see “Discussion: The CPersonList Class, ” which follows.