当前位置:
文档之家› 实验四 C++中的继承与派生
实验四 C++中的继承与派生
实验四C++中的继承与派生
一、实验目的:
1.理解C++中继承与派生的概念;
2.掌握C++中各种不同继承方式的访问属性差别;
3.掌握单继承与多继承的实现方法;
4.掌握派生类构造函数与析构函数的实现及调用顺序;
5.掌握虚基类的使用方法。
二、实验任务
【题目】
小型公司人员管理
某小型公司有四类人员:经理、技术人员、销售经理、销售员。设计一个基类employee,派生出manager(经理)、technician(技术人员)、salesmanager(销售经理)、saleman(销售员)。销售经理既是经理又是销售员,兼具两类人员的特点,因此同时继承manager 和salesman 两个类。
1、类定义
1)employee 类:
基本信息:编号、姓名、性别、出生日期、职位、薪水等;出生日期使用自定义的Date (日期)类;
2)Date 类:
成员变量:年、月、日
3)派生类technician:新增属性:工作时间
派生类saleman:新增属性:销售额、所属部门
2、实现人员信息的录入与显示;
3、计算并显示个人月薪:
月薪计算办法:总经理拿固定月薪8000 元,技术人员按每小时25 元领取月薪;推销员的月薪按当月销售额的4%提成;销售经理固定月薪5000 元加所管辖部门当月销售总额的5‰。
【提示】
1、在基类中,除了定义构造函数和析构函数,还应统一定义对各类人
员信息应有的操作,规范类族中各派生类的基本行为,但是各类人
员的月薪计算方法不同,不能在基类employee 中统一确定计算方
法。各类人员信息的显示内容不同,同样不能在基类employee中
统一确定显示方法。在基类中实现上述功能的函数体应为空,在派
生类中根据同名覆盖原则定义各自的同名函数实现具体功能。
代码:
#include
#include
using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
void SetYear(int x) {year=x;}
void SetMonth(int x) {month=x;}
void SetDay(int x) {day=x;}
int GetYear() {return year;}
int GetMonth() {return month;}
int GetDay() {return day;}
};
class employee
{
protected:
int num;
string nam;
string sex;
Date birth;
string position ;
float salary;
public:
void display()
{cout<<"编号:"<};
class manager:virtual public employee
{
public:
void set_manager(int n,string na,string se,int y,int m,int d,string pos)
{
num=n;
nam=na;
sex=se;
birth.SetYear(y);
birth.SetMonth(m);
birth.SetDay(d);
position=pos;
}
void display()
{
employee::display();
}
void total_salary()
{ salary=8000;
cout<<"经理月薪:";
cout<< salary<<"元";
}
};
class technician:virtual public employee
{
protected:
float Time;
public:
float t;
void set_technician(int n,string na,string se,int y,int m,int d,string pos,float t) { num=n;
nam=na;
sex=se;
birth.SetYear(y);
birth.SetMonth(m);
birth.SetDay(d);
position=pos;
Time=t;
}
void display()
{ employee::display();
cout<<"工作时间:"<