一家公司有四类人员:总经理、技术员、销售经理、销售员。设计一个基类Employee,派生出总经理Manager、技术人员Technician、销售经理Salesmanager和销售员Salesman。销售经理既是经理又是销售人员,兼具两类人员的特点,因此同时继承Manager 和Salesman 两个类。
1)Employee 类的基本属性:编号、姓名、性别、出生日期、职位、薪水等;出生日期使用自定义的 Date(日期)类;
2)Date 类的基本属性:年、月、日,均采用int类型;
3)派生类Technician,新增属性:工作时间;
4)派生类Salesman, 新增属性:销售额;
要求:
1)实现人员信息的设置与显示;
2)计算并显示个人月薪:月薪计算办法:总经理拿固定月薪8000 元,技术人员按每小时25 元领取月薪;销售员的月薪按当月销售额的4%提成;销售经理固定月薪5000 元加所管辖部门当月销售总额的5‰提成 。
3)将四类不同员工的工资计算函数pay()和每个类中的信息显示函数display()用虚函数实现。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Date
{
private:
int year, month, day;
public:
Date(int y = 0, int m = 0, int d = 0)
{
year = y;
month = m;
day = d;
}
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; }
};
class Employee
{
protected:
int num;
string name;
string sex;
Date birthday;
string position;
float salary;
public:
Employee() {}
Employee(int n, string na, string se, Date b, string p) {
num = n;
name = na;
sex = se;
birthday = b;
position = p;
};
virtual void display() = 0;
virtual void pay() = 0;
};
class Manager : virtual public Employee
{
public:
Manager() {}
Manager(int n, string na, string se, Date b, string p)
:Employee(n, na, se, b, p) {
pay();
}
void pay()
{
salary = 8000;
}
void display()
{
cout << "编号:" << num << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10)
<< "出生日期:" << birthday.getyear() << "年" << birthday.getmonth() << "月" << birthday.getday() << "日"
<< setw(6) << "职务:" << position << setw(6) << "工资:" << salary << endl;
}
};
class Technician :public Employee
{
protected:
int time;
public:
Technician() {}
Technician(int n, string na, string se, Date b, string p, int t) :Employee(n, na, se, b, p), time(t) { pay(); }
void pay()
{
salary = 25 * time;
}
void display()
{
cout << "编号:" << num << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10)
<< "出生日期:" << birthday.getyear() << "年" << birthday.getmonth() << "月" << birthday.getday() << "日"
<< setw(6) << "职务:" << position << setw(6) << "工资:" << salary << setw(6) << "工作时间:" << time << endl;
}
};
class Salesman :virtual public Employee
{
protected:
float sale;
public:
Salesman() {}
Salesman(int n, string na, string se, Date b, string p, float s)
: Employee(n, na, se, b, p), sale(s) {
pay();
}
void pay()
{
salary = sale * 0.04;
}
void display()
{
cout << "编号:" << num << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10)
<< "出生日期:" << birthday.getyear() << "年" << birthday.getmonth() << "月" << birthday.getday() << "日"
<< setw(6) << "职务:" << position << setw(6) << "工资:" << salary << setw(6) << "销售额:" << sale << endl;
}
};
class Salesmanager :public Manager, Salesman
{
public:
Salesmanager() { Salesman(); }
Salesmanager(int n, string na, string se, Date b, string p, float s)
:Employee(n, na, se, b, p), Salesman(n, na, se, b, p, s) {
pay();
}
void pay()
{
salary = 5000 + sale * 0.05;
}
void display()
{
cout << "编号:" << num << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10)
<< "出生日期:" << birthday.getyear() << "年" << birthday.getmonth() << "月" << birthday.getday() << "日"
<< setw(6) << "职务:" << position << setw(6) << "工资:" << salary << setw(6) << "销售额:" << sale << endl;
}
};
void Test()
{
Employee* p;
Manager m1(1001, "张三", "男", Date(89, 06, 10), "总经理 ");
Technician t1(1002, "李四", "男", Date(83, 01, 23), "技术人员", 120);
Salesman s1(1003, "王五", "男", Date(84, 02, 23), "推销员 ", 3000);
Salesman s2(1004, "王红", "女", Date(89, 01, 24), "推销员 ", 5000);
Salesmanager s3(1005, "李兵", "男", Date(80, 2, 10), "销售经理", 1000);
Salesmanager s4(1006, "陈云", "女", Date(82, 1, 11), "销售经理", 2000);
p = &m1;
p->display();
p = &t1;
p->display();
p = &s1;
p->display();
p = &s2;
p->display();
p = &s3;
p->display();
p = &s4;
p->display();
Employee* f[6] = { &m1, &t1, &s1, &s2, &s3, &s4 };
for (int i = 0; i < 6; i++)
{
f[i]->display();
}
}
int main()
{
cout << "\n人员信息-----------------------------------------------------------------------------------------" << endl;
Test();
cout << "-------------------------------------------------------------------------------------------------" << endl;
return 0;
}