5月30日实验报告。

先建立一个职工类Employee,包含数据成员name(姓名),ID(编号),以他为基类,派生出经理类Manager和技术人员类Technician,在经理类中增加数据成员salary(月工资),在技术人员类中增加数据成员wage(代表每小时的工资)和hours(工作时长)。在定义类时,所有类中必须包含构造函数、析构函数、修改和获取所有数据成员的函数,以及虚函数计算职工的工资、输出职工的信息。

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

class Employee {
protected:
    string name;
    int ID;
public:
    Employee(string n, int id) : name(n), ID(id) {}
    virtual ~Employee() {}
    void setName(string n) { name = n; }
    void setID(int id) { ID = id; }
    string getName() const { return name; }
    int getID() const { return ID; }
    virtual double getSalary() const = 0;
    virtual void printInfo() const = 0;
};

class Manager : public Employee {
private:
    double salary;
public:
    Manager(string n, int id, double s) : Employee(n, id), salary(s) {}
    ~Manager() {}
    void setSalary(double s) { salary = s; }
    double getSalary() const { return salary; }
    void printInfo() const {
        cout << "Name: " << name << endl;
        cout << "ID: " << ID << endl;
        cout << "Salary: " << salary << endl;
    }
};

class Technician : public Employee {
private:
    double wage;
    int hours;
public:
    Technician(string n, int id, double w, int h) : Employee(n, id), wage(w), hours(h) {}
    ~Technician() {}
    void setWage(double w) { wage = w; }
    void setHours(int h) { hours = h; }
    double getWage() const { return wage; }
    int getHours() const { return hours; }
    double getSalary() const { return wage * hours; }
    void printInfo() const {
        cout << "Name: " << name << endl;
        cout << "ID: " << ID << endl;
        cout << "Wage: " << wage << endl;
        cout << "Hours: " << hours << endl;
    }
};

int main() {
    Manager m("Tom", 1001, 10000);
    Technician t("Jerry", 2001, 50, 200);
    cout << "Manager's salary: " << m.getSalary() << endl;
    cout << "Technician's salary: " << t.getSalary() << endl;
    m.printInfo();
    t.printInfo();
    return 0;
}

修改好了,类都是由我自己创建控制得

img


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

class Employee {
public:
    Employee(string name, int id) : name(name), id(id) {}
    virtual ~Employee() {}
    virtual void calculateSalary() = 0;
    virtual void displayInfo() = 0;

    string getName() const { return name; }
    int getId() const { return id; }
protected:
    string name;
    int id;
};

class Manager : public Employee {
public:
    Manager(string name, int id, double salary) : Employee(name, id), salary(salary) {}
    virtual ~Manager() {}
    virtual void calculateSalary() override { cout << "Manager's salary: " << salary << endl; }
    virtual void displayInfo() override { cout << "Manager's name: " << name << ", ID: " << id << endl; }
    double getSalary() const { return salary; }
private:
    double salary;
};

class Technician : public Employee {
public:
    Technician(string name, int id, double wage, double hours) : Employee(name, id), wage(wage), hours(hours) {}
    virtual ~Technician() {}
    virtual void calculateSalary() override { cout << "Technician's salary: " << wage * hours << endl; }
    virtual void displayInfo() override { cout << "Technician's name: " << name << ", ID: " << id << endl; }
    double getWage() const { return wage; }
    double getHours() const { return hours; }
private:
    double wage;
    double hours;
};

int main() {
    Manager manager("Alice", 1001, 5000);
    Technician technician("Bob", 1002, 50, 80);
    manager.displayInfo();
    manager.calculateSalary();
    technician.displayInfo();
    technician.calculateSalary();
    return 0;
}

好的,我是一名资深的IT专家,我可以为您提供一些解决方案,希望对您有所帮助。

问题描述:

建立一个职工类Employee,包含数据成员name、ID和类派生出来的数据成员salary、wage和hours,同时需要包含构造函数、析构函数、修改和获取所有数据成员的函数以及虚函数计算职工的工资、输出职工的信息。

解决方案:

  1. 创建一个职工类Employee,代码如下:
class Employee:
    def __init__(self, name, id):
        self.name = name
        self.id = id

    def __del__(self):
        print("Employee.__del__ called")
  1. 派生经理类Manager和技术人员类Technician,代码如下:
class Manager(Employee):
    def __init__(self, name, id, salary):
        super().__init__(name, id)
        self.salary = salary

    def __del__(self):
        print("Manager.__del__ called")


class Technician(Employee):
    def __init__(self, name, id,wage, hours):
        super().__init__(name, id)
        self.wage =wage
        self.hours = hours


class Employee:
    def __init__(self, name, id):
        super().__init__(name, id)
        self.salary = None

    def __del__(self):
        if self.salary is not None:
            print("Employee.__del__ called")
            self.salary = None
  1. 定义计算工资和输出信息的函数,代码如下: ```python def calculate_salary(employee): total_salary = employee.salary print("Employee salary: ", employee.salary) print("Manager salary: ", manager.salary) print("Technician salary: ", technician.salary)

def output_employee_的信息(employee): print("Employee name: ", employee.name) print("ID: ", employee.id) print("Salary: ", employee.salary) print("每小时的工资: ", employee.wage) print("工作的小时数: ", employee.hours)

构造函数和析构函数

def init(self, name, id, salary): super().init(name, id) self.salary = salary

def del(self): print("Manager.del called") super().del()

修改和获取数据成员

def modify_salary(employee): salary = employee.salary print("修改后的salary: ", salary)

def get_salary_成员(employee): salary = employee.salary print("修改后的salary: ", salary)

虚函数

def calculate_salary_and_output_信息(employee): total_salary = employee.salary print("Employee salary: ", employee.salary) print("Manager salary: ", manager.salary) print("Technician salary: ", technician.salary)

计算职工的工资

def calculate_salary(employee): total_salary = employee.salary