14.6.2 创建普通员工类
1、在头文件文件夹中添加employee.h头文件,并写入如下代码:
#pragma once
#include "worker.h"
using namespace std;
class Employee :public Worker
{
public:
Employee(int id, string name, int dId);
void showInfo();
string getDeptName();
};
这里有三个成员函数,第一个是构造函数,后两个分别是从父类继承的纯虚函数。
2、添加employee.cpp源文件,并实现那三个成员函数
#include<iostream>
#include "employee.h"
using namespace std;
Employee::Employee(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DId = dId;
}
void Employee::showInfo()
{
cout << "职工编号:"<<this->m_Id
<< "\t姓名:"<<this->m_Name
<<"\t岗位:"<< this->getDeptName()
<<"\t岗位职责:完成经理交给的任务" << endl;
}
string Employee::getDeptName()
{
return string("员工");
}
到这里,普通职工类就创建完成,并且是继承的Worker类,具有多态的功能。可以测试一下,创建一个Worker类指针指向Employee类对象,看看多态功能是否正常。这里不再测试。