#职工管理系统——C++大作业
初学,望多多指教
##作业要求
一个小公司包含四类人员:经理,技术人员,销售人员和销售经理,各类人员的工资计算方法如下:经理:固定月薪(8000);技术人员:月薪按技术等级(1~8)(1600+等级*300);销售人员:按提成(4%*销售额);销售经理:底薪+提成(1500+0.2%*总销售额);设计一个管理程序,实现对各类人员的信息输入,修改和显示。
##基本方法&&疑惑
在添加员工的时候,在堆区开辟了空间,由子类的指针指向,然后将该指针在压入类型为基类指针的vector容器中,以此来使用多态,但是在删除员工的时候时直接用了vector里自带的函数来删除,不知道会不会释放子类指针所指向的堆区空间。
##具体实现
#include#include #include #include using namespace std; //基类--所有员工共有属性和行为 class Worker { protected: //姓名 string Name; //工号 string Id; public: Worker(string,string); Worker(); //打印 virtual void print() = 0; //月薪 virtual int Monthly_Salary() = 0; //保存到文本文档 virtual void print_txt() = 0; string get_name()const; string gte_id()const; void set_name(string); void set_id(string); }; //经理类 --子类 class Managers :public Worker{ public: Managers(string,string); //打印 virtual void print(); //月薪 virtual int Monthly_Salary(); //存入文本文件 virtual void print_txt(); }; //技术人员 class Technicians : public Worker{ private: //等级 int Exp; public: Technicians(string,string,int = 1); //打印 virtual void print(); //月薪 virtual int Monthly_Salary(); //保存到文本文档 virtual void print_txt(); }; //销售经理 class Sale_Managers :public Worker{ private: //销量 int Sale_Num; public: Sale_Managers(string,string,int = 1000); //打印 virtual void print(); //月薪 virtual int Monthly_Salary(); //保存到文本文档 virtual void print_txt(); }; //销售人员 class Salespeople :public Worker{ private: //销售额 int Sale_Num; public: Salespeople(string,string,int = 1000); //打印 virtual void print(); //月薪 virtual int Monthly_Salary(); //保存到文本文档 virtual void print_txt(); }; //类实现 //基类 Worker::Worker(string name, string id) { Name = name; Id = id; } string Worker::get_name() const { return string(Name); } string Worker::gte_id() const { return string(Id); } void Worker::set_name(string n) { Name = n; } void Worker::set_id(string i) { Id = i; } //子类----------------------------------------------------- void Managers::print() { cout<<"姓名:"< return 8000; } Managers::Managers(string name,string id) : Worker(name,id){ } void Managers::print_txt() { ofstream input; input.open("公司职工名单",ios::app); input<<"姓名:"< cout<<"姓名:"< Name<<"t"; cout<<"职位:"<<"技术人员"<<"t"; cout<<"工号:"< Id<<"t"; cout<<"月薪:"< Monthly_Salary()< return 1600+this->Exp*300; } Technicians::Technicians(string name, string id, int exp ) : Worker(name,id){ Exp = exp; } void Technicians::print_txt() { ofstream input; input.open("公司职工名单",ios::app); input<<"姓名:"< cout<<"姓名:"< Name<<"t"; cout<<"职位:"<<"销售经理"<<"t"; cout<<"工号:"< Id<<"t"; cout<<"月薪:"< Monthly_Salary()< return 1500+0.002*Sale_Num; } Sale_Managers::Sale_Managers(string name, string id, int sale) : Worker(name,id){ Sale_Num = sale; } void Sale_Managers::print_txt() { ofstream input; input.open("公司职工名单",ios::app); input<<"姓名:"< cout<<"姓名:"< Name<<"t"; cout<<"职位:"<<"销售人员"<<"t"; cout<<"工号:"< Id<<"t"; cout<<"月薪:"< Monthly_Salary()< return 0.04*Sale_Num; } Salespeople::Salespeople(string name, string id, int sale ) : Worker(name,id){ Sale_Num = sale; } void Salespeople::print_txt() { ofstream input; input.open("公司职工名单",ios::app); input<<"姓名:"< public: //菜单 void Menu(); //退出 void Exit(); //添加 void Add(); //显示 void Show(); //查找-修改-删除 void Check_Mod_Delete(); //保存到文本文档 void Print_M(); //清楚所有数据 void Clean(); }; vector work; //菜单 void Manager::Menu() { cout<<"************************"< cout << "欢迎下此使用" << endl; exit(0); } //添加职工 void Manager::Add() { string name; string id; cout<<"请输入新员工的姓名:"< >name; cout<<"请输入该员工的工号:"< >id; cout<<"1---经理"< cin>>post; if(post == 1) { //经理 Managers *A; A = new Managers(name,id); work.push_back(A); cout<<"添加成功"< //技术人员 int exp; cout<<"请输入该技术人员的等级(1~8):"< >exp; Technicians *B; B = new Technicians(name,id,exp); work.push_back(B); cout<<"添加成功"< //销售 int sale; cout<<"请输入该销售的销售额:"< >sale; Salespeople *C; C = new Salespeople(name,id,sale); work.push_back(C); cout<<"添加成功"< //销售经理 int sale; cout<<"请输入该销售的销售额:"< >sale; Sale_Managers *D; D = new Sale_Managers(name,id,sale); work.push_back(D); cout<<"添加成功"< if (work.empty()) { cout<<"职工信息为空"< work[i]->print(); } } //查找--修改--删除 职工 void Manager::Check_Mod_Delete() { int temp = -1; cout<<"请输入要查找的职工的工号:"< >id; for(int i = 0;i < work.size();i++) { if (work[i]->gte_id() == id ) { temp = i; break; } } if(temp == -1) //没查到 { cout<<"查无此人"< cout<<"查到此人:"< print(); int A1;//接收用户选则 while(true) { cout<<"请选择你要对此员工的操作:"< >A1; if(A1 == 1)//修改基本信息 { string n; string i; cout<<"请输入修改后的姓名:"< >n; cout<<"请输入修改后的学号:"< >i; work[temp]->set_name(n); work[temp]->set_id(i); cout<<"修改成功;"< int exit = -1; string n; n = work[temp]->get_name(); string i; i = work[temp]->gte_id(); cout<<"1---经理"< cin>>post; if(post == 1) { //经理 Managers *A; A = new Managers(n,i); work.insert(work.begin()+temp,A); work.erase(work.begin()+temp+1); break; } else if(post == 2) { //技术人员 int exp; cout<<"请输入该技术人员的等级(1~8):"< >exp; Technicians *B; B = new Technicians(n,i,exp); work.insert(work.begin()+temp,B); work.erase(work.begin()+temp+1); break; } else if(post == 3) { //销售 int sale; cout<<"请输入该销售的销售额:"< >sale; Salespeople *C; C = new Salespeople(n,i,sale); work.insert(work.begin()+temp,C); work.erase(work.begin()+temp+1); break; } else if(post == 4) { //销售经理 int sale; cout<<"请输入该销售的销售额:"< >sale; Sale_Managers *D; D = new Sale_Managers(n,i,sale); work.insert(work.begin()+temp,D); work.erase(work.begin()+temp+1); break; } else cout<<"请按照指示输入:"< work.erase(work.begin()+temp); cout<<"已将"< get_name()<<"开除"< work.clear(); ofstream ooo; ooo.open("公司职工名单",ios::trunc); ooo.close(); cout<<"数据已清空"< if (work.empty()) { cout<<"职工信息为空"< work[i]->print_txt(); } cout<<"存入文档成功"< int choice; while (true) { Manager xhy; xhy.Menu(); cout<<"请输入你的选择:"< >choice; switch (choice) { case 0: //退出系统 xhy.Exit(); break; case 1: xhy.Add(); system("pause"); system("cls"); break; case 2: xhy.Show(); system("pause"); system("cls"); break; case 3: xhy.Check_Mod_Delete(); system("pause"); system("cls"); break; case 4: xhy.Print_M(); system("pause"); system("cls"); break; case 5: xhy.Clean(); system("pause"); system("cls"); break; } } return 0; }



