写在前面: 希望这篇文章能对大家有一点点帮助,欢迎一起交流进步,谢谢大家的阅读!
一、题目 3 小型公司人员管理
某小型公司有四类人员:总经理、技术人员、销售经理、推销员。设计一个基类employee派生出 manager(总经理)、technician(技术人员)、salesmanager(销售经理)、saleman(推
销员)。销售经理既是经理又是销售人员,兼具两类人员的特点,因此同时继承 manager 和
salesman 两个类。
1、类定义
1)employee 类:
基本信息:编号、姓名、性别、出生日期、职位、薪水等;
出生日期使用自定义的 Date(日期)类;
其中:基本信息为 private 属性,成员函数为 public 属性;
多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数;
可以从外部访问类成员的友员函数;
2)Date 类:
成员变量:年、月、日
成员函数:SetYear(int year)、SetMonth(int month)、SetDay(int day)
GetYear()、GetMonth()、GetDay()
3)派生类 technician:新增属性:工作时间
派生类 saleman: 新增属性:销售额、所属部门
2、实现人员信息的录入与显示;
3、计算并显示个人月薪:
月薪计算办法:总经理拿固定月薪 8000 元,技术人员按每小时 25 元领取月薪;推销员
的月薪按当月销售额的 4%提成;销售经理固定月薪 5000 元加所管辖部门当月销售总额的
5‰ 。
[实验提示]
1、在基类中,除了定义构造函数和析构函数,还应统一定义对各类人员信息应有的操
作,规范类族中各派生类的基本行为,但是各类人员的月薪计算方法不同,不能在基类
employee 中统一确定计算方法。各类人员信息的显示内容不同,同样不能在基类 employee
中统一确定显示方法。在基类中实现上述功能的函数体应为空,在派生类中根据同名覆盖原
则定义各自的同名函数实现具体功能。
2、将基类 employee 分成两个独立文档:employee.h(类声明头文件) 和 employy.cpp(类实现文件)
3、“职位”的类型设定为 int(这里应该是错了,我改为string);
[测试数据]
1、编号:10001 姓名:张可 性别:男
出生日期:75-6-10 职位:总经理
2、编号:10002 姓名:李兵 性别:男
出生日期:79-10-10 职位:销售经理 所属部门:华北地区
3、编号:10003 姓名:王刚 性别:男
出生日期:80-12-10 职位:销售员 所属部门:华北地区
4、编号:10004 姓名:陈浩月 性别:女
出生日期:82-1-10 职位:销售员 所属部门:华北地区
5、编号:10005 姓名:宋书 性别:男
出生日期:79-1-19 职位:技术人员 工作时间:120 小时
6、编号:10006 姓名:付强 性别:男
出生日期:82-4-12 职位:销售员 所属部门:华东地区
二、分析:
题目要求我们做一个小型公司管理的信息展示,我们可以先由主函数进行设计。比如要建立对象数组,以销售员为例:要知道有多少个销售员,这里在程序中只设置了10个。然后要输入信息。编号,名字,性别都可以用string类型。年月日由data类存储。我们继承employee类的基本数据类型,那么只需要增加所属地区和销售额就可以了。根据不同部门的经理对应相应的销售员,用strcmp()然后遍历销售员,得总份额,就可以计算对应的销售经理的月薪了。最后进行展示。调用各自的show函数。各自的show函数又调用基类的Show函数这时只会输出基类的数据成员,然后在派生类的show函数中加入各自的特色就可以了。
三、源代码:
头文件:
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Data {
public:
Data(int y = 1900, int m = 1, int d = 1) {
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;
}
protected:
int year;
int month;
int day;
};
class employee {
public:
employee();//缺省构造函数
employee(int, string, char, Data&, string);//带参数构造函数。
void SetInfo(string nam, string pos) {
name = nam;
position = pos;
}
void Show(employee& p);
Data& operator = (Data&);
friend istream& operator >>(istream&, employee&);
friend ostream& operator <<(ostream&, employee&);
int GetNum() { return num; }
string GetName() { return name; }
protected:
int num;
string name;
string sex;
Data birthday;
string position;
int wage;
};
class manager : public employee {
public:
manager();
manager(int n, string nam, char s, Data& bir, string pos,int wag);
void show(manager *p,int n);
friend istream& operator >>(istream&, manager&);
};
class techincian : public employee {
public:
techincian();
techincian(int n, string nam, char s, Data& bir, string pos, int wag,int wt);
void show(techincian* p, int n);
friend istream& operator >>(istream&, techincian&);
protected:
int worktime;
};
class salesmanager : public employee {
public:
salesmanager();
salesmanager(int n, string nam, char s, Data& bir, string pos, int wag, string area0);
void show(salesmanager* p, int n);
friend istream& operator >>(istream&, salesmanager&);
char* Get_Area() { return area; }
void Set_wage(int sum) { wage = sum * 4 / 100; }
protected:
char area[20];
};
class salesman : public employee {
public:
salesman();
salesman(int n, string nam, char s, Data& bir, string pos, int wag, int wt);
void show(salesman* p, int n);
friend istream& operator >>(istream&, salesman&);
char* Get_Area() { return area; }
int Get_salesvolume() { return salesvolume; }
protected:
int salesvolume;
char area[20];
};
源cpp:
#include<iostream>
#include<string>
#include"employee.h"
using namespace std;
employee::employee() {
num = 1000;
name = "xiaoming";
sex = 'f';
Data birthday(1900, 1, 1);
position = "worker";
}
employee::employee(int n, string nam, char s, Data& bir, string pos) {
num = n;
name = nam;
sex = s;
Data birthay = bir;
position = pos;
}
void employee::Show(employee&p) {
cout << p ;
}
Data& employee::operator =(Data& bir) {
bir.SetYear(this->birthday.GetYear());
bir.SetMonth(this->birthday.GetMonth());
bir.SetDay(this->birthday.GetDay());
return bir;
}
istream& operator >>(istream& input, employee& emp) {
int year, month, day;
input >> emp.num >> emp.name >> emp.sex;
input >> year >> month >> day;
input >> emp.position;
emp.birthday.SetYear(year);
emp.birthday.SetMonth(month);
emp.birthday.SetDay(day);
return input;
}
ostream& operator <<(ostream& output, employee& emp) {
output <<"编号:"<< emp.num << " " << "姓名:" << emp.name << " " << "性别:" << emp.sex << " ";
output << "出生日期:" << emp.birthday.GetYear() << "-" << emp.birthday.GetMonth() << "-" << emp.birthday.GetDay() << " ";
output << "职位:" << emp.position;
return output;
}
/
manager::manager() {
num = 10001;
name = "xiaoming";
sex = "男";
birthday.SetYear(2000);
birthday.SetMonth(1);
birthday.SetDay(1);
position = "总经理";
wage = 8000;
}
manager::manager(int n, string nam, char s, Data& bir, string pos,int wag) {
num = n;
name = nam;
sex = s;
Data birthay = bir;
position = pos;
wage = wag;
}
void manager::show(manager *p, int n) {
for (int i = 0; i < n; i++) {
employee::Show(p[i]);
cout << " " << "月薪:" <<p[i].wage<< "元" << endl;
}
}
istream& operator >>(istream&input, manager& man) {
int year, month, day;
input >> man.num >> man.name >> man.sex;
input >> year >> month >> day;
input >> man.position>>man.wage;
man.birthday.SetYear(year);
man.birthday.SetMonth(month);
man.birthday.SetDay(day);
return input;
}
/
techincian::techincian() {
num = 10001;
name = "xiaoming";
sex = "男";
birthday.SetYear(2000);
birthday.SetMonth(1);
birthday.SetDay(1);
position = "技术人员";
wage = 8000;
}
techincian::techincian(int n, string nam, char s, Data& bir, string pos, int wag,int wt) {
num = n;
name = nam;
sex = s;
Data birthay = bir;
position = pos;
wage = wag;
worktime=wt;
}
void techincian::show(techincian* p, int n) {
for (int i = 0; i < n; i++) {
employee::Show(p[i]);
cout <<" "<< "月薪:" << p[i].worktime*25 << "元" << endl;
}
}
istream& operator >>(istream& input, techincian& man) {
int year, month, day;
input >> man.num >> man.name >> man.sex;
input >> year >> month >> day;
input >> man.position >> man.worktime;
man.birthday.SetYear(year);
man.birthday.SetMonth(month);
man.birthday.SetDay(day);
return input;
}
/
salesmanager::salesmanager() {
num = 10001;
name = "xiaoming";
sex = "男";
birthday.SetYear(2000);
birthday.SetMonth(1);
birthday.SetDay(1);
position = "销售经理";
wage = 5000;
}
salesmanager::salesmanager(int n, string nam, char s, Data& bir, string pos, int wag, string area0) {
num = n;
name = nam;
sex = s;
Data birthay = bir;
position = pos;
wage = wag;
}
void salesmanager::show(salesmanager* p, int n) {
for (int i = 0; i < n; i++) {
employee::Show(p[i]);
cout << " " << "月薪:" << wage +5000 << "元" << endl;
}
}
istream& operator >>(istream& input, salesmanager& man) {
int year, month, day;
input >> man.num >> man.name >> man.sex;
input >> year >> month >> day;
input >> man.position >> man. area;
man.birthday.SetYear(year);
man.birthday.SetMonth(month);
man.birthday.SetDay(day);
return input;
}
/
salesman::salesman() {
num = 10001;
name = "xiaoming";
sex = "男";
birthday.SetYear(2000);
birthday.SetMonth(1);
birthday.SetDay(1);
position = "推销员";
wage = 5000;
}
salesman::salesman(int n, string nam, char s, Data& bir, string pos, int wag, int sv) {
num = n;
name = nam;
sex = s;
Data birthay = bir;
position = pos;
wage = wag;
salesvolume = sv;
}
void salesman::show(salesman* p, int n) {
for (int i = 0; i < n; i++) {
employee::Show(p[i]);
cout << " " << "月薪:" << (float)p[i].salesvolume * 0.04<< "元" << endl;
}
}
istream& operator >>(istream& input, salesman& man) {
int year, month, day;
input >> man.num >> man.name >> man.sex;
input >> year >> month >> day;
input >> man.position >> man.area>>man.salesvolume ;
man.birthday.SetYear(year);
man.birthday.SetMonth(month);
man.birthday.SetDay(day);
return input;
}
int main() {
int n_mg, n_tech, n_sales, n_salesmg;
cout << "请输入的总经理的人数:" ;
cin >> n_mg;
cout << endl<< "请输入的技术人员的人数:";
cin >> n_tech;
cout << endl << "请输入的销售人员的人数:";
cin >> n_sales;
cout << endl << "请输入的销售经理的人数:";
cin >> n_salesmg;
cout << endl;
//创立总经理数组:
manager mg[10];
techincian tech[10];
salesmanager salesmg[10];
salesman sales[10];
for (int i = 0; i < n_mg; i++) {
cout << "请输入第" << i + 1 << "个总经理的编号、姓名、性别、出生年、月、日、职位、薪水" << endl;
cin >> mg[i];
cout << endl;
}
for (int i = 0; i < n_tech; i++) {
cout << "请输入第" << i + 1 << "个技术人员的编号、姓名、性别、出生年、月、日、职位、工作时间" << endl;
cin >> tech[i];
cout << endl;
}
for (int i = 0; i < n_salesmg; i++) {
cout << "请输入第" << i + 1 << "个销售经理的编号、姓名、性别、出生年、月、日、职位、所属部门" << endl;
cin >> salesmg[i];
cout << endl;
}
for (int i = 0; i < n_sales; i++) {
cout << "请输入第" << i + 1 << "个推销员的编号、姓名、性别、出生年、月、日、职位、所属部门、销售额" << endl;
cin >> sales[i];
cout << endl;
}
for (int i = 0; i < n_salesmg; i++) {
int sum = 0;
for (int j = 0; j < n_sales; j++) {
if (strcmp(salesmg[i].Get_Area(), sales[j].Get_Area()) == 0) {
sum = sum + sales[j].Get_salesvolume();
}
}
salesmg[i].Set_wage(sum);
}
cout << "-------------------该公司总经理、销售经理、技术人员、销售员的信息为:——————————" << endl;
mg[0].show(mg, n_mg);
cout << "----------------------------------------------------------------------------——————————" << endl;
salesmg[0].show(salesmg, n_salesmg);
cout << "----------------------------------------------------------------------------——————————" << endl;
tech[0].show(tech, n_tech);
cout << "----------------------------------------------------------------------------——————————" << endl;
sales[0].show(sales, n_sales);
return 0;
}
四、调试:
输入错误了:原因是在重载输入销售员函数时,刚好弄错了最后两个地区和销售额的位置
最终结果: