a.Create a class named Employee. Data members include the Employee’s ID number,
ID: 3708516 • Letter: A
Question
a.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. b.Write a program that reads the Employee file and displays Employee data on the screen. Save the file as DisplayEmployeeData.cpp. c.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.
Explanation / Answer
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
using namespace std;
class Employee
{
//creating variables which is private
private: int empid;
char fname[20];
char lname[20];
int payrate;
//create member functions which can be overloaded
public:void insertion(int);
void insertion(char);
void extraction();
};
void insertion(int variable)
{ //insertion of variables this is overloaded one
file.open("EMPLOYEE.DAT",ios::binary|ios::app);//open file EMPLOYEE.DAT
if(!file)
{
cout<<"ERROR IN CREATING FILE "; //if file not found then display error message
return 0;
}
//write into file
file.write((int*)&variable,sizeof(variable));
}
void insertion(char var)
{
file.open("EMPLOYEE.DAT",ios::binary|ios::app); //open file
if(!file)
{
cout<<"ERROR IN CREATING FILE ";
return 0;
}
//write into file
file.write((char*)&var,sizeof(var));
file.close();
}
void extraction()
{
Employee x;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file)
{
cout<<"ERROR IN OPENING FILE ";
return;
}
while(file)
{
cout<<empid<<" "<<fname<<" "<<lname<<" "<<salary<<" "; //display details of file
}
file.close(); //close file
}
void main()
{
//declare variabls
int ch,empid,sal,n;
Employee e; //create class object e
char fname,lname;
do
{
cout<<"enter choice 1.add data 2.display data"; //enter choices
cin>>n;
//start switch case for doing operations
switch(n)
{
case 1: //read employee id,first name,last name and salary and write to file
cout<<"enter employee id:"
cin>>empid;
e.insertion(empid); //call overloaded function to write empid to file
cout<<"enter first name"
cin>>fname;
e.insertion(fname); //call overloaded function to write fname to file
cout<<"enter last name";
cin>>lname;
e.insertion(lname); //call overloaded function to write lname to file
cout<<"enter salary";
cin>>sal;
e.insertion(sal); //call insertion fucntion to write salary to file
break;
case 2: cout<<dispaly details";
e.extraction(); //call function to display details from file
break;
}
cout<<"do you want to continue? 1.yes 2.no";
cin>>ch;
}while(ch=='1'||ch=='2');
}
output
enter choice
1.add data
2.display data
1
enter employee id: 1
enter fisrt name : abc
enter last name : cd
enter salary :1000
do you want to continue? 1.yes 2.no 1
enter choice
1.add data
2.display data
1
enter employee id: 2
enter fisrt name : xyz
enter last name : pm
enter salary :1200
do you want to continue? 1.yes 2.no 1
1
enter choice
1.add data
2.display data
2
1 abc cd 1000
2 xyz pm 1200
do you want to continue? 1.yes 2.no 1
2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.