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)
{