home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK11 / MFC / SAMPLES / TUTORIAL / PERSON.CP$ / person
Encoding:
Text File  |  1992-01-10  |  4.5 KB  |  189 lines

  1. // person.cpp : Defines the class behaviors for CPerson, CPersonList.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "person.h"
  14. #include <string.h>
  15.  
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char BASED_CODE THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. ////////////////////////////////////////////
  22. //
  23. // Call 'IMPLEMENT_SERIAL' macro for all the
  24. //  classes declared in person.h
  25.  
  26. IMPLEMENT_SERIAL( CPerson, CObject, 0 )
  27. IMPLEMENT_SERIAL( CPersonList, CObList, 0 )
  28.  
  29. //////////////////////////////////////////////
  30. // Define member functions for classes
  31. //  declared in person.h
  32. //
  33.  
  34. //////////////////////////////////////////////
  35. //  CPerson::CPerson
  36. //  Copy Constructor for CPerson class
  37. //
  38. CPerson::CPerson( const CPerson& a )
  39. {
  40.     ASSERT_VALID( this );
  41.     ASSERT_VALID( &a );
  42.     m_LastName = a.m_LastName;
  43.     m_FirstName = a.m_FirstName;
  44.     m_PhoneNumber = a.m_PhoneNumber;
  45.     m_modTime = a.m_modTime;
  46. }
  47.  
  48. //////////////////////////////////////////////
  49. //  CPerson::CPerson
  50. //  Memberwise Constructor for CPerson class
  51. //
  52. CPerson::CPerson( const char* pszLastName,
  53.          const char* pszFirstName,
  54.          const char* pszPhoneNum )
  55. {
  56.     ASSERT_VALID( this );
  57.     m_LastName = pszLastName;
  58.     m_FirstName = pszFirstName;
  59.     m_PhoneNumber = pszPhoneNum;
  60.     m_modTime = CTime::GetCurrentTime();
  61. }
  62.  
  63. //////////////////////////////////////////////
  64. //  CPerson::operator=
  65. //  Overloaded operator= to perform assignments.
  66. //
  67. CPerson& CPerson::operator=( const CPerson& b )
  68. {
  69.     ASSERT_VALID( this );
  70.     ASSERT_VALID( &b );
  71.     m_LastName = b.m_LastName;
  72.     m_FirstName = b.m_FirstName;
  73.     m_PhoneNumber = b.m_PhoneNumber;
  74.     m_modTime = b.m_modTime;
  75.     return *this;
  76. }
  77.  
  78. ///////////////////////////////////////////////
  79. //  CPerson::Dump
  80. //  Write the contents of the object to a
  81. //  diagnostic context
  82. //
  83. // The overloaded '<<' operator does all the hard work
  84. //
  85. #ifdef _DEBUG
  86.  
  87. void CPerson::Dump( CDumpContext& dc ) const
  88. {
  89.     ASSERT_VALID( this );
  90.     // Call base class function first
  91.     CObject::Dump( dc );
  92.  
  93.     // Now dump data for this class
  94.     dc   << "\n"
  95.         << "Last Name: " << m_LastName << "\n"
  96.         << "First Name: " << m_FirstName << "\n"
  97.         << "Phone #: " << m_PhoneNumber << "\n"
  98.         << "Modification date: " << m_modTime << "\n";
  99. }
  100.  
  101. void CPerson::AssertValid() const
  102. {
  103.     CObject::AssertValid();
  104. }
  105.  
  106. #endif
  107.  
  108. /////////////////////////////////////////////
  109. //  CPerson::Serialize
  110. //  Read or write the contents of the object
  111. //  to an archive
  112. //
  113. void CPerson::Serialize( CArchive& archive )
  114. {
  115.     ASSERT_VALID( this );
  116.     // Call base class function first
  117.     CObject::Serialize( archive );
  118.  
  119.     // Now dump data for this class
  120.     if ( archive.IsStoring() )
  121.     {
  122.         TRACE( "Serializing a CPerson out.\n" );
  123.         archive << m_LastName << m_FirstName << m_PhoneNumber
  124.                 << m_modTime;
  125.     }
  126.     else
  127.     {
  128.         TRACE( "Serializing a CPerson in.\n" );
  129.         archive >> m_LastName >> m_FirstName >> m_PhoneNumber
  130.                 >> m_modTime;
  131.     }
  132. }
  133.  
  134. //////////////////////////////////////////////////////////////////////////////
  135. //  CPersonList
  136. //  We inherit most of the functionality from CObList.  These are only
  137. //  functions that we added to form our own list type.
  138. //
  139.  
  140. //////////////////////////////////////////////
  141. //  CPersonList::FindPerson
  142. //
  143. CPersonList* CPersonList::FindPerson( const char * szTarget )
  144. {
  145.     ASSERT_VALID( this );
  146.     
  147.     CPersonList* pNewList = new CPersonList;
  148.     CPerson* pNext = NULL;
  149.     
  150.     // Start at front of list
  151.     POSITION pos = GetHeadPosition();
  152.  
  153.     // Iterate over whole list
  154.     while( pos != NULL )
  155.     {
  156.         // Get next element (note cast)
  157.         pNext = (CPerson*)GetNext(pos);
  158.     
  159.         // Add current element to new list if it matches
  160.         if ( _strnicmp( pNext->GetLastName(), szTarget, strlen( szTarget ) )
  161.                  == 0 )
  162.             pNewList->AddTail(pNext);
  163.     }
  164.     
  165.     if ( pNewList->IsEmpty() )
  166.     {
  167.         delete pNewList;
  168.         pNewList = NULL;
  169.     }
  170.     
  171.     return pNewList;
  172. }
  173.  
  174. //////////////////////////////////////////
  175. //  CPersonList::DeleteAll
  176. //  This will delete the objects in the list as the pointers.
  177. //
  178. void CPersonList::DeleteAll()
  179. {
  180.     ASSERT_VALID( this );
  181.     POSITION pos = GetHeadPosition();
  182.  
  183.     while( pos != NULL )
  184.     {
  185.         delete GetNext(pos);
  186.     }
  187.     RemoveAll();
  188. }
  189.