PERSON.CPP contains several preprocessor directives, two macro invocations to support object serialization, and definitions for several of the member functions of classes CPerson and CPersonList. Some of the member functions were defined inline as part of the class declarations in file PERSON.H, but the longer ones were left for definition in PERSON.CPP.
·To create the PERSON.CPP implementation file:
1.Create a file called PERSON.CPP and add the following directives at the top of the file:
#include "person.h"
#include <string.h>
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
Besides #include directives, these directive lines provide support for debugging when the _DEBUG flag is defined. The directives help identify which file an error occurred in.
2.Add the following macro invocations to PERSON.CPP below the preprocessor directives:
// Call 'IMPLEMENT_SERIAL' macro for all the
// classes declared in person.h
IMPLEMENT_SERIAL( CPerson, CObject, 0 )
This line adds code to support object serialization so that CPerson and
CPersonList objects can write themselves to a disk file and read themselves in from a file. Serialization is discussed in detail in “How to Serialize a CPerson Object” on page 33. Later you'll add a similar line for the CPersonList class.
3.Add the following member function definitions for class CPerson:
// CPerson::CPerson
// Copy Constructor for CPerson class
//
CPerson::CPerson( const CPerson& a )
{
ASSERT_VALID( this );
ASSERT_VALID( &a );
m_LastName = a.m_LastName;
m_FirstName = a.m_FirstName;
m_PhoneNumber = a.m_PhoneNumber;
m_modTime = a.m_modTime;
}
// CPerson::CPerson
// Memberwise Constructor for CPerson class
//
CPerson::CPerson( const char* pszLastName,
const char* pszFirstName,
const char* pszPhoneNum )
{
ASSERT_VALID( this );
m_LastName = pszLastName;
m_FirstName = pszFirstName;
m_PhoneNumber = pszPhoneNum;
m_modTime = CTime::GetCurrentTime();
}
// CPerson::operator=
// Overloaded operator= to perform assignments
//
CPerson& CPerson::operator=( const CPerson& b )
{
ASSERT_VALID( this );
ASSERT_VALID( &b );
m_LastName = b.m_LastName;
m_FirstName = b.m_FirstName;
m_PhoneNumber = b.m_PhoneNumber;
m_modTime = b.m_modTime;
return *this;
}
// CPerson::Dump
// Write the contents of the object to a
// diagnostic context
//
// The overloaded '<<' operator does all the hard work
//
#ifdef _DEBUG
void CPerson::Dump( CDumpContext& dc ) const
{
ASSERT_VALID( this );
// Call base class function first
CObject::Dump( dc );
// Now dump data for this class
dc << "\n"
<< "Last Name: " << m_LastName << "\n"
<< "First Name: " << m_FirstName << "\n"
<< "Phone #: " << m_PhoneNumber << "\n"
<< "Modification date: " << m_modTime << "\n";
}
void CPerson::AssertValid() const
{
}
#endif
// CPerson::Serialize
// Read or write the contents of the object
// to an archive
//
void CPerson::Serialize( CArchive& archive )
{
ASSERT_VALID( this );
// Call base class function first
CObject::Serialize( archive );
// Now dump data for this class
if ( archive.IsStoring() )
{
TRACE( "Serializing a CPerson out.\n" );
archive << m_LastName << m_FirstName
<< m_PhoneNumber << m_modTime;
}
else
{
TRACE( "Serializing a CPerson in.\n" );
archive >> m_LastName >> m_FirstName
>> m_PhoneNumber >> m_modTime;
}
}
These definitions complete the member functions declared as part of the CPerson class declaration. They define the following member functions:
4.A copy constructor—to make copies of a CPerson object
5.A constructor—to create new CPerson objects with initializing information passed as parameters
6.An overloaded assignment operator—to assign one CPerson object to another
7.A Dump member function— to dump diagnostic information about CPerson objects
8.An AssertValid member function—to test the validity of a CPerson object
9.A Serialize member function—to write a CPerson object's data to a file or read data from a file into a CPerson object
Later you'll add member function definitions for class CPersonList to PERSON.CPP.
At this point, you've added all of the code for class CPerson to both files.
To continue the tutorial, see “Design the CPersonList Object” on page 36. For more information about the steps you just completed, see “Discussion: The CPerson Class,” which follows.