C++ Modify your Project to: 1. Replace Class with Structure for Employee and Dep
ID: 3844767 • Letter: C
Question
C++ Modify your Project
to: 1. Replace Class with Structure for Employee and Department.
2. Inside each structure, replace all string variables with array of characters.
3. Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department.
4. Do not allow the user to edit the Employee ID and Department ID.
5. Use Random Access Files to store the data in Binary Form.
(
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
class Departments{
string departmentID,departmentName,departmentHeadName;
public:
Departments(){}
Departments(string ID,string name){
departmentID="";
departmentName="";
departmentHeadName="";
}
Departments(string ID,string name,string head){
departmentID=ID;
departmentName=name;
departmentHeadName=head;
}
void setDepartmentID(string ID){
departmentID=ID;
}
void setDepartmentName(string name){
departmentName=name;
}
void setDepartmentHeadName(string head){
departmentHeadName=head;
}
string getDepartmentID(){
return departmentID;
}
string getDepartmentName(){
return departmentName;
}
string getDepartmentHeadName(){
return departmentHeadName;
}
};
class Employee{
string employeeID, employeeName, employeeDepartmentID;
int employeeAge;
double employeeSalary;
public:
Employee(){}
Employee(string ID, string name){
employeeID=ID;
employeeName=name;
}
Employee(string ID, string name,int age){
employeeID=ID;
employeeName=name;
employeeAge=age;
}
Employee(string ID, string name,int age, double salary){
employeeID=ID;
employeeName=name;
employeeAge=age;
employeeSalary=salary;
}
Employee(string ID, string name,int age, double salary,string department){
employeeID=ID;
employeeName=name;
employeeAge=age;
employeeSalary=salary;
employeeDepartmentID=department;
}
void setEmployeeID(string ID){
employeeID=ID;
}
void setEmployeeName(string name){
employeeName=name;
}
void setEmployeeAge(int age){
employeeAge=age;
}
void setEmployeeSalary(double salary){
employeeSalary=salary;
}
void setEmployeeDepartment(string dept){
employeeDepartmentID=dept;
}
string getEmployeeID(){
return employeeID;
}
string getEmployeeName(){
return employeeName;
}
int getEmployeeAge(){
return employeeAge;
}
double getEmployeeSalary(){
return employeeSalary;
}
string getEmployeeDepartment(){
return employeeDepartmentID;
}
};
int main(){
Employee e[7];
Departments d[3];
int ch=0, employeeAge, i=0, j=0;
string departmentID,departmentName,departmentHeadName;
string employeeID, employeeName, employeeDepartmentID;
double employeeSalary;
do{
cout<<" 1. Create Department 2. Create Employee";
cout<<" 3. Write the data to the file 4. Retrieve data from file";
cout<<" 5. Display Report 6.Exit Enter your choice : ";
cin>>ch;
switch(ch){
case 1: if(i<3){
cout<<" Enter the Department Id : ";
cin.ignore();
getline(cin,departmentID);
d[i].setDepartmentID(departmentID);
cout<<" Enter the Department Name : ";
cin.ignore();
getline(cin,departmentName);
d[i].setDepartmentName(departmentName);
cout<<" Enter the Department HeadName : ";
cin.ignore();
getline(cin,departmentHeadName);
d[i].setDepartmentHeadName(departmentHeadName);
i++;
}
else
cout<<" The array is full, you can not add any more Departments";
break;
case 2: if(j<7){
cout<<" Enter the Employee Id : ";
cin.ignore();
getline(cin,employeeID);
e[j].setEmployeeID(employeeID);
cout<<" Enter the Employee Name : ";
cin.ignore();
getline(cin,employeeName);
e[j].setEmployeeName(employeeName);
cout<<" Enter the Employee Department ID : ";
cin.ignore();
getline(cin,employeeDepartmentID);
e[j].setEmployeeDepartment(employeeDepartmentID);
cout<<" Enter the Employee Age : ";
cin>>employeeAge;
e[j].setEmployeeAge(employeeAge);
cout<<" Enter the Employee Salary : ";
cin>>employeeSalary;
e[j].setEmployeeSalary(employeeSalary);
j++;
}
else
cout<<" The array is full, you can not add any more Employees";
break;
case 3: {
ofstream myfile;
myfile.open ("example.txt");
for(int k=0;k<3;k++){
myfile<<" "<<d[k].getDepartmentHeadName();
myfile<<" "<<d[k].getDepartmentID();
myfile<<" "<<d[k].getDepartmentName()<<" ";
}
myfile.close();
break;
}
case 5: cout<<" Report : ";
for(int k=0;k<3;k++){
cout<<" "<<d[k].getDepartmentHeadName()<<" ";
cout<<" "<<d[k].getDepartmentID()<<" ";
cout<<" "<<d[k].getDepartmentName()<<" ";
}
break;
case 6: exit(1);
break;
default : cout<<" Invalid option";
break;
}
}while(ch != 6);
return 0;
}
Explanation / Answer
Please find the answer part wise:
Using namespace std;
a) , b) , c) and d)
struct Employees{
char employeeID[10];
char employeeName[50];
char employeeDepartmentID[10];
int employeeAge;
double employeeSalary;
public Employees( char[] id, char[] name, char[] deptId, int age, double salary){
employeeID=id
employeeName=name
employeeDepartmentID=deptID;
employeeAge=age;
employeeSalary=salary;
}
public void changeValues (char[] name, int age, double salary)
{ // Not allowing to change empId / DeptID
employeeName=name;
employeeAge=age;
employeeSalary=salary;
}
}student;
struct Departments{
char departmentID[10];
char departmentName[50];
char departmentHeadName[50];
public Departments ( char[] did, char[] dname, chart[] DHname)
{
departmentId=did;
departmentName=dname;
departmentHeadName=DHname;
}
public void changeVals(char[] dname, char[] DHname)
{
departmentName=dname;
departmentHeadName=DHname;
}
}department;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.