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;
}
運行結果: