#include
#include
using namespace std;
class Employee
{
public:
Employee(){}
virtual ~Employee(){}
public:
virtual void print(){}
virtual double earing(){}
public:
string name;
string id;
};
class SalariedEmploy :public Employee
{
public:
SalariedEmploy(string name,string id,double salary)
{
this->name = name;
this->id = id;
this->salary = salary;
}
virtual ~SalariedEmploy(){}
public:
void print()
{
cout << "name: " <
double earing()
{
return salary;
}
private:
double salary;
};
class HourlyEmploy :public Employee
{
public:
HourlyEmploy(string name,string id,double wage,double hours)
{
this->name = name;
this->id = id;
this->wage = wage;
this->hours = hours;
}
virtual ~HourlyEmploy(){}
public:
void print()
{
cout << "name: " <
double earing()
{
return wage*hours;
}
private:
double wage;
double hours;
};
int main()
{
Employee* p[5];
p[0] = new SalariedEmploy("MissA","sd0001",7000);
p[1] = new SalariedEmploy("Apink","sd0002",8000);
p[2] = new SalariedEmploy("Tara","sd0003",9000);
p[3] = new HourlyEmploy("Fx","sd0004",105,60);
p[4] = new HourlyEmploy("Kara","sd0005",100,62);
FILE* fp = fopen("employee","a+");
for(int i=0; i<5; i++)
{
char buf[200];
const char* Name = p[i]->name.c_str();
const char* Id = p[i]->id.c_str();
sprintf(buf,"name:%s id:%s salary:%lf",Name,Id,p[i]->earing());
fwrite(buf,1,sizeof(buf),fp);
}
fseek(fp,0,SEEK_SET);//文件指针指向文件头
for(int j=0; j<5; j++)
{
char vt[200];
fread(vt,1,sizeof(vt),fp);
printf("%s\n",vt);
}
fclose(fp);
}
程序我自己经过编译没有问题
纯手打望采纳!
花了我1个小时希望能帮助到你。