C++员工表类的设计和实现
C++员工表类的设计和实现
主要是对员工表类的构造函数和析构函数的定义和实现设计,使用C++的动态数组来储存员工类。
这里的代码不全,没有时间日期类的代码,需要用到上一次日期时间类,这篇文章里有:
https://www.cnblogs.com/lightice/p/12713084.html
EmployeeHeader.h
1.// #include"DateTimeHeader.h"
2.#include"DateTimeFunction.cpp"
3.#include<string>
4.#include<list>
5.using namespace std;
6.//员工类
7.class Employee
8.{
9.public:
10. string Name; //名字
11. int age;// 年龄
12. int Sex;//性别 0:男生 1:女生
13. Date Birthday;//生日
14. string Number;//工号
15. double Salary;//工资
16. Employee(string, int, int, string, double, Date); //构造函数
17. void setall(string, int, int, string, double, Date);
18. // Employee operator =(Employee);
19. Employee() {};
20. void Show(); //显示数据
21.
22.};
23.class Employ_Graph
24.{
25.public:
26. Employee *ptr; //动态数组
27. Employee em[10]; //静态数组
28. int Count;//当前员工数
29. int Max;//最大人数
30.public:
31. Employ_Graph(int count);//初始化员工表,让头指针置空
32. Employ_Graph();//初始化员工表,让头指针置空
33. ~Employ_Graph();//析构函数
34. void show();//显示员工内容
35. void Increase(Employee);//添加
36. void Delete(string Number);//删除
37. void Search(string Number);//查询
38. void Sort();//按年龄排序
39. double Sta(string Number);//统计工资
40. void Analyze(string Number);//分析员工表
41.
42.};
EmployeeFunction.cpp
1.#include "EmployeeHeader.h"
2.#include<iostream>
3.#include<string>
4.
5.using namespace std;
6.
7.//构造函数
8.Employee::Employee(string Name, int age, int Sex, string Number, double Salary, Date Birthday)
9.{
10. this->Name = Name;
11. this->age = age;
12. this->Sex = Sex;
13. this->Number = Number;
14. this->Salary = Salary;
15. this->Birthday = Birthday;
16.
17.}
18.//显示数据
19.void Employee::Show()
20.{
21.
22. cout << "Name:" << this->Name << " ";
23. cout << "age:" << this->age << " ";
24. if (this->Sex == 0) {
25. cout << "Sex:男" << " ";
26. }
27. else
28. {
29. cout << "Sex:女" << " ";
30. }
31.
32. cout << "Number:" << this->Number << " ";
33. cout << "Salary:" << this->Salary << " ";
34. cout << "Birthday:" << " ";
35. this->Birthday.show();
36.}
37.//设置所有属性
38.void Employee::setall(string Name, int age, int Sex, string Number, double Salary, Date Birthday){
39. this->Name = Name;
40. this->age = age;
41. this->Sex = Sex;
42. this->Number = Number;
43. this->Salary = Salary;
44. this->Birthday = Birthday;
45.}
46.
47.
48.
49.//创建并初始化员工表
50.Employ_Graph::Employ_Graph()
51.{
52. Count = 0;
53. Max = 10;
54. ptr = new Employee[10];
55. cout<<"员工表已创建~"<<endl;
56.}
57.
58.//员工表销毁
59.Employ_Graph::~Employ_Graph()
60.{
61. if(this->ptr)delete[] ptr;
62. cout << "员工表已销毁~"<<endl;
63.}
64.
65.//显示所有员工信息
66.void Employ_Graph::show()
67.{
68. int i=0;
69. cout<<"*-------------------------------------------------------------------------------*"<<endl;
70. while(i<Count){
71. cout<<"|";
72. ptr[i].Show();
73.
74. i++;
75. }
76. cout<<"*--------------------------------------------------------------------------------*"<<endl;
77.}
78.
79.
80.
81.//添加员工
82.void Employ_Graph::Increase(Employee E)
83.{
84.
85. if (Count >= Max)
86. {
87. cout << "人数已满,不能加入员工";
88. }
89. else
90. {
91. ptr[Count]= E;
92. em[Count] = E;
93. }
94. Count++;
95.}
96.
97.
98.//删除员工
99.void Employ_Graph::Delete(string Number)
100.{
101. for (int i = 0; i <Count; i++)
102. {
103. if (ptr[i].Number == Number)
104. {
105. for (int j = i; j <=Count-1; j++)
106. {
107. ptr[j] = ptr[j + 1];
108. }
109. Count--;
110. break;
111. }
112. }
113. cout<<"删除成功~"<<endl;
114.}
main.cpp
1.#include<iostream>
2.#include "EmployeeFunction.cpp"
3.
4.using namespace std;
5.
6.int main(){
7. cout<<"come to main~";
8. Employ_Graph mylist;
9. int option;
10. int age,sex,year,month,day;
11. string name,number;
12. Date d0;
13. Employee people0;
14. double salary;
15.
16. while(true){
17. cout<<"-------------------------------------"<<endl;
18. cout<<"1.增加员工 2.删除员工 3.打印员工 0.退出"<<endl;
19. cout<<"-------------------------------------"<<endl;
20. cout<<"您的操作:";
21. cin>>option;
22. switch(option){
23. case 1:
24. cout<<"请输入员工的姓名 年龄 性别 身份证 薪水 生日(年月日)"<<endl;
25. cin>>name>>age>>sex>>number>>salary>>year>>month>>day;
26. d0.init(year,month,day);
27. people0.setall(name,age,sex,number,salary,d0);
28. mylist.Increase(people0);
29. break;
30.
31. case 2:
32. cout<<"请输入需要删除员工的身份证:"<<endl;
33. cin>>number;
34. mylist.Delete(number);
35. break;
36.
37. case 3:
38. mylist.show();
39. break;
40.
41. case 0:return 0;
42. default:break;
43. }
44. }
45. return 0;
}
运行结果: