I'm just a 4-months newbie in C++, and I have an assignment to do a small phonebook program for DOS or FreeDOS. This is what i got from days after searching:
This is CTelrecord class that would save every record in details
class CTelRecord
{
private:
int szTime;
char szName[_MAX_NAME_LEN_];
char szNumber[_MAX_NUMBER_LEN_];
public:
CTelRecord();
CTelRecord(char *name, char *number);
void SetRecord(char *name, char *number);
void SetRecTime(int);
int Compare(CTelRecord *);
void Show();
void ShowName();
void ShowNumber();
void ShowRecTime(short int);
nTime TimeParser();
void ModifyNumber(char *number);
void ModifyName(char *name);
int LookChar(char key);
int SearchMore(char key[_MAX_NAME_LEN_]);
char *GetName()
{
return szName;
}
char *GetNum()
{
return szNumber;
}
int &GetTime()
{
return szTime;
}
};
And this is CNode class for constructing each node of the linked list.
class CNode
{
private:
CTelRecord *pData;
CNode *pNext;
public:
CNode()
{
pData=new CTelRecord;
if (pData==NULL)
{
cout<<"Memory error!"<<endl;
exit(0);
}
pNext=NULL;
}
CNode(CNode &node)
{
//cout<<"copying \n";
node.pData=new CTelRecord;
node.pNext=new CNode;
if (node.pData==NULL||node.pNext==NULL)
{
cout<<"Memory error!"<<endl;
exit(0);
}
node.pData=pData;
node.pNext=pNext;
};
void InputData(CTelRecord *pdata)
{
pData=pdata;
}
void ShowNode()
{
pData->Show();
}
CTelRecord *GetData()
{
return pData;
}
CNode *GetNext()
{
return pNext;
}
~CNode()
{
//cout<<"Deconstructing..."<<endl;
//what should i do here?
}
friend class CList;
};
And finally this is CList class to handle controls for my linked list.
class CList
{
CNode *pHead;
CNode *pCurrent;
public:
CList()
{
pHead=0;
pCurrent=0;
}
~CList()
{
DeleteList();
}
void LoadListFromFile(char *datafile);
void WriteListToFile(char *datafile);
void InsertNode(CNode *);
void InsertNewHead(CNode *);
void InsertToEnd(CNode *);
void MoveCurrentToEnd();
void DeleteNode(CNode *);
void SearchNode(char *, CNode *SearchResult[_MAX_SEARCH_RESULTS_], int &ResultCount);
CNode *GetInsertPosition(CNode *);
CNode *LookUp(CTelRecord &);
CNode *GetCurrent()
{
return pCurrent;
}
CNode *GetHead()
{
return pHead;
}
//void Show();
void DeleteList();
friend class CPhoneBook;
};
void CList::DeleteList()
{
CNode *pTemp;
pCurrent=pHead;
if (pHead!=NULL)
{
while(pCurrent->pNext!=NULL)
{
pTemp=pCurrent;
pCurrent=pCurrent->pNext;
delete []pTemp;
}
}
}
Thing that confuses me most the time is how to get it worked. Now it's working fine, but i'm not sure about the deconstruction of CNode class and CList class.
If i leave my CNode deconstruction function in peace, it works fine, but if i add this line too ~CNode() function:
if (pData) delete [] pData;
it will display memory error.
Can anyone tell me how to solve the deconstruction functions of CNode and CList?
Thanks in advance.