1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| #include <bits\stdc++.h> using namespace std;
class Employee { public: Employee(const long k, const char* str) { number = k; strcpy_s(name, 20, str); } virtual ~Employee() { name[0] = '\0'; } const char* getName() const { return name; } const long getNumber() const { return number; } virtual double earnings() const = 0; virtual void print() const { cout << number << setw(20) << name; } Employee* next;
protected: long number; char name[20]; };
class Manager : public Employee { public: Manager(const long, const char*, double = 0.0); ~Manager() {} void setMonthlySalary(double); virtual double earnings() const; virtual void print() const;
private: double monthlySalary; }; Manager::Manager(const long k, const char* str, double sal) : Employee(k, str) { setMonthlySalary(sal); } void Manager::setMonthlySalary(double sal) { monthlySalary = sal > 0 ? sal : 0; } double Manager::earnings() const { return monthlySalary; } void Manager::print() const { Employee::print(); cout << setw(16) << "Manager\n"; cout << "\tearned $" << monthlySalary << endl; }
class HourlyWorker : public Employee { public: HourlyWorker(const long, const char*, double = 0.0, int = 0); ~HourlyWorker() {} void setWage(double); void setHours(int); virtual double earnings() const; virtual void print() const;
private: double wage; double hours; }; HourlyWorker::HourlyWorker(const long k, const char* str, double w, int h) : Employee(k, str) { setWage(w); setHours(h); } void HourlyWorker::setWage(double w) { wage = w > 0 ? w : 0; } void HourlyWorker::setHours(int h) { hours = h >= 0 && h <= 16 * 31 ? h : 0; } double HourlyWorker::earnings() const { if (hours <= 8 * 22) return wage * hours; else return wage * (8 * 22) + (hours - 8 * 22) * wage * 1.5; } void HourlyWorker::print() const { Employee::print(); cout << setw(16) << "Hours Worker\n"; cout << "\twagePerHour " << wage << " Hours " << hours; cout << " earned $" << earnings() << endl; }
class PieceWorker : public Employee { public: PieceWorker(const long, const char*, double = 0.0, int = 0); ~PieceWorker() {} void setWage(double); void setQuantity(int); virtual double earnings() const; virtual void print() const;
private: double wagePerPiece; int quantity; }; PieceWorker::PieceWorker(const long k, const char* str, double w, int q) : Employee(k, str) { setWage(w); setQuantity(q); } void PieceWorker::setWage(double w) { wagePerPiece = w > 0 ? w : 0; } void PieceWorker::setQuantity(int q) { quantity = q > 0 ? q : 0; } double PieceWorker::earnings() const { return quantity * wagePerPiece; } void PieceWorker::print() const { Employee::print(); cout << setw(16) << "Piece Worker\n"; cout << "\twagePerPiece " << wagePerPiece << " quantity " << quantity; cout << " earned $" << earnings() << endl; }
int main() { Employee* employ[6]; employ[0] = new Manager(10135, "Cheng ShaoHua", 1200); employ[1] = new Manager(10201, "Yan HaiFeng", 5300); employ[2] = new HourlyWorker(30712, "Zhao XiaoMing", 5, 8 * 20); employ[3] = new HourlyWorker(30649, "Gao DongSheng", 4.5, 10 * 30); employ[4] = new PieceWorker(20382, "Xiu LiWei", 0.5, 2850); employ[5] = new PieceWorker(20496, "Huang DongLin", 0.75, 1850); cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); for (int i = 0; i < 5; ++i) { employ[i]->print(); } for (int i = 0; i < 5; ++i) { cout << employ[i]->getName() << " " << employ[i]->earnings() << endl; } return 0; }
|