浙江理工大学信息学院实验指导书实验名称:类的多态性的实现学时安排:3实验类别:设计性实验实验要求:1人1组学号:姓名: ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄一、实验目的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容器。
五程序清单//main.cpp#include "class.h"#include<map>int main(){cout<<"请输入指定月份"<<endl;int month;cin>>month;Report re;re.insert(new Technicist("0001","王华","男",CDate(2,4,2011),"技术",9000));re.insert(new Technicist("0002","李明","女",CDate(4,10,2009),"技术",10000));map<int,int> bonus;bonus[1] = 1000;bonus[2] = 2000;bonus[3] = 3000;re.insert(new Manager("0003","朱黎明","男",CDate(11,8,2001),"经理",12000,bonus));bonus[1] = 500;bonus[2] = 1500;bonus[3] = 2000;re.insert(new Manager("0004","刘改云","男",CDate(8,7,2003),"经理",10000,bonus));map<int,int> sales;sales[1] = 200000;sales[2] = 100000;sales[3] = 500000;re.insert(new SalesPerson("0005","李志武","男",CDate(10,11,2007),"销售",5000,sales));re.print(month);return 0;}//class.h#ifndef CLASS_H_INCLUDED#define CLASS_H_INCLUDED#include<windows.h>#include<iostream>#include<string>#include<list>#include<vector>#include<map>#include"date.h"using namespace std;class Employee{protected:string name;string ID;string sex;string job;CDate date;double basicmoney;public:Employee(string ID,string name,string sex,CDate date,string job,double basicmoney);string getjob(){return job;}string getname(){return name;}string getID(){return ID;}string getsex(){return sex;}double getbasicmoney(){return basicmoney;}CDate getdate(){return date;}virtual double getpay(int m)=0;};class Report{private:list <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* p);void print(int n);};class Manager:public Employee{private:map<int,int> price;public:Manager(string ID,string name,string sex,CDate date,string job,double basicmoney,map<int,int> price):Employee( ID,name,sex,date,job,basicmoney){this->price=price;}double getpay(int m);};class Technicist:public Employee{public:Technicist(string ID,string name,string sex,CDate date,string job,double basicmoney):Employee(ID,name,sex,date,job,basicmoney){}double getpay(int m);};class SalesPerson:public Employee{private:map<int,int> sales;public:SalesPerson(string ID,string name,string sex,CDate date,string job,double basicmoney,map<int,int> sales):Employee(ID,name,sex,date,job,basicmoney){ this->sales=sales;}double getpay(int m);};#endif//fuctions.cpp#include "class.h"#include <iostream>#include <algorithm>using namespace std;Employee::Employee(string ID,string name,string sex,CDate date,string job,double basicmoney){this->ID= ID;this->name = name;this->sex = sex;this->date = date;this->basicmoney = basicmoney;this->job = job;}double Manager::getpay(int m){return price[m]+basicmoney;}list <Employee*> Report::operator[](string job){list<Employee*> cp;list<Employee*>::iterator it;for(it=members.begin();it!=members.end();it++){if((*it)->getjob()==job)cp.push_back(*it);}return cp;}void Report::print(int month){cout << " 第" << month << "月职工收入报表" <<endl;cout << "------------------------------------------------------" << endl;cout << "职位:经理" << endl;cout << "工号\t"<< "姓名\t"<< "性别\t"<< "入职时间\t"<< "基本工资\t"<< "薪酬"<< endl;list<Employee*> emp_ls;emp_ls = (*this)["经理"];print(emp_ls,month);cout << "最低薪酬:" << min_pay(emp_ls, month) << endl;cout << "最高薪酬:" << max_pay(emp_ls, month) << endl;cout << "------------------------------------------------------" << endl;cout << "职位:销售" << endl;cout << "工号\t"<< "姓名\t"<< "性别\t"<< "入职时间\t"<< "基本工资\t"<< "薪酬"<< endl;emp_ls = (*this)["销售"];print(emp_ls,month);cout << "最低薪酬:"<< min_pay(emp_ls, month) << endl;cout << "最高薪酬:"<< max_pay(emp_ls, month) << endl;cout << "------------------------------------------------------" << endl;cout << "职位:技术" << endl;cout << "工号\t"<< "姓名\t"<< "性别\t"<< "入职时间\t"<< "基本工资\t"<< "薪酬"<< endl;emp_ls = (*this)["技术"];print(emp_ls,month);cout << "最低薪酬:" << min_pay(emp_ls, month) << endl;cout << "最高薪酬:" << max_pay(emp_ls, month) << endl;}double Report::min_pay(list<Employee*> emp_list, int month){vector<double> pay;list<Employee*>::iterator it;for(it = emp_list.begin(); it != emp_list.end(); it ++){pay.push_back((*it)->getpay(month));}return *min_element(pay.begin(), pay.end()) ;}double Report::max_pay(list<Employee*> emp_list, int month){vector<double> pay;list<Employee*>::iterator it;for(it = emp_list.begin(); it != emp_list.end(); it ++) {pay.push_back((*it)->getpay(month));}return *max_element(pay.begin(), pay.end()) ;}void Report::print(list<Employee*> emp_list,int month){ list<Employee*>::iterator it;for(it = emp_list.begin();it != emp_list.end();it ++) {cout<<(*it)->getID()<<"\t"<<(*it)->getname()<<"\t";cout<<(*it)->getsex()<<"\t";cout<<(*it)->getdate().format("ddd")<<"\t";cout<<(*it)->getbasicmoney()<<"\t"<<"\t";cout<<(*it)->getpay(month) << endl;}}void Report::insert(Employee* p){members.push_back(p);}Report::~Report(){list<Employee*>::iterator it;for(it = members.begin(); it != members.end(); it ++){ delete *it;}}double Technicist::getpay(int m){return basicmoney;}double SalesPerson::getpay(int m){return basicmoney + sales[m] * 0.05;}//date.h//增加友元函数,重载<<friend ostream &operator<<(ostream &os,CDate &date) {os<<date.y<<"年"<<date.m<<"月"<<date.d<<"日"<<endl; return os;}六运行结果七实验心得此次试验涉及内容较多,主要是多态和STL容器的运用,我了解并运用了vector、list、map容器,并使用了迭代器对容器元素进行访问,复习了重载、继承的知识。