1、继续退出功能的实现。
在WorkerManager.h写了抽象的实现后
//退出系统
void ExitSystem();
在WorkerManager.cpp写具体的函数:
//退出系统
void WorkerManager::ExitSystem() { //格式 void WM::函数名(){}
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
最后在职工管理系统.cpp的int main()函数中运行:
switch (choice) {
case 0:
wm.ExitSystem();
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default: //刷新屏幕
system("cls");
break;
}
结果:
2、职工抽象类
①在头文件中创建worker.h文件 纯虚函数直接=0即可,不需要具体实现
#pragma once
#include <iostream>//包含输入输出流头文件
using namespace std;//使用标准命名空间
#include <string>
//职工抽象类
class Worker {
public:
//显示个人信息 //这里我居然写了具体实现,浪费时间!
virtual void showInfo() = 0;
//获取岗位名称
virtual string getDeptName() = 0;
//职工编号
int m_ID;
//职工姓名
string m_Name;
//部门编号
int m_DeptID;
};
②再在头文件中创建employee.h文件 管理普通员工 抽象
#pragma once
#include <string>
using namespace std;
#include"worker.h";
class Employee:public Worker {
public:
Employee(int id,string name,int dID);
//在这里只需要做声明 注意格式 删除=0
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName() ;
};
③在源文件中新建employee.cpp文件
#include"employee.h"
Employee::Employee(int id, string name, int dID) {
this->m_ID = id;
this->m_Name = name;
this->m_DeptID = dID;
}
//在这里只需要做声明 注意格式 删除=0
//显示个人信息
void Employee::showInfo(){ //添加作用域后删除;纯虚函数->{ }
cout << "职工编号:" << this->m_ID
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务"<<endl;
}
//获取岗位名称
string Employee::getDeptName() {
return string("员工");//加不加string()都可以
}
④在职工管理系统.cpp中进行测试
#include<iostream>
using namespace std;
#include "workerManager.h"
#include <string>
#include "worker.h"
#include "employee.h"
int main() {
//测试代码
Worker* worker = NULL;
worker = new Employee(1,"张三",1);
worker->showInfo();
system("pause");
return 0;
}
其余的代码暂时注释掉,保留
结果:
3、仿照职工类的h,cpp文件继续仿写经理类,老板类
manager.h
#pragma once
#include <string>
using namespace std;
#include"worker.h";
class Manager :public Worker {
public:
Manager(int id, string name, int dID);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
manager.cpp
#include "manager.h"
Manager::Manager(int id, string name, int dID) {
this->m_ID = id;
this->m_Name=name;
this->m_DeptID = dID;
}
//显示个人信息
void Manager::showInfo() {
cout << "职工编号:" << this->m_ID
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成老板交给的任务,并下发任务给员工" << endl;
}
//获取岗位名称
string Manager::getDeptName() {
return string("经理");
}
boss.h
#pragma once
#include <string>
using namespace std;
#include"worker.h";
class Boss :public Worker {
public:
Boss(int id, string name, int dID);
//显示个人信息
virtual void showInfo();
//获取岗位名称
virtual string getDeptName();
};
boss.cpp
#include "boss.h"
Boss::Boss(int id, string name, int dID) {
this->m_ID = id;
this->m_Name = name;
this->m_DeptID = dID;
}
//显示个人信息
void Boss::showInfo() {
cout << "职工编号:" << this->m_ID
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:管理公司所有事务" << endl;
}
//获取岗位名称
string Boss::getDeptName() {
return string("老板");
}
进行测试:
#include<iostream>
using namespace std;
#include "workerManager.h"
#include <string>
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
int main() {
//测试代码
Worker* worker = NULL; //必须声名,否则野指针
worker = new Employee(1,"张三",1);
worker->showInfo();
delete worker;
worker = new Manager(2, "李四", 2);//这里不能加Worker *
worker->showInfo();
delete worker;
worker = new Boss(3, "王五", 3);
worker->showInfo();
delete worker;
system("pause");
return 0;
}
注意:这里多次对worker进行new 叉叉类( , ,)的操作又delete并再次循环
错误写法报错:
Worker *worker = new Boss(3,"王老五",3);
不仅多次一举还会报错
正确测试代码运行结果: