c++面向对象部分学完了,做个项目熟悉一下,总体应用了多态,继承,类的方法。
知识补充:
//打开方式 ios::trunc 如果存在,删除文件并重新创建
//打开方式 ios::trunc 如果存在,删除文件并重新创建
ofstream ofs(FILENAME,ios::trunc);
ofs.close();
下面进入项目:
我们有5个头文件,如下
WorkerManager.h
#pragma once //防止头文件重复包含
#include<iostream> //输入输出流头文件
using namespace std; //使用标准命名空间
#include"worker.h"
#include<fstream>
#define FILENAME "empfile.txt"
class WorkerManager
{
public:
//构造函数
WorkerManager();
//显示菜单
void Show_Menu();
//退出菜单
void returnMenu();
//记录职工人数
int m_EmpNum;
//职工数组指针
Worker** m_EmpArray;
//增加职工的函数
void AddEmp();
//保存文件
void save();
//文件不存在
bool fileIsEmpty;
//记录文件原有的人数
int get_empfile();
//初始化数组
void init_Emp();
//显示职工
void show_Emp();
//返回删除职工的位置
int IsExist(int id);
//删除职工
void del_Emp();
//修改职工
void mod_Emp();
//查找职工
void find_Emp();
//排序职工
void Sort_Emp();
//清空文档
void clean_Emp();
//析构函数
~WorkerManager();
};
Worker.h
#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;//部门编号
};
emplyee.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include"worker.h"
class Emloyee :public Worker
{
public:
//构造函数
Emloyee(int id,string name,int did);
//显示个人信息
virtual void showinfo();
//获取岗位名称
virtual string getDeptName();
};
manager.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include"worker.h"
class Manager :public Worker
{
public:
Manager(int id, string name, int did);
virtual void showinfo();
virtual string getDeptName();
};
boss.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include"worker.h"
class Boss :public Worker
{
public:
Boss(int id, string name, int did);
virtual void showinfo();
virtual string getDeptName();
};
cpp文件有5个:
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("老板");
}
emplyee.cpp
#include"employee.h"
//构造函数
Emloyee::Emloyee(int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_DeptId = did;
}
//显示个人信息
void Emloyee::showinfo()
{
cout << "职工编号: " << this->m_id
<< "\t职工姓名: " << this->m_name
<< "\t岗位: " << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务。" << endl;
}
//获取岗位名称
string Emloyee::getDeptName()
{
return string("员工");
}
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("经理");
}
workermanager.cpp
#include"WorkerManager.h"
#include"worker.h"
#include"Boss.h"
#include"employee.h"
#include"manager.h"
WorkerManager::WorkerManager()
{
//1.文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化属性
//初始化记录人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->fileIsEmpty = true;
ifs.close();
return;
}
//2、文件为空
char ch;
ifs >> ch;
if (ifs.eof())//eof() 如果返回true ,则为空文件
{
cout << "文件为空" << endl;
//初始化记录人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->fileIsEmpty = true;
ifs.close();
return;
}
//3、文件有保存的数据
this->m_EmpNum = this->get_empfile();
cout << m_EmpNum;
//开辟空间
m_EmpArray = new Worker * [m_EmpNum];
//初始化
init_Emp();
//测试代码
for (int i = 0; i < m_EmpNum; i++)
{
cout << "职工编号: " << this->m_EmpArray[i]->m_id <<
" 职工姓名" << this->m_EmpArray[i]->m_name <<
" 部门编号" << this->m_EmpArray[i]->m_DeptId << endl;
}
}
//显示菜单
void WorkerManager::Show_Menu()
{
cout << "****************************************************" << endl;
cout << "************欢迎使用职工管理系统********************" << endl;
cout << "****************0.退出管理程序**********************" << endl;
cout << "****************1.增加职工信息********************" << endl;
cout << "****************2.显示职工信息**********************" << endl;
cout << "****************3.删除职工信息**********************" << endl;
cout << "****************4.修改职工信息**********************" << endl;
cout << "****************5.查找职工信息**********************" << endl;
cout << "****************6.按照编号排序**********************" << endl;
cout << "****************7.清空所有文档**********************" << endl;
cout << endl;
}
//退出菜单
void WorkerManager::returnMenu()
{
cout << "欢迎您再次使用.." << endl;
system("pause");
exit(0);//退出程序
}
//添加职工
void WorkerManager::AddEmp()
{
cout << "输入添加员工的个数" << endl;
int Addnum = 0;
cin >> Addnum;
if (Addnum > 0)
{
//添加
//计算添加的大小
int newSize = this->m_EmpNum + Addnum;
Worker** newSpace = new Worker * [newSize];
if (m_EmpNum != 0)
{
for (int i = 0; i < m_EmpNum; i++)
{
newSpace[i] = m_EmpArray[i];
}
}
//批量加新数据
for (int i = 0; i < Addnum; i++)
{
int id;
string name;
int idSelect;
cout << "请输入第" << i + 1 << "个职工编号" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个职工姓名" << endl;
cin >> name;
cout << "请输入职工岗位:" << endl;
cout << "1.普通职工" << endl;
cout << "2.经理" << endl;
cout << "3.老板" << endl;
cin >> idSelect;
Worker* worker = NULL;
switch (idSelect)
{
case 1:
worker = new Emloyee(id, name, 1);
break;
case 2:
worker = new Manager(id, name, 2);
break;
case 3:
worker = new Boss(id, name, 3);
break;
default:
break;
}
//将创建职工指针,保存到数组中
newSpace[this->m_EmpNum + i] = worker;
}
//释放原来的空间
delete[]this->m_EmpArray;
//更改新的空间的指向
m_EmpArray = newSpace;
//更新新的职工人数
m_EmpNum = newSize;
//更新记录人数不为空
this->fileIsEmpty = false;
//提示添加成功
cout << "成功添加" << Addnum << "位新职工" << endl;
//保存数据到文件中
this->save();
}
else
{
cout << "输入有误.." << endl;
}
system("pause");
system("cls");
}
void WorkerManager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);//用输出的方式打开文件,写文件
for (int i = 0; i < m_EmpNum; i++)//将每个人写入文件中
{
ofs << this->m_EmpArray[i]->m_id << " "
<< this->m_EmpArray[i]->m_name << " "
<< this->m_EmpArray[i]->m_DeptId << endl;
}
ofs.close();
}
int WorkerManager::get_empfile()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
//记录人数
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
num++;
}
ifs.close();
return num;
}
void WorkerManager::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
//做标签
int index = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
Worker* worker = NULL;
switch (did)
{
case 1:
worker = new Emloyee(id, name, did);
break;
case 2:
worker = new Manager(id, name, did);
break;
case 3:
worker = new Boss(id, name, did);
break;
default:
break;
}
this->m_EmpArray[index++] = worker;
}
ifs.close();
}
void WorkerManager::show_Emp()
{
if (this->fileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
for (int i = 0; i < m_EmpNum; i++)
{
//利用多态调用接口
this->m_EmpArray[i]->showinfo();
}
}
system("pause");
system("cls");
}
int WorkerManager::IsExist(int id)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_id == id)
{
index = i;
break;
}
}
return index;
}
void WorkerManager::del_Emp()
{
if (this->fileIsEmpty)
{
cout << "文件不存在或记录为空,无法删除!" << endl;
}
else {
cout << "请输入删除的员工编号:" << endl;
int id = 0;
cin >> id;
int index = IsExist(id);
if (index != -1)
{
for (int i = index; i < this->m_EmpNum; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
m_EmpNum--;//更新数组中记录人员的个数
//数据同步保存
this->save();
cout << "删除成功!" << endl;
}
else {
cout << "查无此人,无法删除" << endl;
}
}
system("pause");
system("cls");
}
void WorkerManager::mod_Emp()
{
if (this->fileIsEmpty)
{
cout << "文件不存在或文件为空!" << endl;
}
else {
cout << "请输入您要修改职工的编号:" << endl;
int id;
cin >> id;
int ret = IsExist(id);
if (ret != -1)
{
delete this->m_EmpArray[ret];
int newId = 0;
string newName;
int newDid = 0;
cout << "查到" << id << "号职工,请输入新职工的编号:" << endl;
cin >> newId;
cout << "请输入新职工的姓名" << endl;
cin >> newName;
cout << "请输入新职工的部门" << endl;
cout << "1、普通职工" << endl;
cout << "2、经理" << endl;
cout << "3、老板" << endl;
cin >> newDid;
Worker* worker = NULL;
switch (newDid)
{
case 1:
worker = new Emloyee(newId, newName, newDid);
case 2:
worker = new Manager(newId, newName, newDid);
case 3:
worker = new Boss(newId, newName, newDid);
default:
break;
}
//更新数据 到数组中
this->m_EmpArray[ret] = worker;
cout << "修改成功!" << endl;
this->save();
}
else {
cout << "查无此人,无法修改!" << endl;
}
}
system("pause");
system("cls");
}
void WorkerManager::find_Emp()
{
if (this->fileIsEmpty)
{
cout << "文件为空或不存在!" << endl;
}
else {
cout << "请输入查找方式" << endl;
cout << "1.按照编号排序" << endl;
cout << "2.按照姓名排序" << endl;
int iSelect = 0;
cin >> iSelect;
if (iSelect == 1)
{
cout << "请输入查找的编号:" << endl;
int id1;
cin >> id1;
int ret = IsExist(id1);
if (ret != -1)
{
cout << "查找成功,该职工的信息如下:" << endl;
this->m_EmpArray[ret]->showinfo();
}
else {
cout << "查找失败,查无此人!" << endl;
}
}
else if (iSelect == 2)
{
string name;
cout << "请输入查找的姓名:" << endl;
cin >> name;
bool flag = false;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_name == name)
{
cout << "查找成功,该职工的信息如下:" << endl;
this->m_EmpArray[i]->showinfo();
flag = true;
}
}
if (flag == false)
{
cout << "查找失败,查无此人!" << endl;
}
}
else
{
cout << "输入有误!" << endl;
}
}
system("pause");
system("cls");
}
void WorkerManager::Sort_Emp()
{
if (this->fileIsEmpty)
{
cout << "文件不存在或文件为空!" << endl;
system("pause");
system("cls");
}
else {
cout << "请输入排序方式:" << endl;
cout << "1.按职工编号升序" << endl;
cout << "2.按职工编号降序" << endl;
int select = 0;
cin >> select;
//选择排序法
for (int i = 0; i < this->m_EmpNum; i++)
{
int minOrmax = i;
for (int j = i + 1; j < this->m_EmpNum; j++)
{
if (select == 1)
{
if (this->m_EmpArray[minOrmax]->m_id > this->m_EmpArray[j]->m_id)
{
minOrmax = j;
}
}
else//降序
{
if (this->m_EmpArray[minOrmax]->m_id < this->m_EmpArray[j]->m_id)
{
minOrmax = j;
}
}
}
if (i != minOrmax)
{
Worker* temp = m_EmpArray[i];
m_EmpArray[i] = m_EmpArray[minOrmax];
m_EmpArray[minOrmax] = temp;
}
}
cout << "排序成功,排序结果为:" << endl;
this->save();
this->show_Emp();
}
}
void WorkerManager::clean_Emp()
{
cout << "确认清空?" << endl;
cout << "1.确认" << endl;
cout << "2.返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//打开方式 ios::trunc 如果存在,删除文件并重新创建
ofstream ofs(FILENAME,ios::trunc);
ofs.close();
if (this->m_EmpArray != NULL)
{
for (int i = 0; i < m_EmpNum; i++)
{
if (m_EmpArray[i] != NULL)
{
delete m_EmpArray[i];
}
}
this->m_EmpNum = 0;
delete[]this->m_EmpArray;
this->m_EmpArray = NULL;
this->fileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
WorkerManager::~WorkerManager()
{
//释放堆区数据
if (this->m_EmpArray != NULL)
{
delete[]this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
管理系统.cpp
#include<iostream>
using namespace std;
#include"WorkerManager.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"Boss.h"
int main()
{
测试代码
//Worker* worker = NULL;
//worker = new Emloyee(1, "关羽",1);
//worker->showinfo();
//delete worker;
//worker = new Manager(2, "张飞", 2);
//worker->showinfo();
//delete worker;
//worker = new Boss(3, "刘备", 3);
//worker->showinfo();
//delete worker;
WorkerManager wm;
int choice=0;//记录用户的选择
while (true)
{
wm.Show_Menu();
cout << "请输入你的选择:" << endl;
cin >> choice;
switch (choice)
{
case 0://退出
wm.returnMenu();
break;
case 1://增加职工
wm.AddEmp();
break;
case 2://显示职工
wm.show_Emp();
break;
case 3://删除职工
wm.del_Emp();
break;
case 4://修改职工
wm.mod_Emp();
break;
case 5://查找职工
wm.find_Emp();
break;
case 6://按编号排序职工
wm.Sort_Emp();
break;
case 7://清空文档
wm.clean_Emp();
break;
default:
system("cls");
break;
}
}
wm.Show_Menu();
system("pause");
return 0;
}
over…