Code:
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include "employee.h"
using namespace std;
int main()
{
Direct D1("Harry Johnson", 25,45365,800,10), D2;
Indirect ID3("Larry King", 60, 34546,20,40), ID4;
Employee *STAFF[20]; cin>>D2; cin>>ID4; system("cls");
STAFF[0]=&D1; STAFF[1]=&D2;
STAFF[2]=&ID3; STAFF[3]=&ID4;
int count=4;
ifstream direct; ifstream indirect;
direct.open("direct.dat", ios::in);
if(direct.fail())
{
cerr<<"direct.dat cannot be opened\n";
exit(1);
}
indirect.open("indirect.dat", ios::in);
if(indirect.fail())
{
cerr<<"indirect.dat cannot be opened\n";
exit(1);
}
while(!direct.eof())
{
Direct *temp=new Direct;
direct>>*temp;
STAFF[count]=temp;
count++;
}
direct.close();
count--;
while(!indirect.eof())
{
Indirect *temp=new Indirect;
indirect>>*temp;
STAFF[count]=temp;
count++;
}
indirect.close();
count--;
cout<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
<<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
cout<<"======================================================"
<<"======================\n";
for(int loop=0; loop<4; loop++)
{
STAFF[loop]->cal_pay();
STAFF[loop]->display();
}
for(loop=4; loop<count;loop++)
{
if(loop%8==0)
{
cout<<endl; system("pause"); system("cls");
cout<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
<<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
cout<<"======================================================"
<<"======================\n";
}
STAFF[loop]->cal_pay();
STAFF[loop]->display();
}
system("pause"); system("cls");
for(loop=1; loop<count; loop++)
{
for(int number=0; number<count-1; number++)
{
if(strcmp(STAFF[number]->getname(),
STAFF[number+1]->getname())>0)
{
Employee *temp=STAFF[number];
STAFF[number]=STAFF[number+1];
STAFF[number+1]=temp;
}
}
}
cout<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
<<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
cout<<"======================================================"
<<"======================\n";
for(loop=0; loop<count; loop++)
{
STAFF[loop]->display();
}
ofstream employee; employee.open("employee.dat", ios::out);
if(employee.fail())
{
cerr<<"employee.dat cannot be opened\n";
exit(1);
}
employee<<"Type"<<setw(11)<<"Name"<<setw(22)<<"Age"<<setw(5)<<"Id"<<setw(17)<<"Basic/Hours"
<<setw(11)<<"Cpf/Rate"<<setw(6)<<"Pay"<<endl;
employee<<"======================================================"
<<"======================\n";
for(loop=0; loop<count; loop++)
{
// how to solve this?
employee<<STAFF[loop]<<endl;
}
cout<<endl;
return 0;
}
// the employee.h
#include <iostream>
using namespace std;
#ifndef _EMPLOYEE_H_
#define _EMPLOYEE_H_
class Employee
{
protected:
char name[20]; float age; int EID; float pay;
public:
Employee();
Employee(char *name, float ag, int ID, float p);
char *getname();
virtual void cal_pay()=0;
virtual void display()=0;
};
class Direct:public Employee
{
float basic; float cpf_rate;
public:
Direct();
Direct(char *name, float ag, int ID, float bas, float rat);
void cal_pay();
void display();
void read_keyboard(istream & input);
void read_file(ifstream & input);
void write_file(ofstream & output);
};
istream & operator>>(istream & input, Direct & person);
ifstream & operator>>(ifstream & input, Direct & person);
ofstream & operator<<(ofstream & output, Direct & person);
class Indirect:public Employee
{
float hours; float rate;
public:
Indirect();
Indirect(char *name, float ag, int ID, float hrs, float rat);
void cal_pay();
void display();
void read_keyboard(istream & input);
void read_file(ifstream & input);
void write_file(ofstream & output);
};
istream & operator>>(istream & input, Indirect & person);
ifstream & operator>>(ifstream & input, Indirect & person);
ofstream & operator<<(ofstream & output, Indirect & person);
#endif
// the employee.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include "employee.h"
using namespace std;
Employee::Employee()
{
pay=0;
}
Employee::Employee(char *nam, float ag, int ID, float p)
{
strcpy(name,nam); age=ag; EID=ID; pay=p;
}
char *Employee::getname()
{
return name;
}
void Employee::cal_pay()
{
}
void Employee::display()
{
}
////////////////////////////////////////////////////////////////
Direct::Direct()
{
basic=0;
cpf_rate=0;
}
Direct::Direct(char *nam, float ag, int ID, float bas, float rat)
{
strcpy(name,nam); age=ag; EID=ID;
basic=bas;
cpf_rate=rat;
}
void Direct::cal_pay()
{
pay=basic*(1-(cpf_rate/100));
}
void Direct::display()
{
int length=strlen(name);
cout<<"Direct"<<setw(5+length)<<name;
char number[10];
length=23-length+strlen(itoa(age,number,10));
cout<<setw(length)<<age<<setw(9)<<EID; length=3+strlen(itoa(basic,number,10));
cout<<setw(length)<<basic; length=14-strlen(itoa(basic,number,10))
+strlen(itoa(cpf_rate,number,10));
cout<<setw(length)<<cpf_rate; length=11-strlen(itoa(cpf_rate,number,10))
+strlen(itoa(pay,number,10));
cout<<setw(length)<<pay<<endl;
}
void Direct::read_keyboard(istream & input)
{
cout<<"Please enter the name of the employee\n";
input.getline(name,255,'\n');
cout<<"Please enter the age of the employee\n";
input>>age;
cout<<"Please enter the id of the employee\n";
input>>EID;
cout<<"Please enter the basic pay of the employee\n";
input>>basic;
cout<<"Please enter the cpf rate of the employee\n";
input>>cpf_rate;
cin.ignore(255,'\n');
}
void Direct::read_file(ifstream & input)
{
input.getline(name,255,'*');
input>>age>>EID>>basic>>cpf_rate; input.ignore(255,'\n');
}
void Direct::write_file(ofstream & output)
{
int length=strlen(name);
output<<"Direct"<<setw(5+length)<<name;
char number[10];
length=23-length+strlen(itoa(age,number,10));
output<<setw(length)<<age<<setw(9)<<EID; length=3+strlen(itoa(basic,number,10));
output<<setw(length)<<basic; length=14-strlen(itoa(basic,number,10))
+strlen(itoa(cpf_rate,number,10));
output<<setw(length)<<cpf_rate; length=11-strlen(itoa(cpf_rate,number,10))
+strlen(itoa(pay,number,10));
output<<setw(length)<<pay<<endl;
}
istream & operator>>(istream & input, Direct & person)
{
person.read_keyboard(input);
return input;
}
ifstream & operator>>(ifstream & input, Direct & person)
{
person.read_file(input);
return input;
}
ofstream & operator<<(ofstream & output, Direct & person)
{
person.write_file(output);
return output;
}
/////////////////////////////////////////////////////////////////
Indirect::Indirect()
{
hours=0;
rate=0;
}
Indirect::Indirect(char *nam, float ag, int ID, float hrs,
float rat)
{
strcpy(name,nam); age=ag; EID=ID;
hours=hrs;
rate=rat;
}
void Indirect::cal_pay()
{
if(hours>40)
{
int extra_hours=hours-40;
pay=40*rate+extra_hours*1.5*rate;
}
else
pay=hours*rate;
}
void Indirect::display()
{
int length=strlen(name); char number[10]; length=length+3;
cout<<"Indirect"<<setw(length)<<name; length=23-strlen(name)
+strlen(itoa(age,number,10));
cout<<setw(length)<<age;
cout<<setw(9)<<EID; length=3+strlen(itoa(hours,number,10));
cout<<setw(length)<<hours; length=14-strlen(itoa(hours,number,10))
+strlen(itoa(rate,number,10));
cout<<setw(length)<<rate; length=11-strlen(itoa(rate,number,10))
+strlen(itoa(pay,number,10));
cout<<setw(length)<<pay<<endl;
}
void Indirect::read_keyboard(istream & input)
{
cout<<"Please enter the name of the employee\n";
input.getline(name,255,'\n');
cout<<"Please enter the age of the employee\n";
input>>age;
cout<<"Please enter the id of the employee\n";
input>>EID;
cout<<"Please enter the number of hours worked for the employee\n";
input>>hours;
cout<<"Please enter the rate of the employee\n";
input>>rate;
cin.ignore(255,'\n');
}
void Indirect::read_file(ifstream & input)
{
input.getline(name,255,'*'); input>>age>>EID
>>hours>>rate; input.ignore(1,EOF);
}
void Indirect::write_file(ofstream & output)
{
int length=strlen(name); char number[10]; length=length+3;
cout<<"Indirect"<<setw(length)<<name; length=23-strlen(name)
+strlen(itoa(age,number,10));
cout<<setw(length)<<age;
cout<<setw(9)<<EID; length=3+strlen(itoa(hours,number,10));
cout<<setw(length)<<hours; length=14-strlen(itoa(hours,number,10))
+strlen(itoa(rate,number,10));
cout<<setw(length)<<rate; length=11-strlen(itoa(rate,number,10))
+strlen(itoa(pay,number,10));
cout<<setw(length)<<pay<<endl;
}
istream & operator>>(istream & input, Indirect & person)
{
person.read_keyboard(input);
return input;
}
ifstream & operator>>(ifstream & input, Indirect & person)
{
person.read_file(input);
return input;
}
ofstream & operator<<(ofstream & output, Indirect & person)
{
person.write_file(output);
return output;
}
Here is the output of employee.dat