c++为某公司设计雇员工资发放系统

利用c++为某公司设计雇员工资发放系统,要求可以在VS和CB上完美运行,详细的设计要求放在下面图片中,希望能够得到解答

img

img

运行结果(雇员信息直接在代码中写死了,也可以改成手动输入):

img

代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

//定义当前月份,也可以通过系统函数获取真实的系统时间,这里为了简便,直接定义为一个固定值
#define CURRENTMONTH 5  


class Date
{
private:
    int mYear, mMon, mDay; //年月日
public:
    Date() { mYear = 1997; mMon = 1; mDay = 1; }
    Date(int y, int m, int d) { mYear = y; mMon = m; mDay = d; }
    void setYear(int y) { mYear = y; } //设置年
    void setMonth(int m) { mMon = m; } //设置月
    void setDay(int d) { mDay = d; } //设置日
    int getYear() { return mYear; } //获取年份
    int getMonth() { return mMon; } //获取月份
    int getDay() { return mDay; }; //获取日
};
//定义雇员类
class Employee
{
protected:
    string name; //姓名
    string id; //工号
    //float salary; //薪水
    Date birthday; //生日
public:
    Employee(string _id, string _name) :id(_id), name(_name) {  }

    void setName(string _name) { name = _name; }
    void setId(string _id) { id = _id; }
    
    void setBirthday(Date d) { birthday.setYear(d.getYear()); birthday.setMonth(d.getMonth()); birthday.setDay(d.getDay()); }

    string getNmae() { return name; }
    string getId() { return id; }
    
    Date getBirthday() { return birthday; }


    virtual float caculateSalary() = 0; //纯虚函数,计算工资
    virtual void show() = 0; //纯虚函数,显示雇员信息
};

//定义周薪雇员
class SalariedEmployee :public Employee
{
private:
    float salary;  //固定周薪

public:
    SalariedEmployee(string _id, string _name) :Employee(_id, _name) { }
    SalariedEmployee(string _id, string _name, float _salary) :Employee(_id, _name) { salary = _salary; }
    void setSalary(float s) { salary = s; }
    float getSalary() { return salary; }
    float caculateSalary()//实现纯虚函数,计算工资
    {
            return salary; //周薪员工为固定薪资
    }
    void show()  //实现纯虚函数,显示雇员信息
    {
        cout << "工号:" << id << ",姓名:" << name << ",员工类型:周薪雇员,薪资:" << fixed << setprecision(2) << caculateSalary() << ",生日福利:";
        if (birthday.getMonth() == CURRENTMONTH)
            cout << "100,总计:" << fixed << setprecision(2) << (caculateSalary()+100) << endl;
        else
            cout << "0,总计:" << fixed << setprecision(2) << (caculateSalary() + 0) << endl;
    }
};

//定义时薪员工
class HourlyEmployee : public Employee
{
private:
    int hour; //工作时长
    float hourSalary; //时薪
public:
    HourlyEmployee(string _id, string _name) :Employee(_id, _name) { hour = 0; }
    void setWorkHour(int h) { hour = h; } //设置工作时长
    int getWorkHour() { return hour; } //获取工作时长
    void setHourSalary(float s) { hourSalary = s; } //设置时薪
    float getHourSalary() { return hourSalary; } //获取时薪
    float caculateSalary()//实现纯虚函数,计算工资
    {
        float salary;
        if (hour < 40)
            salary = hour * hourSalary;
        else
            salary = 40 * hourSalary + (hour - 40) * hourSalary * 1.5;
        return salary; 
    }
    void show()  //实现纯虚函数,显示雇员信息
    {
        cout << "工号:" << id << ",姓名:" << name << ",员工类型:时薪雇员,时薪:" << fixed << setprecision(2) << hourSalary << ",工时:"<< hour << ",薪资:" << caculateSalary() << ",生日福利:";
        if (birthday.getMonth() == CURRENTMONTH)
            cout << "100,总计:" << fixed << setprecision(2) << (caculateSalary() + 100) << endl;
        else
            cout << "0,总计:" << fixed << setprecision(2) << (caculateSalary() + 0) << endl;
    }
};

//佣金雇员
class CommissionEmployee :public Employee
{
protected:
    int count; //销售量
    float stufPrice; //货品佣金
public:
    CommissionEmployee(string _id, string _name) :Employee(_id, _name) { count = 0; stufPrice = 0; }

    void setCount(int n) { count = n; } //设置销售数量
    int getCount() { return count; } //获取销售数量
    void setStufPrice(float p) { stufPrice = p; } //设置商品佣金
    float getStufPrice() { return stufPrice; } //获取商品佣金

    float caculateSalary()//实现纯虚函数,计算工资
    {
        float salary = count * stufPrice;
        return salary;
    }
    void show()  //实现纯虚函数,显示雇员信息
    {
        cout << "工号:" << id << ",姓名:" << name << ",员工类型:佣金雇员,商品佣金:" << fixed << setprecision(2) << stufPrice << ",销售数量:" << count << ",薪资:" << caculateSalary() << ",生日福利:";
        if (birthday.getMonth() == CURRENTMONTH)
            cout << "100,总计:" << fixed << setprecision(2) << (caculateSalary() + 100) << endl;
        else
            cout << "0,总计:" << fixed << setprecision(2) << (caculateSalary() + 0) << endl;
    }

};

//带底薪佣金雇员
class BasePlusCommission : public CommissionEmployee
{
private:
    float baseSalary; //底薪
    float percent; //底薪提升比例
public:
    BasePlusCommission(string _id, string _name) :CommissionEmployee(_id, _name) { baseSalary = 0; percent = 0; }
    void setBaseSalary(float bs) { baseSalary = bs; } //设置底薪
    float getBaseSalary() { return baseSalary; } //获取底薪
    void setPercent(float ps) { percent = ps; } //设置底薪比例
    float getPercent() { return percent; }; //获取底薪比例

    float caculateSalary()//实现纯虚函数,计算工资
    {
        float salary = count * stufPrice + baseSalary * (1 + percent);
        return salary;
    }
    void show()  //实现纯虚函数,显示雇员信息
    {
        cout << "工号:" << id << ",姓名:" << name << ",员工类型:带底薪佣金雇员,底薪" << fixed << setprecision(2) << baseSalary <<",底薪提升比例:" << percent*100 << "%,商品佣金:" << stufPrice << ",销售数量:" << count << ",薪资:" << caculateSalary() << ",生日福利:";
        if (birthday.getMonth() == CURRENTMONTH)
            cout << "100,总计:" << fixed << setprecision(2) << (caculateSalary() + 100) << endl;
        else
            cout << "0,总计:" << fixed << setprecision(2) << (caculateSalary() + 0) << endl;
    }
};


int main()
{
    Employee* allEmp[10] = { 0 };
    //定义周薪雇员对象
    allEmp[0] = new SalariedEmployee("1001","张三");
    allEmp[0]->setBirthday(Date(2000, 3, 4));
    ((SalariedEmployee*)allEmp[0])->setSalary(2000);
    //定义时薪员工对象
    allEmp[1] = new HourlyEmployee("1002", "李四");
    ((HourlyEmployee*)allEmp[1])->setBirthday(Date(2003, 5, 20));
    ((HourlyEmployee*)allEmp[1])->setHourSalary(200); //设置时薪
    ((HourlyEmployee*)allEmp[1])->setWorkHour(56); //设置工作时长
    //定义佣金雇员对象
    allEmp[2] = new CommissionEmployee("1003", "王五");
    ((CommissionEmployee*)allEmp[2])->setBirthday(Date(2003, 6, 20));
    ((CommissionEmployee*)allEmp[2])->setCount(1000);
    ((CommissionEmployee*)allEmp[2])->setStufPrice(2.5); //设置商品佣金
    //定义带底薪佣金雇员对象
    allEmp[3] = new BasePlusCommission("1004", "李六");
    ((BasePlusCommission*)allEmp[3])->setBirthday(Date(2003, 8, 20));
    ((BasePlusCommission*)allEmp[3])->setBaseSalary(2000); //设置底薪
    ((BasePlusCommission*)allEmp[3])->setCount(200); //设置销售数量
    ((BasePlusCommission*)allEmp[3])->setStufPrice(3.2); //设置商品佣金
    ((BasePlusCommission*)allEmp[3])->setPercent(0.1); //提升10%底薪

    //显示全部信息
    for (int i = 0; i < 4; i++)
        allEmp[i]->show();
    return 0;
}

引用chatGPT作答,以下是实现需求的C++代码:

#include <iostream>
#include <vector>

// 日期类
class Date {
public:
    Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
    int getYear() const { return year_; }
    int getMonth() const { return month_; }
    int getDay() const { return day_; }
private:
    int year_, month_, day_;
};

// 员工抽象类
class Employee {
public:
    Employee(std::string name, std::string id, Date birthDate)
        : name_(name), id_(id), birthDate_(birthDate) {}
    virtual ~Employee() {}
    virtual double calculatePay() const = 0;
    virtual void display() const {
        std::cout << "Type: " << getType() << std::endl;
        std::cout << "Name: " << name_ << std::endl;
        std::cout << "ID: " << id_ << std::endl;
        std::cout << "Birth Date: " << birthDate_.getYear() << "-" 
                  << birthDate_.getMonth() << "-" << birthDate_.getDay() << std::endl;
        std::cout << "Pay: " << calculatePay() << std::endl;
    }
    virtual std::string getType() const = 0;
protected:
    std::string name_;
    std::string id_;
    Date birthDate_;
};

// 周薪雇员类
class SalariedEmployee : public Employee {
public:
    SalariedEmployee(std::string name, std::string id, Date birthDate, double weeklySalary)
        : Employee(name, id, birthDate), weeklySalary_(weeklySalary) {}
    double calculatePay() const override {
        return weeklySalary_;
    }
    std::string getType() const override { return "Salaried Employee"; }
private:
    double weeklySalary_;
};

// 时薪雇员类
class HourlyEmployee : public Employee {
public:
    HourlyEmployee(std::string name, std::string id, Date birthDate, double hourlySalary, double hours)
        : Employee(name, id, birthDate), hourlySalary_(hourlySalary), hours_(hours) {}
    double calculatePay() const override {
        if (hours_ <= 40) {
            return hours_ * hourlySalary_;
        } else {
            return 40 * hourlySalary_ + (hours_ - 40) * hourlySalary_ * 1.5;
        }
    }
    std::string getType() const override { return "Hourly Employee"; }
private:
    double hourlySalary_;
    double hours_;
};

// 佣金雇员类
class CommissionEmployee : public Employee {
public:
    CommissionEmployee(std::string name, std::string id, Date birthDate, double commission, double sales)
        : Employee(name, id, birthDate), commission_(commission), sales_(sales) {}
    double calculatePay() const override {
        return commission_ * sales_;
    }
    std::string getType() const override { return "Commission Employee"; }
private:
    double commission_;
    double sales_;
};

// 带底薪佣金雇员类
class BasePlusCommissionEmployee : public CommissionEmployee {
public:
    BasePlusCommissionEmployee(std::string name, std::string id, Date birthDate, double commission, double sales, double baseSalary)
        : CommissionEmployee(name, id, birthDate, commission, sales), baseSalary_(baseSalary) {}
    double calculatePay() const
override {
return baseSalary_ + CommissionEmployee::calculatePay();
}
void display() const override {
std::cout << "Employee Type: Base Plus Commission Employee\n";
Employee::display();
std::cout << "Base Salary: " << baseSalary_ << "\n";
std::cout << "Commission: " << commission_ << "\n";
std::cout << "Sales: " << sales_ << "\n";
std::cout << "Total Pay: " << calculatePay() << "\n";
}
private:
double baseSalary_;
};

int main() {
// 创建一个容器用来管理公司各种雇员对象
std::vector<std::shared_ptr<Employee>> employees;
// 添加不同类型的雇员对象
employees.push_back(std::make_shared<SalariedEmployee>("John Doe", "001", Date(1990, 10, 15), 5000));
employees.push_back(std::make_shared<HourlyEmployee>("Jane Smith", "002", Date(1992, 2, 29), 20, 45));
employees.push_back(std::make_shared<CommissionEmployee>("Bob Johnson", "003", Date(1985, 5, 6), 0.1, 10000));
employees.push_back(std::make_shared<BasePlusCommissionEmployee>("Mike Williams", "004", Date(1995, 12, 25), 0.15, 8000, 4000));

// 循环计算并输出每个雇员的工资
for (const auto& emp : employees) {
    emp->display();
    // 如果雇员的生日在本月,就奖给该雇员100元
    if (emp->getBirthDate().getMonth() == Date::getCurrentDate().getMonth()) {
        std::cout << "Happy Birthday! You have received a bonus of 100 yuan.\n";
    }
    // 如果是带薪佣金雇员,把基本工资提高10%
    auto bpcEmp = std::dynamic_pointer_cast<BasePlusCommissionEmployee>(emp);
    if (bpcEmp != nullptr) {
        bpcEmp->setBaseSalary(bpcEmp->getBaseSalary() * 1.1);
        std::cout << "Base salary has been increased by 10%.\n";
    }
    std::cout << "\n";
}

return 0;
}

以下是设计的代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

class Date {
public:
    Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
    int year() const { return year_; }
    int month() const { return month_; }
    int day() const { return day_; }
private:
    int year_;
    int month_;
    int day_;
};

class Employee {
public:
    Employee(const string& name, const string& id, const Date& birthDate) : name_(name), id_(id), birthDate_(birthDate) {}
    virtual double getSalary() const = 0;
    virtual void display() const {
        cout << "Name: " << name_ << endl;
        cout << "ID: " << id_ << endl;
        cout << "Birth Date: " << birthDate_.year() << "-" << birthDate_.month() << "-" << birthDate_.day() << endl;
    }
protected:
    string name_;
    string id_;
    Date birthDate_;
};

class SalariedEmployee : public Employee {
public:
    SalariedEmployee(const string& name, const string& id, const Date& birthDate, double weeklySalary) : Employee(name, id, birthDate), weeklySalary_(weeklySalary) {}
    double getSalary() const override {
        return weeklySalary_;
    }
    void display() const override {
        cout << "Type: Salaried Employee" << endl;
        Employee::display();
        cout << "Weekly Salary: " << weeklySalary_ << endl;
    }
private:
    double weeklySalary_;
};

class HourlyEmployee : public Employee {
public:
    HourlyEmployee(const string& name, const string& id, const Date& birthDate, double hourlySalary, double hours) : Employee(name, id, birthDate), hourlySalary_(hourlySalary), hours_(hours) {}
    double getSalary() const override {
        if (hours_ <= 40) {
            return hours_ * hourlySalary_;
        }
        else {
            return 40 * hourlySalary_ + (hours_ - 40) * hourlySalary_ * 1.5;
        }
    }
    void display() const override {
        cout << "Type: Hourly Employee" << endl;
        Employee::display();
        cout << "Hourly Salary: " << hourlySalary_ << endl;
        cout << "Hours: " << hours_ << endl;
    }
private:
    double hourlySalary_;
    double hours_;
};

class CommissionEmployee : public Employee {
public:
    CommissionEmployee(const string& name, const string& id, const Date& birthDate, double commissionRate, double sales) : Employee(name, id, birthDate), commissionRate_(commissionRate), sales_(sales) {}
    double getSalary() const override {
        return commissionRate_ * sales_;
    }
    void display() const override {
        cout << "Type: Commission Employee" << endl;
        Employee::display();
        cout << "Commission Rate: " << commissionRate_ << endl;
        cout << "Sales: " << sales_ << endl;
    }
private:
    double commissionRate_;
    double sales_;
};

class BasePlusCommissionEmployee : public CommissionEmployee {
public:
    BasePlusCommissionEmployee(const string& name, const string& id, const Date& birthDate, double commissionRate, double sales, double baseSalary) : CommissionEmployee(name, id, birthDate, commissionRate, sales), baseSalary_(baseSalary) {}
    double getSalary() const override {
        return baseSalary_ + CommissionEmployee::getSalary();
    }
    void display() const override {
        cout << "Type: Base Plus Commission Employee" << endl;
        Employee::display();
        cout << "Commission Rate: " << CommissionEmployee::commissionRate_ << endl;
        cout << "Sales: " << CommissionEmployee::sales_ << endl;
        cout << "Base Salary: " << baseSalary_ << endl;
    }
    void increaseBaseSalary(double percent) {
        baseSalary_ *= (1 + percent);
    }
private:
    double baseSalary_;
};

int main() {
    vector<Employee*> employees;
    employees.push_back(new SalariedEmployee("John", "001", Date(1990, 1, 1), 1000));
    employees.push_back(new HourlyEmployee("Mary", "002", Date(1991, 2, 2), 10, 35));
    employees.push_back(new CommissionEmployee("Tom", "003", Date(1992, 3, 3), 0.1, 5000));
    employees.push_back(new BasePlusCommissionEmployee("Jerry", "004", Date(1993, 4, 4), 0.1, 10000, 2000));

    for (auto e : employees) {
        e->display();
        double salary = e->getSalary();
        cout << "Salary: " << salary << endl;
        if (e->birthDate_.month() == 9) {
            cout << "Happy Birthday! You get a bonus of 100 yuan." << endl;
            salary += 100;
        }
        if (dynamic_cast<BasePlusCommissionEmployee*>(e)) {
            BasePlusCommissionEmployee* b = dynamic_cast<BasePlusCommissionEmployee*>(e);
            b->increaseBaseSalary(0.1);
            cout << "Base Salary Increased by 10%: " << b->getSalary() - b->CommissionEmployee::getSalary() << endl;
        }
        cout << endl;
    }

    for (auto e : employees) {
        delete e;
    }

    return 0;
}

在这个设计中,我们使用了抽象类 Employee 来表示所有雇员,其中包含了姓名、工号和生日等基本信息,以及计算工资和显示输出的虚函数。然后,我们派生出了四个具体的雇员类,分别实现了计算工资和显示输出的功能。在主函数中,我们创建了一个容器来管理所有雇员对象,多态地计算并输出每个雇员的工资。如果雇员的生日在本月,就奖给该雇员100元。同时,在本次工资发放阶段,公司决定奖励带薪佣金雇员,把他们的基本工资提高10%。