Create a class named Employee. Data members include the Employee’s ID number, fi
ID: 3712036 • Letter: C
Question
Create a class named Employee. Data members include the Employee’s ID number, first and last names, and hourly pay rate. Member functions include overloaded insertion and extraction operators. Continue to prompt the user for data for Employee sand write each Employee to a data file until the end-of-file is entered. Save the file as CreateEmployeeDataFile.cpp.
Write a program that reads the Employee file and displays Employee data on the screen. Save the file as DisplayEmployeeData.cpp.
Revise the CreateEmployeeFile application so the user is prompted for a filename for the output file. Execute the program twice, using different filenames to represent files of Employee data from the North and South divisions of the company. Save the file as RequestEmployeeFileName.cpp.
Add an overloaded operator>()function to the Employee class. The operator>()function compares Employee ID numbers and is used to sort Employee objects. Write an application that reads the North and South division files created in part c of this exercise. As each record is read, display it and load it into an array of Employee objects. Then sort the array by Employee ID number and display the newly merged and sorted records. Save the file as MergeEmployeeRecords.cpp.
Explanation / Answer
Here is your program implemented in C++ (ofc :P)
a) CreateEmployeeDataFIle.cpp
#include<iostream>
#include<string>
using namespace std;
class Employee{
int eid;
string fName;
string lName;
int hourlyPay;
public:
Employee operator>(Employee e1, Employee e2) {
if(e1.eid > e2.eid) {
return true;
} else {
return false;
}
}
}
int main() {
Employee e;
fstream file;
string fileName;
cout << "Enter the filename: ";
getline(cin, fileName);
file.open(fileName.c_str(), ios::out | ios::in);
int ch = N;
do {
cout << "Enter the Employee ID: ";
cin >> e.eid;
cout << "Enter the First Name: ";
getline(cin, fName);
cout << "Enter the Last Name: ";
getline(cin, lName);
cout << "Enter the hourly pay rate: ";
cin >> hourlyPay;
file.write((char *)&e, sizeof(Employee));
cout << "Do you want to continue(y/n: ";
cin >> ch;
if(ch == 'N' | ch == 'n') {
break;
}
} while(true);
file.close();
}
b) DisplayEmployeeData.cpp
void displayEmployee() {
fstream file;
string fileName;
cout << "Enter the filename: ";
getline(cin, fileName);
file.open(fileName.c_str(), ios::out | ios::in);
while(file.read((char *)&e, sizeof(Employee))) {
cout << "The Employee ID: ";
cout << e.eid << endl;
cout << "The First Name: ";
cout << fName << endl;
cout << "The Last Name: ";
cout << lName << endl;
cout << "The hourly pay rate: ";
cout << hourlyPay << endl;
cout << endl;
}
file.close();
}
c) RequestEmployeeFileName.cpp
This part of the question is answered in part (a) itself. It is a few simple lines of code wherein it asks for user input. You can seem the implementation in part(a) and if you further like to understand comment down below.
d) MergeEmployeeRecords.cpp
void mergeEmployees() {
Employee e1, e2;
fstream file1, file2, file3;
string fileName;
cout << "Enter the first filename: ";
getline(cin, fileName);
file1.open(fileName.c_str(), ios::out | ios::in);
getline(cin, fileName);
file2.open(fileName.c_str(), ios::out | ios::in);
file1.read((char *)&e1, sizeof(Employee));
file2.read((char *)&e2, sizeof(Employee));
fileName = "Merged Records.txt";
file1.open(fileName.c_str(), ios::out | ios::in);
while(true) {
if(file1.eof()) {
while(file2.read((char *)&e2, sizeof(Employee))) {
file3.write((char *)&e2, sizeof(Employee));
}
break;
} else if(file2.eof()) {
while(file1.read((char *)&e1, sizeof(Employee))) {
file3.write((char *)&e2, sizeof(Employee));
}
break;
}
if(e1 > e2) {
file3.write((char *)&e1, sizeof(Employee));
file1.read((char *)&e1, sizeof(Employee));
} else {
file3.write((char *)&e2, sizeof(Employee));
file2.read((char *)&e2, sizeof(Employee));
}
}
file1.close();
file2.close();
file3.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.