class SalariedEmployee : public Employee { public: SalariedEmployee(string name, int id, Date birthDate, double salary) : Employee(name, id, birthDate), salary(salary) {} double calculatePay() override { return salary; }
时间: 2023-06-19 09:06:09 浏览: 140
这是一个C++的类定义,定义了一个名为SalariedEmployee的类,继承自Employee类。该类有四个成员变量:name,id,birthDate和salary。其中name、id和birthDate从Employee类中继承而来,而salary是该类自己定义的。构造函数中初始化了这四个成员变量,其中调用了Employee类的构造函数来初始化name、id和birthDate。该类还重写了Employee类中的calculatePay()函数,用于计算薪水,返回值为salary。
相关问题
为某公司设计雇员工资发放系统。 每个雇员的基本信息包括:姓名(name),工号(id)。 雇员的收入取决于雇员的类型。写出主函数测试各类。在雇员基本信息中增加雇员的生日(birthDate),并设计日期类(Date)来表示生日。在主函数中创建一个容器用来管理公司各种雇员对象,多态地计算并输出每个雇员的工资。如果雇员的生日在本月,就奖给该雇员100元。同时,在本次工资发放阶段,公司决定奖励带薪佣金雇员,把他们的基本工资提高10%。
以下是代码实现:
```cpp
#include <iostream>
#include <vector>
using namespace std;
class Date{
private:
int year;
int month;
int day;
public:
Date(int year, int month, int day){
this->year = year;
this->month = month;
this->day = day;
}
int getYear(){
return year;
}
int getMonth(){
return month;
}
int getDay(){
return day;
}
};
class Employee{
protected:
string name;
int id;
Date birthDate;
public:
Employee(string name, int id, Date birthDate){
this->name = name;
this->id = id;
this->birthDate = birthDate;
}
virtual double calcSalary() = 0;
virtual void printInfo(){
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Birth Date: " << birthDate.getYear() << "-" << birthDate.getMonth() << "-" << birthDate.getDay() << endl;
}
};
class SalariedEmployee : public Employee{
private:
double salary;
public:
SalariedEmployee(string name, int id, Date birthDate, double salary) : Employee(name, id, birthDate){
this->salary = salary;
}
double calcSalary(){
return salary;
}
void printInfo(){
Employee::printInfo();
cout << "Salary: " << salary << endl;
}
};
class HourlyEmployee : public Employee{
private:
double wage;
double hours;
public:
HourlyEmployee(string name, int id, Date birthDate, double wage, double hours) : Employee(name, id, birthDate){
this->wage = wage;
this->hours = hours;
}
double calcSalary(){
return wage * hours;
}
void printInfo(){
Employee::printInfo();
cout << "Wage: " << wage << endl;
cout << "Hours: " << hours << endl;
}
};
class CommissionEmployee : public Employee{
private:
double sales;
double rate;
public:
CommissionEmployee(string name, int id, Date birthDate, double sales, double rate) : Employee(name, id, birthDate){
this->sales = sales;
this->rate = rate;
}
double calcSalary(){
return sales * rate;
}
void printInfo(){
Employee::printInfo();
cout << "Sales: " << sales << endl;
cout << "Rate: " << rate << endl;
}
};
class BasePlusCommissionEmployee : public CommissionEmployee{
private:
double baseSalary;
public:
BasePlusCommissionEmployee(string name, int id, Date birthDate, double sales, double rate, double baseSalary) : CommissionEmployee(name, id, birthDate, sales, rate){
this->baseSalary = baseSalary;
}
double calcSalary(){
return baseSalary + CommissionEmployee::calcSalary();
}
void printInfo(){
CommissionEmployee::printInfo();
cout << "Base Salary: " << baseSalary << endl;
}
};
int main(){
vector<Employee*> employees;
Date d1(1990, 10, 15);
employees.push_back(new SalariedEmployee("John", 1, d1, 5000));
Date d2(1985, 5, 20);
employees.push_back(new HourlyEmployee("Mike", 2, d2, 20, 160));
Date d3(1992, 11, 30);
employees.push_back(new CommissionEmployee("Lucy", 3, d3, 10000, 0.05));
Date d4(1988, 7, 25);
employees.push_back(new BasePlusCommissionEmployee("Mary", 4, d4, 15000, 0.03, 5000));
int currentMonth = 11;
for(int i=0; i<employees.size(); i++){
employees[i]->printInfo();
double salary = employees[i]->calcSalary();
if(employees[i]->birthDate.getMonth() == currentMonth){
salary += 100;
}
if(dynamic_cast<BasePlusCommissionEmployee*>(employees[i]) != nullptr){
salary *= 1.1;
}
cout << "Salary: " << salary << endl;
cout << endl;
}
return 0;
}
```
其中,我们定义了一个日期类 Date,表示员工的生日。然后我们定义了一个 Employee 类作为父类,包含了姓名、工号、生日等基本信息,并定义了一个纯虚函数 calcSalary() 用于计算工资。然后我们分别定义了四个派生类 SalariedEmployee、HourlyEmployee、CommissionEmployee 和 BasePlusCommissionEmployee,分别表示计时工、计件工、带薪佣金工和带底薪佣金工,它们分别实现了 calcSalary() 函数,计算各自对应的工资。在主函数中,我们创建了一个容器来管理各种雇员对象,并多态地计算并输出每个雇员的工资。如果雇员的生日在本月,就奖给该雇员100元。同时,在本次工资发放阶段,公司决定奖励带薪佣金雇员,把他们的基本工资提高10%。
C++代码编写:1.编写一个Date类,有checkMonth和checkDay函数以检查月份和日期的合法性,有判断是否为闰年的函数,设计定义set类函数,用以设定年,月份和日期,设计定义多个get函数用以获取年、月和日。 2. 创建一个抽象类employee类,该类中有姓名(sting 类型),ID(sting 类型)和出生日期(Date类型)。定义构造函数初始化每个成员;定义changeName()函数用来修改姓名,定义Display()函数用来显示每个员工的基本信息。纯虚函数为发放工资函数。 3.创建由Employee类派生出的其它四个员工类Salaried Employee,Commission Employee,Hourly Employee,以及由Commission Employee派生出的BasePlus Commission Employee。编写他们的新增成员、构造函数等,他们都有一个计算工资的函数。
1. Date类的代码如下:
```C++
class Date {
private:
int year;
int month;
int day;
bool isLeapYear() {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
public:
Date(int y = 0, int m = 0, int d = 0) {
year = y;
month = m;
day = d;
}
bool checkMonth() {
return month >= 1 && month <= 12;
}
bool checkDay() {
if (month == 2) {
if (isLeapYear()) {
return day >= 1 && day <= 29;
} else {
return day >= 1 && day <= 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return day >= 1 && day <= 30;
} else {
return day >= 1 && day <= 31;
}
}
void setYear(int y) {
year = y;
}
void setMonth(int m) {
month = m;
}
void setDay(int d) {
day = d;
}
int getYear() {
return year;
}
int getMonth() {
return month;
}
int getDay() {
return day;
}
};
```
2. Employee类的代码如下:
```C++
class Employee {
private:
string name;
string id;
Date birthDate;
public:
Employee(string n, string i, Date bd) {
name = n;
id = i;
birthDate = bd;
}
void changeName(string n) {
name = n;
}
void Display() {
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Birth Date: " << birthDate.getYear() << "-" << birthDate.getMonth() << "-" << birthDate.getDay() << endl;
}
virtual double pay() = 0;
};
```
注意到这里声明了一个纯虚函数pay(),表示需要在派生出的具体类中实现。
3. SalariedEmployee类、CommissionEmployee类、HourlyEmployee类和BasePlusCommissionEmployee类的代码如下:
```C++
class SalariedEmployee : public Employee {
private:
double salary;
public:
SalariedEmployee(string n, string i, Date bd, double s) : Employee(n, i, bd) {
salary = s;
}
double pay() {
return salary;
}
};
class CommissionEmployee : public Employee {
private:
double sales;
double rate;
public:
CommissionEmployee(string n, string i, Date bd, double s, double r) : Employee(n, i, bd) {
sales = s;
rate = r;
}
double pay() {
return sales * rate;
}
};
class HourlyEmployee : public Employee {
private:
double wage;
double hours;
public:
HourlyEmployee(string n, string i, Date bd, double w, double h) : Employee(n, i, bd) {
wage = w;
hours = h;
}
double pay() {
if (hours <= 40) {
return wage * hours;
} else {
return wage * 40 + wage * 1.5 * (hours - 40);
}
}
};
class BasePlusCommissionEmployee : public CommissionEmployee {
private:
double baseSalary;
public:
BasePlusCommissionEmployee(string n, string i, Date bd, double s, double r, double b) : CommissionEmployee(n, i, bd, s, r) {
baseSalary = b;
}
double pay() {
return CommissionEmployee::pay() + baseSalary;
}
};
```
这里可以看到,派生类的构造函数中都直接调用了基类的构造函数,使用了冒号“:”语法。同时,派生类中的pay()函数都实现了基类中声明的纯虚函数。
阅读全文