在父类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;
}
}
};



