规范代码
定义函数或者类尽量放到头文件中,这样不容易出现重复命名和代码冗杂的问题。
#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() = 0;
virtual string getdeptName() = 0;
};
例如我们需要一个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;
};
worker的CPP文件,在这里定义worker的属性。
要注意一点就是项目文件一定要放到一个包里面,VS2019中可以用
#pragma 包名(项目名字)
来实现这个功能。
类似上面的情况,我们可以实现在不更改项目主体的情况下,对项目进行维护和修改
接下来再加几个功能,让项目更加饱满一些。
BOSS类
头文件
#pragma once
#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() = 0;
virtual string getdeptName() = 0;
};
CPP文件
#include"boss.h"
#include<iostream>
using namespace std;
//构造
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;
cout << "\t姓名" << this->m_Name;
cout << "\t岗位" << this->getdeptName() << endl;
cout << "职责(leader)" << endl;
}
string Boss::getdeptName()
{
return string("leeader");
}
EMPLOYEE
头文件
#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();
private:
};
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_DeptId = dId;
}
void Employee::showInfo()
{
cout << "编号" << this->m_Id;
cout<< "\t姓名" << this->m_Name;
cout << "\t岗位" << this->getdeptName()<<endl;
cout << "职责" << endl;
}
//virtual string getdeptName();
string Employee::getdeptName()
{
return string("员工");
}
workermanager
头文件
#pragma once//防止头文件多次包含
#include<iostream>
using namespace std;
class WorkerManager
{
public:
WorkerManager();//构造
//展示菜单
void Show_Menu();
//获取名称
virtual
~WorkerManager();//析构
};
CPP文件
#include"workerManager.h"
WorkerManager::WorkerManager()
{
}
void WorkerManager::Show_Menu()
{
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;
}
WorkerManager::~WorkerManager()
{
}
主函数(只有60多行)
#include<iostream>
#include"workerManager.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
using namespace std;
int main()
{
Worker* worker = NULL;
worker = new Employee(1,"zhang",1);
worker->showInfo();
delete worker;//记得释放
//实例化管理者对象
WorkerManager wm;
//Worker* worker = NULL;加不加都行
worker = new Manager(2,"li manager",2);
worker->showInfo();
delete worker;
worker = new Boss(2, "li manager", 2);
worker->showInfo();
delete worker;
int choice = 0;//记录选项
while (1)
{
wm.Show_Menu();
cout << "输入选择" << endl;
cin >> choice;
switch (choice)
{
case 0://退出
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;
}
}
//调用show
wm.Show_Menu();
system("pause");
return 0;
}
转载:https://blog.csdn.net/EIDoradol/article/details/108245190
查看评论