0

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. All three classes are inherited from class SchoolMember publicly. SchoolMember has data members int id and char name[30]. Student, Teacher and Staff has some additional data members and member functions.

I have three different functions removeStudent(), removeTeacher() and removeStaff() to remove contents of student, teacher and staff respectively from a binary file.
Here's how removeTeacher() looks like:

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! The only difference is in object i.e. in removeStudent() Student studentRead is used and in removeTeacher() Teacher teacherRead is used and also, the file path is different.

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. 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. All the data members of class Student ain't removed. I want to remove all the data members of the 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

1 Answers1

0

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/

newkid
  • 1,368
  • 1
  • 11
  • 27