C++实验八 多态与虚函数

    技术2022-07-10  99

    C++ 实验八 多态与虚函数

    某小型公司有四类人员:总经理、技术人员、销售经理、推销员。设计一个基类employee, 派生出manager(总经理)、technician(技术人员)、salesmanager(销售经理)、saleman(推销员)。销售经理既是经理又是销售人员,兼具两类人员的特点,因此同时继承manager 和salesman 两个类。 1、类定义 1)employee 类: 基本信息:编号、姓名、性别、出生日期、职位、薪水等; 出生日期使用自定义的 Date(日期)类; 其中:基本信息为 private 属性,成员函数为public 属性; 多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数; 可以从外部访问类成员的友员函数; 2)Date 类: 成员变量:年、月、日 成员函数:SetYear(int year)、SetMonth(int month)、SetDay(int day) GetYear()、GetMonth()、GetDay() 3)派生类technician:新增属性:工作时间 派生类saleman: 新增属性:销售额、所属部门 2、实现人员信息的录入与显示; 3、计算并显示个人月薪: 月薪计算办法:总经理拿固定月薪8000 元,技术人员按每小时25 元领取月薪;推销员 的月薪按当月销售额的4%提成;销售经理固定月薪5000 元加所管辖部门当月销售总额的5 ‰ 。 附加要求 1、将实验中三不同员工的工资计算函数pay(),用虚函数实现; 2、将每个类中的信息显示函数:display()用虚函数实现; 3、对每个类增加一个升职函数:promote();

    Employee.h

    //Employee.h #pragma once #include <string> using namespace std; //或者用(using std::string;)也可以解决string报错 class Date //Date类 { protected: int year, month, day; public: Date(int y = 0, int m = 0, int d = 0); void setYear(int y); void setMonth(int m); void setDay(int d); int getYear(); int getMonth(); int getDay(); }; class Employee //Employee类 { protected: int number; string name; char sex; Date birthday; string post; int wages; public: Employee(); Employee(int, string, char, Date, string); virtual void Pay() = 0; virtual void Show() = 0; virtual void Promote(int); };

    注意: //两个.cpp都引用.h文件出现的重定义问题的解决:将函数实现放在.cpp文件中,另外.h中定义全局变量也会出现这种问题 Employee.cpp

    #include <iostream> #include "Employee.h" #include <string> #include <iomanip> using namespace std; Date::Date(int y, int m, int d) { year = y; month = m; day = d; } void Date::setYear(int y) { year = y; } void Date::setMonth(int m) { month = m; } void Date::setDay(int d) { day = d; } int Date::getYear() { return year; } int Date::getMonth() { return month; } int Date::getDay() { return day; } Employee::Employee() {} Employee::Employee(int num, string na, char se, Date bi, string po) { number = num; name = na; sex = se; birthday = bi; post = po; } void Employee::Promote(int) {}

    mian.cpp

    #include "Employee.h" #include <iostream> #include <string> #include <iomanip> using namespace std; class Manager : virtual public Employee //总经理类 { public: Manager(); Manager(int, string, char, Date, string); void Pay(); void Promote(int); void Show(); }; Manager::Manager() {} Manager::Manager(int num, string na, char se, Date bi, string po) :Employee(num, na, se, bi, po) { Pay(); } void Manager::Pay() { wages = 8000; } void Manager::Promote(int x) { if (x == 1) post = "执行总裁"; } void Manager::Show() { cout << "编号:" << number << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10) << "出生日期:" << birthday.getYear() << "-" << birthday.getMonth() << "-" << birthday.getDay() << setw(6)<< "职务:" << post << setw(6) << "工资:" << wages << endl; } class Technician :public Employee //技术人员类 { public: Technician(); Technician(int, string, char, Date, string, int); void Pay(); void Promote(int); void Show(); protected: int time; }; Technician::Technician() {} Technician::Technician(int num, string na, char se, Date bi, string po, int ti) :Employee(num, na, se, bi, po), time(ti) { Pay(); } void Technician::Pay() { wages = 25 * time; } void Technician::Promote(int x) { if (x == 1) post = "技术总监"; } void Technician::Show() { cout << "编号:" << number << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10) << "出生日期:" << birthday.getYear() << "-" << birthday.getMonth() << "-" << birthday.getDay() << setw(6) << "职务:" << post << setw(6) << "工资:" << wages << setw(10) << "工作时间:" << time << "小时" << endl; } class Saleman :virtual public Employee //销售员类 { public: Saleman(); Saleman(int, string, char, Date, string, int, string); void Pay(); void Promote(int); void Show(); protected: int sale; string branch; }; Saleman::Saleman() {} Saleman::Saleman(int num, string na, char se, Date bi, string po, int sa, string br) :Employee(num, na, se, bi, po), sale(sa), branch(br) { Pay(); } void Saleman::Pay() { wages = sale * 0.04; } void Saleman::Promote(int x) { if (x == 1) post = "销售总监"; } void Saleman::Show() { cout << "编号:" << number << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10) << "出生日期:" << birthday.getYear() << "-" << birthday.getMonth() << "-" << birthday.getDay() << setw(6) << "职务:" << post << setw(6) << "工资:" << wages << setw(8) << "销售额:" << sale << setw(10) << "所属部门:" << branch << endl; } class Salesmanager :public Manager, Saleman //销售经理类 { public: Salesmanager(); Salesmanager(int, string, char, Date, string, int, string); void Pay(); void Promote(int); void Show(); }; Salesmanager::Salesmanager() { Saleman(); } Salesmanager::Salesmanager(int num, string na, char se, Date bi, string po, int sa, string br) :Employee(num, na, se, bi, po), Saleman(num, na, se, bi, po, sa, br) { Pay(); } void Salesmanager::Pay() { Saleman::wages = 5000 + sale * 0.05; } void Salesmanager::Promote(int x) { if (x == 1) post = "销售部长"; } void Salesmanager::Show() { cout << "编号:" << number << setw(6) << "姓名:" << name << setw(6) << "性别:" << sex << setw(10) << "出生日期:" << birthday.getYear() << "-" << birthday.getMonth() << "-" << birthday.getDay() << setw(6) << "职务:" << post << setw(6) << "工资:" << wages << setw(8) << "销售额:" << sale << setw(10) << "所属部门:" << branch << endl; } void Test() { Employee *p; Manager m1(1001, "张可", 'M', Date(89, 06, 10), "总经理 "); Technician t1(1002, "宋书", 'M', Date(83, 01, 23), "技术人员", 120); Saleman s1(1003, "付强", 'M', Date(84, 02, 23), "推销员 ", 3000, "华北地区"); Saleman s2(1004, "陈妍", 'F', Date(89, 01, 24), "推销员 ", 5000, "华东地区"); Salesmanager s3(1005, "李兵", 'M', Date(80, 2, 10), "销售经理", 1000, "华北地区"); Salesmanager s4(1006, "王刚", 'F', Date(82, 1, 11), "销售经理", 2000, "华东地区"); p = &m1; p->Show(); p = &t1; p->Show(); p = &s1; p->Show(); p = &s2; p->Show(); p = &s3; p->Show(); p = &s4; p->Show(); cout << "升职:\n"; Employee *f[6] = {&m1, &t1, &s1, &s2, &s3, &s4}; for (int i = 0; i < 6; i++) { f[i]->Promote(1); f[i]->Show(); } } int main() { cout << "\n人员信息-----------------------------------------------------------------------------------------" << endl; Test(); cout << "-------------------------------------------------------------------------------------------------" << endl; return 0; }
    Processed: 0.016, SQL: 9