|
60鱼币
为某公司设计雇员工资发放系统。
每个雇员的基本信息包括:姓名(name),工号(id)。
雇员的收入取决于雇员的类型。该公司共有四类雇员:
周薪雇员(SalariedEmployee):收入=固定周薪。
时薪雇员(HourlyEmployee):若工作40小时以下,收入=小时数*每小时薪水;若工作40小时以上,收入=40*每小时薪水+(小时数-40)*每小时薪水*150%。
佣金雇员(CommissionEmployee):收入=销售量*每个商品的销售佣金
带底薪佣金雇员(BasePlusCommissionEmployee):收入=底薪+销售量*每个商品的销售佣金
要求:建立雇员继承层次,每个类包含计算工资和显示输出的功能,可以计算和显示输出公司雇员(Employee)的每周收入。输出时要显示该类雇员的所有信息。(包括雇员类型、姓名、工号、工资各项明细),写出主函数测试各类。在雇员基本信息中增加雇员的生日(birthDate),并设计日期类(Date)来表示生日。在主函数中创建一个容器用来管理公司各种雇员对象,多态地计算并输出每个雇员的工资。如果雇员的生日在本月,就奖给该雇员100元。同时,在本次工资发放阶段,公司决定奖励带薪佣金雇员,把他们的基本工资提高10%。
设计提示:使用抽象类。
#include <iostream>
#include <string>
using namespace std;
class Salary{
public :
virtual void calculate(void) = 0;
virtual void print_result(void) = 0;
virtual ~Salary(){};
};
class Date{
private :
public :
int year, month, day;
Date(int y, int m, int d) : year(y), month(m), day(d){};
bool isbirthday(void){ return month == 1;}
~Date(){};
};
class SalariedEmployee : public Salary{
private :
int salary = 4*1000;
string Name, Id;
Date *birthday;
public :
SalariedEmployee(string pname, string pid){ Name = pname, Id = pid;}
void calculate(void){
cout << "请输入该周薪员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
if(birthday->isbirthday()) salary += 100;}
void print_result(void){
cout << "该员工为周薪员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(固定周薪为1000元):" << salary << endl;
}
~SalariedEmployee(){delete this;}
};
class HourlyEmployee : public Salary{
private :
int salary = 0;
string Name, Id;
Date *birthday;
int hour;
public :
HourlyEmployee(string pname, string pid)
{
Name = pname, Id = pid;
cout << "请输入该时薪员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
}
void calculate(void){
cout << "请输入该时薪员工" << Name<< "工作时长:";
cin >> hour;
if(hour <= 40)
{
salary = hour * 20;
}
else
{
salary = 800 + (hour - 40) * 30;
}
if(birthday->isbirthday()) salary += 100;
}
void print_result(void){
cout << "该员工为时薪员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(基础时薪为20元):" << salary << endl;
}
~HourlyEmployee(){};
};
class CommissionEmployee : public Salary{
private :
int salary = 0;
string Name, Id;
Date *birthday;
int num;
public :
CommissionEmployee(string pname, string pid)
{
Name = pname, Id = pid;
cout << "请输入该佣金员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
}
void calculate(void){
cout << "请输入该佣金员工" << Name<<"销售数量:";
cin >> num;
salary = num * 5;
if(birthday->isbirthday()) salary += 100;
}
void print_result(void){
cout << "该员工为佣金员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(销售佣金为5元):" << salary << endl;
}
};
class BasePlusCommissionEmployee : public Salary{
private :
float salary;
string Name, Id;
Date *birthday;
int num;
public :
BasePlusCommissionEmployee(string pname, string pid)
{
Name = pname, Id = pid;
cout << "请输入该带底薪佣金员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
}
void calculate(void){
cout << "请输入该带底薪佣金员工" << Name<<"销售数量:";
cin >> num;
salary = num * 3 + 1100;
if(birthday->isbirthday()) salary += 100;
}
void print_result(void){
cout << "该员工为佣金员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(底薪1100元(提高百分之十!),销售佣金为3元):" << salary << endl;
}
};
void fn(Salary &A)
{
A.calculate();
A.print_result();
}
Salary& inputA(string name, string id)
{
return *new SalariedEmployee(name, id);
}
Salary& inputB(string name, string id)
{
return *new HourlyEmployee(name, id);
}
Salary& inputC(string name, string id)
{
return *new CommissionEmployee(name, id);
}
Salary& inputD(string name, string id)
{
return *new BasePlusCommissionEmployee(name, id);
}
int main()
{
cout << "当前为一月份!" << endl;
Salary& A = inputA("张三", "001");fn(A);
Salary& B = inputB("李四", "002");fn(B);
Salary& C = inputC("王五", "003");fn(C);
Salary& D = inputD("赵六", "004");fn(D);
return 0;
}
因为我不知道应该输入些什么,对于这些输入,输出什么才是正确的
只把代码改成这样 #include <iostream>
#include <string>
using namespace std;
class Salary{
public :
virtual void calculate(void) = 0;
virtual void print_result(void) = 0;
virtual ~Salary(){};
};
class Date{
private :
public :
int year, month, day;
Date(): year(0), month(0), day(0) {}
Date(int y, int m, int d) : year(y), month(m), day(d){};
bool isbirthday(void){ return month == 1;}
~Date(){};
};
class SalariedEmployee : public Salary{
private :
int salary = 4*1000;
string Name, Id;
Date *birthday;
public :
SalariedEmployee(string pname, string pid){ Name = pname, Id = pid;}
void calculate(void){
cout << "请输入该周薪员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
this->birthday = new Date;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
if(birthday->isbirthday()) salary += 100;}
void print_result(void){
cout << "该员工为周薪员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(固定周薪为1000元):" << salary << endl;
}
//~SalariedEmployee(){delete this;}
~SalariedEmployee(){delete this->birthday;}
};
class HourlyEmployee : public Salary{
private :
int salary = 0;
string Name, Id;
Date *birthday;
int hour;
public :
HourlyEmployee(string pname, string pid)
{
Name = pname, Id = pid;
cout << "请输入该时薪员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
this->birthday = new Date;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
}
void calculate(void){
cout << "请输入该时薪员工" << Name<< "工作时长:";
cin >> hour;
if(hour <= 40)
{
salary = hour * 20;
}
else
{
salary = 800 + (hour - 40) * 30;
}
if(birthday->isbirthday()) salary += 100;
}
void print_result(void){
cout << "该员工为时薪员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(基础时薪为20元):" << salary << endl;
}
~HourlyEmployee(){delete this->birthday;};
};
class CommissionEmployee : public Salary{
private :
int salary = 0;
string Name, Id;
Date *birthday;
int num;
public :
CommissionEmployee(string pname, string pid)
{
Name = pname, Id = pid;
cout << "请输入该佣金员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
this->birthday = new Date;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
}
void calculate(void){
cout << "请输入该佣金员工" << Name<<"销售数量:";
cin >> num;
salary = num * 5;
if(birthday->isbirthday()) salary += 100;
}
void print_result(void){
cout << "该员工为佣金员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(销售佣金为5元):" << salary << endl;
}
~CommissionEmployee() {delete this->birthday;}
};
class BasePlusCommissionEmployee : public Salary{
private :
float salary;
string Name, Id;
Date *birthday;
int num;
public :
BasePlusCommissionEmployee(string pname, string pid)
{
Name = pname, Id = pid;
cout << "请输入该带底薪佣金员工" << Name<<"的生日" << endl;
cout << "请输入出生年!" << endl;
this->birthday = new Date;
cin >> birthday->year;
cout << "请输入出生月!" << endl;
cin >> birthday->month;
cout << "请输入出生日!" << endl;
cin >> birthday->day;
}
void calculate(void){
cout << "请输入该带底薪佣金员工" << Name<<"销售数量:";
cin >> num;
salary = num * 3 + 1100;
if(birthday->isbirthday()) salary += 100;
}
void print_result(void){
cout << "该员工为佣金员工\n姓名为:" << Name << endl;
cout << "工号为:" << Id << endl;
if(birthday->isbirthday())
cout << "本月为员工生日!奖励100元!" << endl;
cout << "工资为(底薪1100元(提高百分之十!),销售佣金为3元):" << salary << endl;
}
~BasePlusCommissionEmployee() {delete this->birthday;}
};
void fn(Salary &A)
{
A.calculate();
A.print_result();
}
Salary& inputA(string name, string id)
{
return *new SalariedEmployee(name, id);
}
Salary& inputB(string name, string id)
{
return *new HourlyEmployee(name, id);
}
Salary& inputC(string name, string id)
{
return *new CommissionEmployee(name, id);
}
Salary& inputD(string name, string id)
{
return *new BasePlusCommissionEmployee(name, id);
}
int main()
{
cout << "当前为一月份!" << endl;
Salary& A = inputA("张三", "001");fn(A);
Salary& B = inputB("李四", "002");fn(B);
Salary& C = inputC("王五", "003");fn(C);
Salary& D = inputD("赵六", "004");fn(D);
delete &A; delete &B; delete &C; delete &D;
return 0;
}
|
最佳答案
查看完整内容
因为我不知道应该输入些什么,对于这些输入,输出什么才是正确的
只把代码改成这样
|