📄 employ3.cpp
字号:
/*
employ3.cpp. stream inserters.
*/
#include "stdio.h"
#include "stdlib.h"
#include <iostream.h>
#include <fstream.h>
#include <string.h>
class CEmployee
{
public:
CEmployee (int ID, char *First, char *Last);
CEmployee ()
{
m_EmployeeFirst = NULL;
m_EmployeeLast = NULL;
}
CEmployee (const CEmployee &);
~CEmployee ();
int GetID () {return (m_EmployeeID);}
char *GetFirstName () {return (m_EmployeeFirst);}
char *GetLastName () {return (m_EmployeeLast);}
void PutNewName (char *NewFirst, char *NewLast);
const CEmployee& operator= (const CEmployee &);
friend ofstream& operator<<(ofstream& stream, CEmployee& c);
friend ostream& operator<<(ostream& stream, CEmployee& c);
friend ifstream& operator<<(ifstream& stream, CEmployee& c);
friend istream& operator<<(istream& stream, CEmployee& c);
private:
int m_EmployeeID;
char *m_EmployeeFirst;
char *m_EmployeeLast;
};
ostream& operator<<(ostream& stream, CEmployee& c)
{
stream << "First Name: ";
stream << c.m_EmployeeFirst << endl;
stream << "Last Name: ";
stream << c.m_EmployeeLast << endl;
stream << "ID Number: " << c.m_EmployeeID << endl;
return (stream);
}
ofstream& operator<<(ofstream& stream, CEmployee& c)
{
stream << c.m_EmployeeFirst << ',';
stream << c.m_EmployeeLast << ',';
stream << c.m_EmployeeID << endl;
return (stream);
}
istream& operator>>(istream& stream, CEmployee& c)
{
char First[128], Last[128], ID[128];
cout << "Employee's First Name: ";
stream >> First;
cout << "Employee's Last Name: ";
stream >> Last;
cout << "Employee's ID Number: ";
cin >> ID;
CEmployee temp(atoi (ID), First, Last);
c = temp;
return (stream);
}
ifstream& operator>>(ifstream& stream, CEmployee& c)
{
char buf[128];
stream >> buf;
char *First = strtok (buf, ",");
char *Last = strtok (NULL, ",");
char *ID = strtok (NULL, ",");
CEmployee temp(atoi (ID), First, Last);
c = temp;
return (stream);
}
int main ()
{
CEmployee emp[] = {
CEmployee (1641, "Thomas", "Jefferson"),
CEmployee (1802, "George", "Washington"),
CEmployee (1732, "John", "Adams"),
CEmployee (1752, "Andrew", "Jackson"),
CEmployee (2401, "Elsie", "Smith")
};
CEmployee NewEmp;
ifstream i("employ3.dat", ios::in | ios::out
| ios::binary);
i >> NewEmp;
cout << NewEmp;
return (0);
cin >> NewEmp;
cout << NewEmp;
CEmployee Emp = emp[0];
emp[0].PutNewName("Lawrence", "Smith");
cout << Emp;
ofstream f("employ3.dat", ios::in | ios::out
| ios::binary);
for (int x = 0; x < 6; ++x)
f << emp[x];
f.close ();
// printf ("%s %s\n", Emp.GetFirstName(), Emp.GetLastName());
// printf ("%s %s\n", emp[0].GetFirstName(), emp[0].GetLastName());
}
CEmployee::CEmployee (int ID, char *First, char *Last)
{
m_EmployeeID = ID;
m_EmployeeFirst = new char [strlen (First) + 1];
strcpy (m_EmployeeFirst, First);
m_EmployeeLast = new char [strlen (Last) + 1];
strcpy (m_EmployeeLast, Last);
}
CEmployee::~CEmployee ()
{
delete [] m_EmployeeFirst;
delete [] m_EmployeeLast;
}
void CEmployee::PutNewName (char *NewFirst, char *NewLast)
{
delete [] m_EmployeeFirst;
delete [] m_EmployeeLast;
m_EmployeeFirst = new char [strlen (NewFirst) + 1];
strcpy (m_EmployeeFirst, NewFirst);
m_EmployeeLast = new char [strlen (NewLast) + 1];
strcpy (m_EmployeeLast, NewLast);
}
const CEmployee& CEmployee::operator=(const CEmployee &Old)
{
delete [] m_EmployeeFirst;
delete [] m_EmployeeLast;
m_EmployeeFirst = new char [strlen (Old.m_EmployeeFirst) + 1];
m_EmployeeLast = new char [strlen (Old.m_EmployeeLast) + 1];
strcpy (m_EmployeeFirst, Old.m_EmployeeFirst);
strcpy (m_EmployeeLast, Old.m_EmployeeLast);
m_EmployeeID = Old.m_EmployeeID;
return (*this);
}
CEmployee::CEmployee (const CEmployee &Old)
{
m_EmployeeFirst = new char [strlen (Old.m_EmployeeFirst) + 1];
m_EmployeeLast = new char [strlen (Old.m_EmployeeLast) + 1];
strcpy (m_EmployeeFirst, Old.m_EmployeeFirst);
strcpy (m_EmployeeLast, Old.m_EmployeeLast);
m_EmployeeID = Old.m_EmployeeID;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -