[英]Writing Derived Class Members to Binary File
I am working on a school-management-system command line interface application. 我正在研究学校管理系统命令行界面应用程序。 First, let me try to explain how's my program is designed and what I am trying to achieve.
首先,让我尝试解释程序的设计方式以及我要实现的目标。
My present program design: 我现在的程序设计:
I have three classes viz. 我有三堂课。
Student
, Teacher
and Staff
. Student
, Teacher
和Staff
。 All three classes are inherited from class SchoolMember
publicly. 这三个课程都是从
SchoolMember
公开继承的。 SchoolMember
has data members int id
and char name[30]
. SchoolMember
具有数据成员int id
和char name[30]
。 Student
, Teacher
and Staff
has some additional data members and member functions. Student
, Teacher
和Staff
还有一些其他数据成员和成员函数。
I have three different functions removeStudent()
, removeTeacher()
and removeStaff()
to remove contents of student, teacher and staff respectively from a binary file. 我有三个不同的函数
removeStudent()
, removeTeacher()
和removeStaff()
分别从二进制文件中删除学生,教师和教职员工的内容。
Here's how removeTeacher()
looks like: 这是
removeTeacher()
样子:
void removeTeacher()
{
Teacher teacherRead;
int inputTeacherId;
short flag = 0;
ifstream fileToRead("data/teacher.dat", ios::binary);
ofstream fileToWrite("data/temp_teacher.dat", ios::binary | ios::app);
cout << "Enter ID of teacher whose data has to be removed: ";
cin >> inputTeacherId;
while (fileToRead.read((char *)&teacherRead, sizeof(teacherRead)))
{
if (inputTeacherId == teacherRead.getId())
flag++;
else
fileToWrite.write((char *)&teacherRead, sizeof(teacherRead));
}
if (flag == 0)
cout << "Sorry, No Match found.";
else
cout << "Data of teacher " << inputTeacherId << " has been removed from file.";
fileToRead.close();
fileToWrite.close();
remove("data/teacher.dat");
rename("data/temp_teacher.dat", "data/teacher.dat");
}
The removeStudent()
and removeStaff()
function is exactly same! removeStudent()
和removeStaff()
函数完全相同! The only difference is in object ie in removeStudent()
Student studentRead
is used and in removeTeacher()
Teacher teacherRead
is used and also, the file path is different. 唯一的区别在于对象,即,在
removeStudent()
Student studentRead
,在removeTeacher()
Teacher teacherRead
,并且文件路径也不同。
What I'm trying to achieve: 我想要达到的目标:
Since the functionality of the three functions is the same. 由于三个功能的功能相同。 I want to replace it with one function.
我想用一个功能代替它。 So, I searched the internet and read some inheritance and OOP concepts.
因此,我在互联网上搜索并阅读了一些继承和OOP概念。 Then I implemented a function:
然后我实现了一个功能:
void removeMember(string filePath, string tempFilePath, SchoolMember member)
{
int inputId = 0;
short flag = 0;
ifstream fileToRead(filePath.c_str(), ios::binary);
ofstream fileToWrite(tempFilePath.c_str(), ios::binary | ios::app);
cout << "Enter ID whose data has to be removed: ";
cin >> inputId;
while (fileToRead.read((char *)&member, sizeof(member)))
{
if (inputId == member.getId())
++flag;
else
fileToWrite.write((char *)&member, sizeof(member));
}
if (flag == 0)
cout << "Sorry, No Match found.";
else
cout << "Data of ID " << inputId << " has been removed from file.";
fileToRead.close();
fileToWrite.close();
remove(filePath.c_str());
rename(tempFilePath.c_str(), filePath.c_str());
}
Now, when I call the above function in: 现在,当我在以下位置调用上述函数时:
void removeDataScreen()
{
Student schoolStudent; // Object
removeMember("data/student.dat", "data/temp_student.dat", schoolStudent);
}
Only id
and name
are removed from the student.dat
file. 仅
id
和name
从student.dat
文件中删除。 All the data members of class Student
ain't removed. Student
类的所有数据成员都不会删除。 I want to remove all the data members of the Student
. 我要删除
Student
所有数据成员。 how can that be achieved? 如何实现?
I hope I explained the issue in enough detail. 我希望我能详细解释这个问题。 I will be looking for solutions :)
我将寻找解决方案:)
Full source code can be found at: https://github.com/vkWeb/school-management-system/blob/master/main.cpp 完整的源代码可以在以下位置找到: https : //github.com/vkWeb/school-management-system/blob/master/main.cpp
For this particular problem, template functions are well suited: 对于此特定问题,模板功能非常适合:
template <class T>
void removeMember(string filePath, string tempFilePath) {
T MemberObject;
// Rest of your code
}
See: http://www.cplusplus.com/doc/oldtutorial/templates/ 请参阅: http : //www.cplusplus.com/doc/oldtutorial/templates/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.