栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++多态员工管理实例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++多态员工管理实例

在父类Person中共有staff和BOSS两个子类,显示信息时需工号,姓名和主要工作,control和person呈现聚合关系。

Boss.h

#pragma once
#include
#include"Person.h"
using namespace std;
class Boss:public Person
{public:
	Boss(int nId, string strName, string strGw);
public:
	
	virtual void Work();
};

staff.h

#pragma once
#include
#include"Person.h"
using namespace std;
class Staff:public Person
{public:
	Staff(int nId, string strName, string strGw);
public:
	virtual void Work();
};

person.h

#pragma once
#include
#include
using namespace std;
class Person
{
public:int nId;
	  string strName;
	  string strGw;
public:
	Person();
	void Show();
	virtual void Work() = 0;

};

control.h

#pragma once
#include
#include "Person.h"
using namespace std;
class Control
{
public:Person* arr[10];
public:Control();
public:
	
	void AddStaff(Person* pPs);
	void Show();
	void Work();
};

boss.cpp

#include "Boss.h"
#include"Person.h"

Boss::Boss(int nId,string strName,string strGw)
{
	this->nId = nId;
	this->strName = strName;
	this->strGw = strGw;
};

void Boss::Work() {
	cout << "发工资" << endl;
};

staff.cpp

#include "Staff.h"


Staff::Staff(int nId, string strName, string strGw)
{
	this->nId = nId;
	this->strName = strName;
	this->strGw = strGw;
};

void Staff::Work() {
	cout << "卖苦力" << endl;
};

person.cpp

#include "Person.h"

Person::Person() {
	nId=0;
	strName="";
	strGw = "";
}
void Person::Show() {
	cout << nId << "" << strName << "" << strGw << endl;
};
 

control.cpp

#include "Control.h"

Control::Control() {
	::memset(arr, 0, sizeof(Person*) * 10);
};
void Control::AddStaff(Person* pPs) {
	for (int i = 0; i < 10; i++) {
		if (arr[i]== NULL) {
			arr[i] = pPs;
			return;
		}
	}
	cout << "编制已满" << endl;
};
void Control::Show() {
	for (int i = 0; i < 10; i++) {
		if(arr[i]!=NULL)
		arr[i]->Show();
		else
		{
			cout << "   " << endl;
		}
	}
};
void  Control::Work() {
	for (int i = 0; i < 10; i++) {
		if (arr[i] != NULL)
			arr[i]->Work();
		else
		{
			cout << "   " << endl;
		}
	}
};

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号