C++ 第二阶段 总结 封装、继承、多态

感谢B站黑马程序员!有兴趣的可以去B站详细学习!

黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili 

多态的好处:可能在短期的代码量可能会多一点,但是会使整个项目的代码更加的有扩展性,在追加新的代码功能时,不会更改原有的代码,会使得代码结构更加的清晰。

案例分析:

worker类作为抽象的父类:

#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;
};

创建员工类,继承抽象父类,同样创建经理类与老板类:

#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

class employee : public Worker
{
public:
	employee(int id,string name,int did);

	virtual void showinfo();

	virtual string getdeptname();

};

//****************
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

class Manager :public Worker
{
public:
	Manager(int id, string name, int did);

	virtual void showinfo();

	virtual string getdeptname();
};
//**************
#pragma once
#include<iostream>
using namespace std;
#include "worker.h"

class Boss : public Worker
{
public:
	Boss(int id, string name, int did);

	virtual void showinfo();

	virtual string getdeptname();

};

在cpp中继承并实现显示与返回:

#include "employee.h"

employee::employee(int id, string name, int did)
{
	this->m_id = id;
	this->m_name = name;
	this->m_deptid = did;
}

void employee::showinfo()
{
	cout << "职工编号: " << this->m_id
		<< "\t职工姓名: " << this->m_name
		<< "\t职工岗位: " << this->getdeptname()
		<< "\t岗位职责:完成经理交给的任务!" << endl;
}

string employee::getdeptname() 
{
	return string("员工");
}
//**********************************
#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("经理");
}
//************************
#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("总裁");
}

 对Worker** n_EmpArray;的理解:

我们需要一个指针数组来接收记录所创建的职工,并用二级指针去维护数组

在头文件中声明,在cpp中实现功能:

#pragma once //防止头文件重复包含
#include <iostream>    // 包含输入输出流的头文件
using namespace std; //使用标准的命名空间
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
#include <fstream>
#define FILENAME "empFile.txt"


class WorkerManager
{
public:
	

	WorkerManager();

	void Show_Menu();

	void exitsystem();

	int n_Empnum;

	Worker** n_EmpArray;

	void add_Emp();//添加返回职工

	void save();

    bool n_fileisempty;

	int get_EmpNum();

	void init_Emp();

	void show_Emp();

	int IsExist(int id);

	void Del_Emp();

	void Mod_Emp();

	void find_Emp();

	void sort_Emp();

	void Clean_File();

	~WorkerManager();
};

 实现功能:

#include "workerManager.h"

WorkerManager::WorkerManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
		if (!ifs.is_open())
		{
			//cout << "文件不存在!!!" << endl;
			this->n_Empnum = 0;
			this->n_fileisempty = true;
			this->n_EmpArray = NULL;
			ifs.close();
			return;
		}
		char ch;
		ifs >> ch;
		if (ifs.eof())
		{
			//cout << "文件为空!!!" << endl;
			this->n_Empnum = 0;
			this->n_fileisempty = true;
			this->n_EmpArray = NULL;
			ifs.close();
			return;
		}
		//文件存在:
		int num = this->get_EmpNum();
		cout << "职工人数为:" << num << endl;
		this->n_Empnum = num;

		this->n_EmpArray = new Worker * [this->n_Empnum];
		this->init_Emp();

		/*for (int i = 0; i < this->n_Empnum; i++)
		{
			cout << "职工编号: " << this->n_EmpArray[i]->m_id
				<< "   姓名: " << this->n_EmpArray[i]->m_name
				<< "   部门编号: " << this->n_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;
	cout << endl;
}

void WorkerManager::exitsystem()
{
	cout << "欢迎下次使用!!" << endl;
	system("pause");
	exit(0);
}

void WorkerManager::add_Emp()
{
	cout << "请输入添加人的数量:" << endl;
	int addnum = 0;
	cin >> addnum;
	if (addnum > 0)
	{
		int newsize = this->n_Empnum + addnum;
		Worker ** newspace = new Worker* [newsize];

		if (this->n_EmpArray != NULL)
		{
			for (int i = 0; i < this->n_Empnum; i++)
			{
				newspace[i] = this->n_EmpArray[i];
			}
		}
		for (int i = 0; i < addnum; i++)
		{
			int id;
			string name;
			int dSelect;

			cout << "请输入第" << i + 1 << "个新职工的编号: " << endl;
			cin >> id;
			cout << "请输入第" << i + 1 << "个新职工的姓名: " << endl;
			cin >> name;
			cout << "请输入第" << i + 1 << "个新职工的岗位:" << endl;
			cout << "1、普通员工" << endl;
			cout << "2、经理" << endl;
			cout << "3、老板" << endl;
			cin >> dSelect;

			Worker* worker = NULL;
			switch(dSelect)
			{
				case 1:
					worker = new employee(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->n_Empnum + i] = worker;
		}
		delete[] this->n_EmpArray;
		this->n_EmpArray = newspace;

		this->n_Empnum = newsize;

		this->n_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 < this->n_Empnum; i++)
	{
		ofs << this->n_EmpArray[i]->m_id << " "
			<< this->n_EmpArray[i]->m_name << " "
			<< this->n_EmpArray[i]->m_deptid << endl;
	}
	ofs.close();
}

int WorkerManager::get_EmpNum()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id;
	string name;
	int did;

	int num = 0;
	while (ifs >> id && ifs >> name && ifs >> did)
	{
		num++;
	}
	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;
		if (did == 1)
		{
			worker = new employee(id, name, did);
		}
		else if (did == 2)
		{
			worker = new Manager(id, name, did);
		}
		else
		{
			worker = new Boss(id, name, did);
		}
		this->n_EmpArray[index] = worker;
		index++;
	}
	ifs.close();
}

void WorkerManager::show_Emp()
{
	if (this->n_fileisempty)
	{
		cout << "文件不存在或者无记录!!!" << endl;
	}
	else
	{
		for (int i = 0; i < this->n_Empnum; i++)
		{
			this->n_EmpArray[i]->showinfo();
		}
	}
	system("pause");
	system("cls");
}

int WorkerManager::IsExist(int id)
{
	int index = -1;

	for (int i = 0; i < n_EmpArray[i]->m_id; i++)
	{
		if (this->n_EmpArray[i]->m_id == id)
		{
			index = i;
			break;
		}
	}
	return index;
}

void WorkerManager::Del_Emp()
{
	if (this->n_fileisempty)
	{
		cout << "职工不存在!!" << endl;
	}
	else
	{
		cout << "请输入想要删除职工的编号:" << endl;
		int id = 0;
		cin >> id;

		int index = this->IsExist(id);
		if (index != -1)
		{
			for (int i = index; i < this->n_Empnum - 1; i++)
			{
				this->n_EmpArray[i] = this->n_EmpArray[i + 1];
			}
			this->n_Empnum--;
			this->save();

			cout << "删除成功!!!" << endl;
		}
		else
		{
			cout << "删除失败,没有找到该编号的员工!!" << endl;
		}

	}

	system("pause");
	system("cls");
}

void WorkerManager::Mod_Emp()
{
	if (this->n_fileisempty)
	{
		cout << "文件不存在或记录为空!!!" << endl;

	}
	else
	{
		cout << "请输入修改职工编号:" << endl;
		int id;
		cin >> id;
		int res = this->IsExist(id);
		if (res != -1)
		{
			delete this->n_EmpArray[res];
			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 employee(newid,newname,newdid);
				break;
			case 2:
				worker = new Manager(newid, newname, newdid);
				break;
			case 3:
				worker = new Boss(newid, newname, newdid);
				break;
			default:
				break;
			}

			this->n_EmpArray[res] = worker;

			cout << "修改成功!!!" << endl;

			this->save();
		}
		else
		{
			cout << "修改失败,查无此人!!!" << endl;
		}
	}

	system("pause");
	system("cls");
}

void WorkerManager::find_Emp()
{
	if (this->n_fileisempty)
	{
		cout << "文件不存在或记录为空!!!" << endl;
	}
	else
	{
		cout << "请输入查找的方式:" << endl;
		cout << "1、按照职工编号进行查找" << endl;
		cout << "2、按照职工姓名进行查找" << endl;

		int select = 0;
		cin >> select;
		if (select == 1)
		{
			int id;
			cout << "请输入查找的职工编号: " << endl;
			cin >> id;
			int res = IsExist(id);
			if (res != -1)
			{
				cout << "查找成功!!!该职工信息如下:" << endl;
				this->n_EmpArray[res]->showinfo();
			}
			else
			{
				cout << "查无此人!!!" << endl;
			}

		}
		else if (select == 2)
		{
			string name;
			cout << "请输入查找的姓名: " << endl;
			cin >> name;
			bool flag = false;

			for (int i = 0; i < this->n_Empnum; i++)
			{
				if (this->n_EmpArray[i]->m_name == name)
				{
					cout << "查找成功!!!该职工信息如下:" << endl;
					flag = true;
						
					this->n_EmpArray[i]->showinfo();
				}
			}
			if (flag == false)
			{
				cout << "查找失败!!查无此人!!!" << endl;
			}
		}
		else
		{
			cout << "输入有误!!!" << endl; 
		}
	}

	system("pause");
	system("cls");
}

void WorkerManager::sort_Emp()
{
	if (this->n_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->n_Empnum; i++)
		{
			int minormax = i;
			for (int j=i+1;j<this->n_Empnum;j++)
			{ 
			if (select == 1)
			{
				if (this->n_EmpArray[minormax]->m_id > this->n_EmpArray[j]->m_id)
				{
					minormax = j;
				}
			}
			else
			{
				if (this->n_EmpArray[minormax]->m_id < this->n_EmpArray[j]->m_id)
				{
					minormax = j;
				}
			}
			}
			if (i != minormax)
			{
				Worker* temp = this->n_EmpArray[i];
				this->n_EmpArray[i] = this->n_EmpArray[minormax];
				this->n_EmpArray[minormax] = temp;
			}
		}
		cout << "排序成功!!!排序后的结果为: " << endl;
		this->save();
		this->show_Emp();
	}
}

void WorkerManager::Clean_File()
{
	cout << "确定清空文件吗???" << endl;
	cout << "1、确定!" << endl;
	cout << "2、再想想!" << endl;

	int select = 0;
	cin >> select;
	if (select == 1)
	{
		ofstream ofs(FILENAME,ios::trunc);
		ofs.close();

		if (this->n_EmpArray != NULL)
		{
			for (int i = 0; i < this->n_Empnum; i++)
			{
				delete this->n_EmpArray[i];
				this->n_EmpArray[i] = NULL;
			}

			delete[] this->n_EmpArray;
			this->n_EmpArray = NULL;
			this->n_Empnum = 0;
			this->n_fileisempty = true;
		}
		cout << "清空成功!!!" << endl;
	}
	system("pause");
	system("cls");
}

WorkerManager::	~WorkerManager()
{
	if (this->n_EmpArray != NULL)
	{
		delete[] this->n_EmpArray;
		this->n_EmpArray = NULL;
	}
}

最后在主main函数中选用case语句去依次实现功能:

#include <iostream>
#include <string>
#include <fstream>
#include "workerManager.h"
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
using namespace std;

int main()
{

	WorkerManager wm;
	while(true)
	{ 
	wm.Show_Menu();
	cout << "您的选择是:" << endl;
	int choice = 0;
	cin >> choice;
	
	switch (choice)
	{
	case 0 :  //退出
		wm.exitsystem();
		break;
	case 1 :  //增加
		wm.add_Emp();
		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_File();
		break;
	default:
		break;
	}
}
	system("pause");
}

友情提示:

建议自己动手打代码!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值