C++面向对象程序设计考题求助详细解答
某公司有两种雇员:正式雇员,他们每周无论工作时间长短,付给固定的薪水;合同工,按小时付薪水;定义三个类:Employee,SalariedEmployee和HourlyE...
某公司有两种雇员:正式雇员,他们每周无论工作时间长短,付给固定的薪水;合同工,按小时付薪水;定义三个类:Employee,SalariedEmployee和HourlyEmployee。Employee是抽象类,存储雇员的姓名(name:string类型)和id(id:string类型);SalariedEmploy需要加上正式雇员的薪水(salary:double类型);HourlyEmploy需要加上合同工的每小时工资(wage:double类型)和每周工作的小时数(hours:double类型);三个类都有printf()函数输出雇员的所有信息,还有一个earings()函数返回雇员的收入。要求:(1)按以上要求写出三个类的实现并简单验证。
(2)实现多态,提示:创建一个指向Employee的指针数组或引用。然后用new创建若干个SalariedEmployee或HourlyEmployee类型的对象来保存数据,最后将指向对象的指针存入数组。
(3)将数组中的指针所指向的所有雇员的信息写入一个磁盘文件:employee,然后再从该文件中读出并输出到屏幕。 展开
(2)实现多态,提示:创建一个指向Employee的指针数组或引用。然后用new创建若干个SalariedEmployee或HourlyEmployee类型的对象来保存数据,最后将指向对象的指针存入数组。
(3)将数组中的指针所指向的所有雇员的信息写入一个磁盘文件:employee,然后再从该文件中读出并输出到屏幕。 展开
展开全部
#include <iostream>
#include <cstdio>
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: " <<name<< " id: " <<id<< " salary: " <<salary<<endl;
}
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: " <<name<< " id: " <<id<< " salary: " <<wage*hours<<endl;
}
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个小时希望能帮助到你。
#include <cstdio>
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: " <<name<< " id: " <<id<< " salary: " <<salary<<endl;
}
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: " <<name<< " id: " <<id<< " salary: " <<wage*hours<<endl;
}
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个小时希望能帮助到你。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询