Kai’blog

主博客 | 势利纷华,近之而不染者洁,不近者亦洁,君子不立危墙之下。

code

//#pragma execution_charactor_set("utf-8")
#include<iostream>
#include <string>
#include <vector>
using namespace std;

class Police;
class Thief;
class PoliceStation;
class Sheriff;
class NormalPolice;
class AuxiliaryPolice;
class BigThief;
class SmallThief;
class Police
{
public:
~Police() = default;
virtual void Catch(Thief& athief) = 0;
virtual string id() const = 0;
Police() { bonus = catchingnum = 0; PS = NULL; }
Police(string N) :name(N), bonus(0), catchingnum(0), PS(NULL) {}
void addPS(PoliceStation* const aPS) { if (aPS != NULL)PS = aPS; }
int getbonus() const { return bonus; }
string getname()const { return name; }
protected:
int bonus;
int catchingnum;
string name;
public:PoliceStation* PS;//所属警察局
};
class PoliceStation {
public:
int getPrestige() const { return prestige; };
int getNumber() const { return P.size(); };
void addPrestige(int n) { prestige += n; }
void addPolice(Police& aP) { P.push_back(&aP); aP.addPS(this); }
PoliceStation() { prestige = 0; }
PoliceStation(int pp) :prestige(pp) {}
PoliceStation(vector <Police*> PP, int pp) :P(PP), prestige(pp) {}
~PoliceStation()=default;

void show()const;

private:
int prestige;
vector<Police*> P;
};
class Thief {
public:
virtual int becaught() const = 0;

Thief() :dirtymoney(0) {};
Thief(int D) :dirtymoney(D) {}
~Thief() = default;
protected:
int dirtymoney;
};
class Sheriff :public Police//警长
{
public:
virtual void Catch(Thief& athief)
{
PS->addPrestige(athief.becaught());
catchingnum++;
bonus += 100;
}
virtual string id() const
{
return "警长";
}
~Sheriff() = default;
Sheriff() = default;
Sheriff(string N) :Police(N) {}
};
class NormalPolice :public Police//普通警察
{
public:
virtual void Catch(Thief& athief)
{
PS->addPrestige(athief.becaught());
catchingnum++;
bonus += 200 * catchingnum;
}
virtual string id()const
{
return "警员";
}
~NormalPolice() = default;
NormalPolice() = default;
NormalPolice(string N) :Police(N) {}
};
class AuxiliaryPolice :public Police//协警
{
public:
virtual void Catch(Thief& athief)
{
PS->addPrestige(athief.becaught());
catchingnum++;
bonus += 300;
}
virtual string id()const
{
return "辅警";
}
~AuxiliaryPolice() = default;
AuxiliaryPolice() = default;
AuxiliaryPolice(string N) :Police(N) {}
};
class BigThief :public Thief
{
public:
virtual int becaught() const
{
return 5 + dirtymoney / 10;
}
BigThief() = default;
BigThief(int D) :Thief(D) {}
};
class SmallThief :public Thief
{
public:
virtual int becaught() const
{
return 1;
}
SmallThief() = default;
SmallThief(int D) :Thief(D) {}
};
void PoliceStation::show() const
{
cout << "----INFORMATIONS-----" << endl;
cout << "警察局声望:" << prestige << endl;
for (const auto &p : P)
{
cout << p->id() << p->getname() << "的奖金:" << p->getbonus() << endl;
}
};
using SP = Sheriff;
using AP = AuxiliaryPolice;
using NP = NormalPolice;

int main()
{
PoliceStation station(100);
Police &ZS = *new SP("张三"), &LS = *new NP("李四"), &WU = *new NP("王五"), &ZL = *new AP("赵六");
station.addPolice(ZS);station.addPolice(LS);station.addPolice(WU);station.addPolice(ZL);
Thief &aBF = *new BigThief(20), &aSF = *new SmallThief;

ZS.Catch(aBF); ZS.Catch(aSF);
LS.Catch(aSF); LS.Catch(aSF);
WU.Catch(aSF);
ZL.Catch(aSF);

station.show();

delete &ZS, &LS, &WU, ZL;
return 0;
}

posted @ 2024-05-30 09:31  Kai-G  阅读(11)  评论(0编辑  收藏  举报
Copyright © 2019-2020 拱垲. All rights reserved.