员工(类的多态性实验)
一、實驗目的
1.理解重載運算符的意義。
2.掌握使用成員函數、友員函數重載運算符的特點。
3.掌握重載運算符函數的調用方法。
4.掌握動態聯編的概念。
5.掌握虛函數和純虛函數的使用方法。
二、實驗原理介紹
設計性實驗
具體原理請見實驗內容和步驟
實現對抽象類的繼承,通過operator函數調用的形式,實現運算符的重載
三、實驗設備介紹
軟件需求: windows或linux下的c++編譯器
硬件需求: 對于硬件方面的要求,建議配置是Pentium III 450以上的CPU處理器,64MB以上的內存,200MB的自由硬盤空間、CD-ROM驅動器、能支持24位真彩色的顯示卡、彩色顯示器、打印機。
四、實驗內容
某公司的員工有經理Manager、技術人員Technicist和營銷人員SalsePerson,他們的薪金計算方法如下:
經理按月計酬,方法是:基本工資+獎金;技術人員按月計酬,方法是:基本工資;營銷人員按月計酬,方法是:基本工資+銷售利潤*5%。
每類人員都有職工編號、姓名、性別、入職時間、職位、基本工資等數據,其中為入職時間定義CDate類,并為該類重載運算符<<,實現入職時間的輸出;各類人員使用統一接口get_pay()計算各類人員的月薪。其次,設計一個統計并輸出該公司每個人員某幾個月薪金情況的報表類Report,該類提供insert接口向Report類的容器中添加員工信息,并提供print接口用于輸出每個員工的職工編號、姓名、性別、入職時間、職位和在設定的月份時間段中該員工的薪酬總額。為了方便實現查找功能,為Report類重載[]運算符的功能,下標值為職位,能根據職位信息查找出所有符合該職位的員工,并重載print接口,輸出查找出的員工信息,信息包括職工編號、姓名、性別、入職時間、職位、基本工資。在主函數中對實現的類進行測試,首先,創建各類人員對象,通過Report類的insert接口向報表中添加這些人員信息,然后通過Report類的print接口輸出報表。其次測試報表的查找功能,輸入要查找的員工職位信息,通過Report類的print接口輸出查找到的員工基本信息報表。
五、注意事項和要求
要求學生要提前準備實驗的內容
實驗完成后要求寫出實驗報告
六、參考書目
【C++ Prime題解】侯捷譯 華中科技大學出版社
【C++程序設計與應用】 張耀仁著 華中科技大學出版社
一、實驗目的
1.理解重載運算符的意義。
2.掌握使用成員函數、友員函數重載運算符的特點。
3.掌握重載運算符函數的調用方法。
4.掌握動態聯編的概念。
5.掌握虛函數和純虛函數的使用方法。
二、實驗原理介紹
設計性實驗
具體原理請見實驗內容和步驟
實現對抽象類的繼承,通過operator函數調用的形式,實現運算符的重載
三、實驗設備介紹
軟件需求: windows或linux下的c++編譯器
硬件需求: 對于硬件方面的要求,建議配置是Pentium III 450以上的CPU處理器,64MB以上的內存,200MB的自由硬盤空間、CD-ROM驅動器、能支持24位真彩色的顯示卡、彩色顯示器、打印機。
四、實驗內容
某公司的員工有經理Manager、技術人員Technicist和營銷人員SalsePerson,他們的薪金計算方法如下:
經理按月計酬,方法是:基本工資+獎金;技術人員按月計酬,方法是:基本工資;營銷人員按月計酬,方法是:基本工資+銷售利潤*5%。
每類人員都有職工編號、姓名、性別、入職時間、職位、基本工資等數據;各類人員使用統一接口get_pay()計算各類人員的月薪,重載<<運算符實現員工信息的輸出。其次,設計一個統計并輸出該公司員工當月薪金情況的報表類Report,該類提供insert接口向Report類的容器中添加員工信息,并提供print接口用于展示以職位為單位的每個員工的職工編號、姓名、性別、入職時間以及當月該員工的薪酬,并統計出該職位員工薪酬的最高值和最低值。為了提供更方便的查找功能,請為Report類重載[]運算符,下標值為職位,能根據職位信息查找出所有符合該職位的員工。在主函數中對實現的類進行測試,首先,創建各類人員對象,通過Report類的insert接口向報表中添加這些人員信息,然后通過Report類的print接口輸出當月員工薪酬情況報表。存儲員工對象的容器請選用合適的STL容器。
五 程序清單
date.h
date.cpp
#include "date.h" CDate::CDate(int dd, int mm, int yy) //初始化 {if((mm>=1 && mm<=12) && (dd>=1 && dd<=31)){m = mm; d = dd; y = yy;}else {m = 0; d = 0; y = 0;cout << "the date created is wrong" << endl;} } CDate::CDate() //初始化 {if(default_date != 0){d = default_date -> d;m = default_date -> m;y = default_date -> y;}else{time_t now;time(&now);struct tm *t_now;t_now = localtime(&now);y = t_now -> tm_year + 1900;m = t_now -> tm_mon + 1;d = t_now -> tm_mday;} } void CDate::add_year(int n) //加n年 {y += n; } void CDate::add_month(int n) //加n月 {m += n; } void CDate::add_day(int n) //加n天 {d += n; } string CDate::format(string df) {char c_df[20];if(df == df_s){sprintf(c_df, "%d-%d-%d", y, m, d);return string(c_df);}if(df == df_l){sprintf(c_df, "%d年%d月%d日", y, m, d);return string(c_df);}return string(""); } int CDate::get_year() const {return y; } int CDate::get_month() const {return m; } int CDate::get_day() const {return d; } CDate *CDate::default_date = 0; const string CDate::df_s = "ddd"; const string CDate::df_l = "DDD"; void CDate::set_default(int d, int m, int y){CDate::default_date = new CDate(d,m,y); } ostream& operator<<(ostream &os,CDate d) {os<<d.format("DDD");return os; }Employee.h
#pragma once #include<string> #include<map> #include<iostream> #include <iomanip> #include"date.h" using namespace std; class Employee { protected:string name,id;int gender;CDate enroll_date;string job;int salary; public:Employee(string ID,string Name,int Gender,CDate Enroll_date,string Job,int Salary);void set_salary(int salary){this->salary = salary;}void set_job(string job){this->job = job;}void set_enroll(CDate enroll){this->enroll_date = enroll;}string get_id(){return id;}string get_name(){return name;}CDate get_enroll(){return enroll_date;}int get_gender(){return gender;}string get_job(){return job;}int get_salary(){return salary;}virtual double get_pay(int m) = 0;friend ostream &operator<<(ostream &output,const Employee &a1); }; class Manager:public Employee{map<int,int> bonus; public:Manager(string id,string name,int gender,CDate enroll,string job,int salary);Manager(string id,string name,int gender,CDate enroll,string job,int salary,map<int,int> bonus);void set_bonus(map<int,int> bonus);virtual double get_pay(int m); }; class Technicist:public Employee{ public:Technicist(string id,string name,int gender,CDate enroll,string job,int salary);virtual double get_pay(int m); }; class SalesPerson:public Employee{ private:map<int,int> sales; public:SalesPerson(string id,string name,int gender,CDate enroll,string job,int salary);SalesPerson(string id,string name,int gender,CDate enroll,string job,int salary,map<int,int> sales);void set_sales(map<int,int> sales);virtual double get_pay(int m); };Employee.cpp
#include<iostream> #include "Employee.h" using namespace std;ostream &operator<<(ostream &output,const Employee &a) {output<<a.id<<"\t"<<a.name<<"\t"<<(a.gender?"男":"女")<<"\t"<<a.enroll_date<<"\t"<<a.job<<"\t"<<a.salary;return output; }Employee::Employee(string ID,string Name,int Gender,CDate Enroll_date,string Job,int Salary){id=ID;name=Name;gender=Gender;enroll_date=Enroll_date;salary=Salary;job=Job; } Manager::Manager(string id,string name,int gender,CDate enroll,string job,int salary):Employee(id,name,gender,enroll,job,salary){} Manager::Manager(string id,string name,int gender,CDate enroll,string job,int salary,map<int,int> bonus):Employee(id,name,gender,enroll,job,salary),bonus(bonus){} double Manager::get_pay(int m){return bonus[m]+get_salary();} Technicist::Technicist(string id,string name,int gender,CDate enroll,string job,int salary):Employee(id,name,gender,enroll,job,salary){} double Technicist::get_pay(int m){return get_salary();} SalesPerson::SalesPerson(string id,string name,int gender,CDate enroll,string job,int salary):Employee(id,name,gender,enroll,job,salary){} SalesPerson::SalesPerson(string id,string name,int gender,CDate enroll,string job,int salary,map<int,int> sales):Employee(id,name,gender,enroll,job,salary),sales(sales){} double SalesPerson::get_pay(int m){return sales[m]*0.05+get_salary();}Report.h
#include"Employee.h" #include<vector> #include<list> #include<iostream> #include<algorithm> using namespace std; class Report { private:vector<Employee*> members;list<Employee*> operator[](string job);double min_pay(list<Employee*> emp_list, int month);double max_pay(list<Employee*> emp_list, int month);void print(list<Employee*> emp_list,int month); public:~Report();void insert(Employee* member);void print(int rp_mon); };Report.cpp
#include "Report.h" Report::~Report(){} list<Employee*> Report::operator[](string job){list<Employee*> L;for(vector<Employee*>::iterator iter1=members.begin();iter1!=members.end();iter1++){if((*iter1)->get_job()==job){L.push_back(*iter1);}}return L; } double Report::min_pay(list<Employee*> emp_list, int month){list<double>Payment;for(list<Employee*>::iterator iter3=emp_list.begin();iter3!=emp_list.end();iter3++){Payment.push_back((*iter3)->get_pay(month));}return *min_element(Payment.begin(),Payment.end()); } double Report::max_pay(list<Employee*> emp_list, int month){list<double>Payment;for(list<Employee*>::iterator iter3=emp_list.begin();iter3!=emp_list.end();iter3++){Payment.push_back((*iter3)->get_pay(month));}return *max_element(Payment.begin(),Payment.end()); } void Report::print(list<Employee*> emp_list,int month){for(list<Employee*>::iterator iter2=emp_list.begin();iter2!=emp_list.end();iter2++){cout<<(*(*iter2))<<"\t"<<(*iter2)->get_pay(month)<<endl;} }void Report::insert(Employee* member){members.push_back(member); } void Report::print(int rp_mon){cout<<" 第"<<rp_mon<<"月職工收入報表 "<<endl;list<Employee*>empl;string Position[3]={"經理","技術","銷售"};for(int i=0;i<3;i++){cout<<"------------------------------------------------------------------------"<<endl;cout<<"職位:"<<Position[i]<<endl;cout<<"工號\t"<<"姓名\t"<<"性別\t"<<"入職時間\t"<<"職位\t"<<"基本工資\t"<<"獎金"<<endl;empl=(*this)[Position[i]];print(empl,rp_mon);cout<<"Max payment: "<<max_pay(empl,rp_mon)<<endl;cout<<"Min payment: "<<min_pay(empl,rp_mon)<<endl<<endl;} }main.cpp
/* *@Author: STZG *@Language: C++ */ #include<iostream> #include "employee.h" #include "report.h" #include "date.h" using namespace std; int main() {Report AA;AA.insert(new Manager("001","Tom",1,CDate(11,8,2001),"經理",2000,map<int,int>{{1,1000},{2,2000},{3,3000},{4,3000},{5,2500},{6,4000},{7,7000},{8,8000},{9,3000},{10,1050},{11,10000},{12,6000}}));AA.insert(new Manager("002","Ann",0,CDate(23,5,2013),"經理",3000,map<int,int>{{1,1000},{2,2000},{3,3000},{4,3000},{5,2500},{6,4000},{7,7000},{8,8000},{9,3000},{10,1050},{11,10000},{12,6000}}));AA.insert(new Manager("003","Jim",1,CDate(16,7,1998),"經理",3000,map<int,int>{{1,1000},{2,2000},{3,3000},{4,3000},{5,2500},{6,4000},{7,7000},{8,8000},{9,3000},{10,1050},{11,10000},{12,6000}}));AA.insert(new Technicist("004","David",0,CDate(14,12,2009),"技術",2000));AA.insert(new Technicist("005","Tina",1,CDate(26,8,2015),"技術",1500));AA.insert(new Technicist("006","John",1,CDate(18,1,1999),"技術",3000));AA.insert(new SalesPerson("007","Helen",0,CDate(4,2,2000),"銷售",2000,map<int,int>{{1,1000},{2,8000},{3,1000},{4,9000},{5,1000},{6,1000},{7,1000},{8,1000},{9,1000},{10,2000},{11,1000},{12,1000}}));AA.insert(new SalesPerson("008","Lily",0,CDate(30,11,2007),"銷售",1000,map<int,int>{{1,1000},{2,2000},{3,3000},{4,3000},{5,2500},{6,4000},{7,7000},{8,8000},{9,3000},{10,1050},{11,10000},{12,6000}}));AA.insert(new SalesPerson("009","Tony",1,CDate(1,8,2013),"銷售",1500,map<int,int>{{1,1000},{2,1000},{3,3000},{4,1000},{5,1000},{6,1000},{7,1000},{8,1000},{9,1000},{10,1000},{11,1000},{12,1000}}));int month;cout<<"請輸入生成報表的月份"<<endl;cin>>month;AA.print(month);//cout << "Hello world!" << endlreturn 0; }六 運行結果
七 實驗心得
1.理解重載運算符的意義。
2.掌握使用成員函數、友員函數重載運算符的特點。
3.掌握重載運算符函數的調用方法。
4.掌握動態聯編的概念。
5.掌握虛函數和純虛函數的使用方法。
總結
以上是生活随笔為你收集整理的员工(类的多态性实验)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浙江理工大学2019年5月赛
- 下一篇: 离散数学实验题目-集合