Program To Implement Serialization
Program To Implement Serialization
Edit Doc.cpp, and insert the Menu Item Click Event Handler for EditUser,
void CCustDoc::OnEdituser()
{
CUsersDlg dlg;
if(dlg.DoModal() == IDOK)
{
CUser* pUser = new CUser(dlg.m_strName, dlg.m_strAddr);
m_setOfUsers.Add(pUser);
UpdateAllViews(NULL);
SetModifiedFlag();
}
}
1
And also add the definition for user defined functions,
int CCustDoc::GetCount() const
{
return m_setOfUsers.GetSize();
}
Open the Notepad and type the following and save as Users.h
#pragma once
class CUser : public CObject
{
DECLARE_SERIAL(CUser);
public:
// Constructors
CUser();
CUser(const CString& strName, const CString& strAddr);
// Attributes
void Set(const CString& strName, const CString& strAddr);
CString GetName() const;
CString GetAddr() const;
// Operations
2
virtual void Serialize(CArchive& ar);
// Implementation
private:
// The user's name
CString m_strName;
// The user's e-mail address
CString m_strAddr;
};
Open notepad and type the following and save as Users.cpp file..
#include "stdafx.h"
#include "Users.h"
IMPLEMENT_SERIAL(CUser, CObject, 1);
CUser::CUser() { }
3
{
ar << m_strName << m_strAddr;
}
}
1. Go to Resources pane, Insert a new Dialog with two edit boxes for Name and
Mail Id.
2. Create member Variables of type CString for both the edit controls, the variable
names should be similar to the one used in Users.cpp “ as m_ “, which u’ve
assigned in the Get, Set, etc. functions.
3. Then insert a New class for this dialog by clicking the class wizard when u are in
the dialog box, it automatically will promp as “Do u wish to create a new Class,
Say Yes, and give the class name as UsersDlg, respective header and cpp files
will be created.