PERSON.H contains a list of preprocessor directives and two C++ class declarations. Class CPerson defines a class of “person objects.” Class CPersonList defines a class of list objects capable of containing CPerson objects.
·To create the PERSON.H interface file:
1.Create a file called PERSON.H and add the following directives at the top of the file:
#ifndef __PERSON_H__
#define __PERSON_H__
#ifdef _DOS
#include <afx.h>
#else
#include <afxwin.h>
#endif
#include <afxcoll.h>
The directives above prevent any implementation code in PERSON.H from being included twice, in case two files include PERSON.H and one of them includes the other. This is a common safety measure used in all Microsoft Foundation #include files. If the code were included twice, you'd get linker errors.
2.Add the following class declaration for CPerson to PERSON.H:
// class CPerson:
// Represents one person in the phone database. This class is
// derived from CObject (mostly to get access to the serialization
// protocol).
//
class CPerson : public CObject
{
DECLARE_SERIAL( CPerson );
public:
//Construction
// For serializable classes, declare a constructor with no
// arguments.
CPerson()
{ m_modTime = CTime::GetCurrentTime(); }
CPerson( const CPerson& a );
// For our convenience, also declare a constructor with arguments.
CPerson( const char* pszLastName,
const char* pszFirstName,
const char* pszPhoneNum );
//Attributes
// Member functions to modify the protected member variables.
void SetLastName( const char* pszName )
{ ASSERT_VALID( this );
ASSERT( pszName != NULL);
m_LastName = pszName;
m_modTime = CTime::GetCurrentTime(); }
const CString& GetLastName() const
{ ASSERT_VALID( this );
return m_LastName; }
void SetFirstName( const char* pszName )
{ ASSERT_VALID( this );
ASSERT( pszName != NULL );
m_FirstName = pszName;
m_modTime = CTime::GetCurrentTime(); }
const CString& GetFirstName() const
{ ASSERT_VALID( this );
return m_FirstName; }
void SetPhoneNumber( const char* pszNumber )
{ ASSERT_VALID( this );
ASSERT( pszNumber != NULL );
m_PhoneNumber = pszNumber;
m_modTime = CTime::GetCurrentTime(); }
const CString& GetPhoneNumber() const
{ ASSERT_VALID( this );
return m_PhoneNumber; }
const CTime GetModTime() const
{ ASSERT_VALID( this );
return m_modTime; }
//Operations
CPerson& operator=( const CPerson& b );
//Implementation
protected:
// Member variables that hold data for person
CString m_LastName;
CString m_FirstName;
CString m_PhoneNumber;
CTime m_modTime;
public:
// Override the Serialize function
virtual void Serialize( CArchive& archive );
#ifdef _DEBUG
// Override Dump for debugging support
virtual void Dump( CDumpContext& dc ) const;
virtual void AssertValid() const;
#endif
};
C++ techniques are used to derive class CPerson from the Microsoft Foundation Class CObject. Notice that the class declares several constructors, an overloaded assignment operator, several member functions for getting and setting the attributes of a person object, and several member variables for storing information about a person. The class also takes advantage of CObject's ability to write an object's contents to disk by invoking the DECLARE_SERIAL macro and overriding the Serialize member function. In addition, it overrides several of CObject's member functions to provide diagnostics during program development. The class is discussed in detail in “Discussion: The CPerson Class” on page 28.
3.Add the following directive as the last line of code in PERSON.H:
#endif // __PERSON_H__
Later you'll add the declaration for class CPersonList to PERSON.H. Be sure to keep this #endif directive as the last line of code in the file.