目录
需求
分析设计
动物类
笼子类
房子类
人类
小贴士
整体设计图
开发实现
动物类
笼子类
房子类
人类
小贴士
控制台主函数
需求
动物猫(CCat)、狗(CDog)、蛇(CSnake),包含名字(m_strName)、颜色(m_strColor)、年龄(m_nAge)。
笼子类(CCage):它负责装3种动物,每个笼子有对应的编号。
房子类(CHouse):负责存放装动物的笼子。
人(CPeople):负责花钱买动物,造笼子,将动物放到笼子中,存放到房子里,杀死某个笼子动物,销毁某个笼子(如果有动物一并销毁),
查看某个或所有笼子动物,和某个动物玩耍。控制台程序,通过输入命令,可以进行简单的人机交互。
分析设计
动物类
由于猫狗蛇有着许多共同的属性,所以我们选择创建一个动物类作为他们的父类,用继承和多态来实现他们的关系。
- 它的成员属性包括:名字、颜色、年龄
- 成员函数包括:初始化、玩耍、展示
笼子类
它与动物类的关系是关联的关系
- 成员属性:笼子编号、装的动物
- 成员函数:笼子初始化、向笼子中装动物
房子类
它与笼子类是聚合的关系
- 成员属性:存放笼子的链表
- 成员函数:向房子中装笼子
人类
它与动物类、笼子类和房子类是依赖的关系
- 成员属性:姓名、钱
- 成员函数:初始化、买动物、造笼子、将动物放在笼子里、将笼子放在房子里、清除笼子中的动物、销毁笼子、查看所有笼子、查看某一个笼子、和某个动物去玩、显示人物信息
小贴士
最后我们创建了一个小贴纸,将枚举类型和宏定义放在了里面
- 枚举动物的种类,宏定义动物的价格和人的钱
整体设计图
开发实现
动物类
- 父类
//CAnimal.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class CAnimal
{
public:
string m_strName;
string m_strColor;
int m_nAge;
public:
CAnimal();
virtual ~CAnimal(); //虚析构
void initAni(string name, string color, int age);
virtual void PlayAni() = 0;
void ShowAni();
};
//CAnimal.cpp
#include "CAnimal.h"
CAnimal::CAnimal():m_nAge(0)
{
}
CAnimal::~CAnimal()
{
}
void CAnimal::initAni(string name, string color, int age)
{
m_strName = name;
m_strColor = color;
m_nAge = age;
}
void CAnimal::ShowAni()
{
cout << "name: " << m_strName << ", color: " << m_strColor << ", age" << m_nAge << endl;
}
- 三个子类
狗:
//CDog.h
#pragma once
#include "CAnimal.h"
class CDog :public CAnimal
{
public:
virtual void PlayAni();
};
//CDog.cpp
#include "CDog.h"
void CDog::PlayAni()
{
cout << "汪~汪~汪,去啃骨头" << endl;
}
猫:
//CCat.h
#pragma once
#include "CAnimal.h"
class CCat :public CAnimal
{
public:
virtual void PlayAni();
};
//CCat.cpp
#include "CCat.h"
void CCat::PlayAni()
{
cout << "喵~喵~喵,捞鱼吃" << endl;
}
蛇:
//CSnake.h
#pragma once
#include "CAnimal.h"
class CSnake :public CAnimal
{
public:
virtual void PlayAni();
};
//CSnake.cpp
#include "CSnake.h"
void CSnake::PlayAni()
{
cout << "嘶~嘶~嘶,吃老鼠" << endl;
}
笼子类
//CCage.h
#pragma once
#include "CAnimal.h"
class CCage
{
public:
int m_id;
CAnimal* m_pAni;
public:
CCage();
~CCage();
void InitCage(int id);
void PushAnimal(CAnimal* pAni);
};
//CCage.cpp
#include "CCage.h"
CCage::CCage():m_id(0),m_pAni(nullptr)
{
}
CCage::~CCage()
{
}
void CCage::InitCage(int id)
{
m_id = id;
}
void CCage::PushAnimal(CAnimal* pAni)
{
if (pAni) {
m_pAni = pAni;
}
}
房子类
//CHouse.h
#pragma once
#include<list>
#include "CCage.h"
using namespace std;
class CHouse
{
public:
list<CCage*> m_lstCage;
public:
CHouse();
~CHouse();
void PushCage(CCage* pCage);
};
//CHouse.cpp
#include "CHouse.h"
CHouse::CHouse()
{
}
CHouse::~CHouse()
{
list<CCage* >::iterator ite = m_lstCage.begin();
while (ite != m_lstCage.end()) {
if ((*ite)) { //笼子不为空
if ((*ite)->m_pAni) { //如果存在动物
delete(*ite)->m_pAni; //销毁动物
(*ite)->m_pAni = nullptr;
}
delete(*ite); //销毁笼子
(*ite) = nullptr;
}
ite++;
}
m_lstCage.clear(); //清空节点
}
void CHouse::PushCage(CCage* pCage)
{
if (pCage) m_lstCage.push_back(pCage);
}
人类
//CPeople.h
#pragma once
#include "CHouse.h"
#include "config.h"
class CPeople
{
public:
string m_name;
int m_money;
public:
CPeople();
~CPeople();
void InitPeople(string name, int money);
bool BuyAnimal(CAnimal* pAni, ani_kind kind);
CCage* CreatCage();
void PushAniToCage(CCage* pCage, CAnimal* pAni);
void PushCageToHouse(CHouse* pHouse, CCage* pCage);
void KillAnimal(CHouse* pHouse, int id);
void DestoryCage(CHouse* pHouse, int id);
void LookAll(CHouse* pHouse);
void LookOne(CHouse* pHouse, int id);
void PlayWithAnimal(CHouse* pHouse, int id);
void showPeople();
};
//CPeople.cpp
#include "CPeople.h"
CPeople::CPeople():m_money(0)
{
}
CPeople::~CPeople()
{
}
void CPeople::InitPeople(string name, int money)
{
m_name = name;
m_money = money;
}
#define BUY_ANIMAL(ANI_PRICE)\
if (m_money - ANI_PRICE >= 0) {\
m_money -= ANI_PRICE;\
}\
else {\
cout << "穷鬼~,钱不够买动物了" << endl;\
return false;\
}
bool CPeople::BuyAnimal(CAnimal* pAni, ani_kind kind)
{
if (!pAni) {
cout << "动物的指针为空" << endl;
return false;
}
switch (kind)
{
case ani_kind::ani_dog:
{
/* if (m_money - DOG_PRICE >= 0) {
m_money -= DOG_PRICE;
}
else {
cout << "穷鬼~,钱不够买动物了" << endl;
return false;
}*/
BUY_ANIMAL(DOG_PRICE)
}
break;
case ani_kind::ani_cat:
{
BUY_ANIMAL(CAT_PRICE)
}
break;
case ani_kind::ani_sna:
{
BUY_ANIMAL(SNA_PRICE)
}
break;
}
cout << "动物买成功了" << endl;
return true;
}
CCage* CPeople::CreatCage()
{
CCage* pCage = new CCage;
cout << "请输入笼子的ID(ID>0)" << endl;
int id = 0;
cin >> id;
while (id <= 0) {
cout << "输入的ID不合法,重新输入" << endl;
cin >> id;
}
pCage->InitCage(id);
return pCage;
}
void CPeople::PushAniToCage(CCage* pCage, CAnimal* pAni)
{
if (!pCage || !pAni) {
cout << "参数不合法" << endl;
return;
}
pCage->PushAnimal(pAni);
cout << "动物成功的添加到笼子中" << endl;
}
void CPeople::PushCageToHouse(CHouse* pHouse, CCage* pCage)
{
if (!pHouse || !pCage) {
cout << "参数不合法" << endl;
return;
}
pHouse->PushCage(pCage);
cout << "笼子成功的添加到房子中" << endl;
}
void CPeople::KillAnimal(CHouse* pHouse, int id)
{
if (!pHouse) {
cout << "房子的指针为空" << endl;
return;
}
list<CCage*>::iterator ite = pHouse->m_lstCage.begin();
while (ite != pHouse->m_lstCage.end()) { //遍历笼子的链表
if ((*ite) && (*ite)->m_id == id) { //笼子不为空 且 匹配到了
if ((*ite)->m_pAni) { //笼子中存在着动物
delete(*ite)->m_pAni; //杀死动物
(*ite)->m_pAni = nullptr;
}
cout << "笼子中不存在动物了" << endl;
return;
}
ite++;
}
cout << "未匹配到笼子id:" << id << endl;
}
void CPeople::DestoryCage(CHouse* pHouse, int id)
{
if (!pHouse) {
cout << "房子的指针为空" << endl;
return;
}
list<CCage*>::iterator ite = pHouse->m_lstCage.begin();
while (ite != pHouse->m_lstCage.end()) { //遍历笼子的链表
if ((*ite) && (*ite)->m_id == id) { //笼子不为空 且 匹配到了
if ((*ite)->m_pAni) { //笼子中存在着动物
delete(*ite)->m_pAni; //杀死动物
(*ite)->m_pAni = nullptr;
}
delete(*ite); //销毁笼子
(*ite) = nullptr;
cout << "笼子被成功的销毁了" << endl;
return;
}
ite++;
}
cout << "未匹配到笼子id:" << id << endl;
}
void CPeople::LookAll(CHouse* pHouse) {
if (!pHouse) {
cout << "房子的指针为空" << endl;
return;
}
for (CCage* pCage: pHouse->m_lstCage) {
if (pCage) {
cout << "id : " << pCage->m_id << " , ";
if (pCage->m_pAni) {
pCage->m_pAni->ShowAni();
}
else {
cout << "笼子为空" << endl;
}
}
}
}
void CPeople::LookOne(CHouse* pHouse, int id) {
if (!pHouse) {
cout << "房子的指针为空" << endl;
return;
}
for (CCage* pCage : pHouse->m_lstCage) {
if (pCage && pCage->m_id == id) {
cout << "id : " << pCage->m_id << " , ";
if (pCage->m_pAni) {
pCage->m_pAni->ShowAni();
}
else {
cout << "笼子为空" << endl;
}
return;
}
}
cout << "未匹配到笼子id:" << id << endl;
}
void CPeople::PlayWithAnimal(CHouse* pHouse, int id) {
if (!pHouse) {
cout << "房子的指针为空" << endl;
return;
}
for (CCage* pCage : pHouse->m_lstCage) {
if (pCage && pCage->m_id == id) {
cout << "准备去玩耍..." << endl;
if (pCage->m_pAni) {
pCage->m_pAni->PlayAni();
}
else {
cout << "自己玩耍" << endl;
}
return;
}
}
cout << "未匹配到笼子id:" << id << endl;
}
void CPeople::showPeople() {
cout << "name : " << m_name << ", money : " << m_money << endl;
}
小贴士
//config.h
#pragma once
enum class ani_kind {
ani_cat,
ani_dog,
ani_sna
};
#define DOG_PRICE 100
#define CAT_PRICE 80
#define SNA_PRICE 120
#define PEO_MONEY 1200
#define CMD_BUY_CAT "buy cat"
#define CMD_BUY_DOG "buy dog"
#define CMD_BUY_SNA "buy sna"
#define CMD_KILL_ANI "kill"
#define CMD_DESTORY_ANI "destory"
#define CMD_LOOK_ONE "look one"
#define CMD_LOOK_ALL "look all"
#define CMD_PLAY "play"
#define CMD_SHOW "show"
#define CMD_QUIT "quit"
#define CMD_HELP "help"
控制台主函数
#include <iostream>
using namespace std;
#include "CPeople.h"
#include "CCat.h"
#include "CDog.h"
#include "CSnake.h"
#include <windows.h>
void help() {
cout << "*********宠物小屋 by wangj 2023.4.30*************************************************" << endl;
cout << "*" << CMD_BUY_CAT << "买猫 *********************************************************" << endl;
cout << "*" << CMD_BUY_DOG << "买猫 *********************************************************" << endl;
cout << "*" << CMD_BUY_SNA << "买蛇 *********************************************************" << endl;
cout << "*" << CMD_KILL_ANI << "杀死动物 *********************************************************" << endl;
cout << "*" << CMD_DESTORY_ANI << "销毁笼子(如果有动物,一并销毁) *********************************************************" << endl;
cout << "*" << CMD_LOOK_ONE << "查看某个动物 *********************************************************" << endl;
cout << "*" << CMD_LOOK_ALL << "查看所有动物 *********************************************************" << endl;
cout << "*" << CMD_PLAY << "和某个动物玩耍 *********************************************************" << endl;
cout << "*" << CMD_SHOW << "显示人物信息 *********************************************************" << endl;
cout << "*" << CMD_QUIT << "程序退出 *********************************************************" << endl;
cout << "*" << CMD_HELP << "命令帮助 *********************************************************" << endl;
cout << "**********************************************************" << endl;
}
#define BUY_ANIMAL(ANI_CLASS,ANI_KIND)\
pAni = new ANI_CLASS;\
cout << "请输入动物的名字、颜色、年龄" << endl;\
cin >> name >> color >> age;\
pAni->initAni(name, color, age);\
if (!pPeo->BuyAnimal(pAni, ANI_KIND)) {\
delete pAni;\
pAni = nullptr;\
}
int main() {
CPeople* pPeo = new CPeople;
pPeo->InitPeople("张三", PEO_MONEY);
CHouse* pHouse = new CHouse;
//-------------------------------------------------------
string cmd;
string Return;
help();
while (1) {
//cout << "请输入命令" << endl;
//cin >> cmd;
getline(cin, cmd); //读取一行(遇到空格不停止)
//cin.sync();
//cin.clear();
//判断输入的命令是否是买动物
string name;
string color;
int age = 0;
CAnimal* pAni = nullptr;
if (cmd == CMD_BUY_CAT) {
//pAni = new CCat;
//cout << "请输入动物的名字、颜色、年龄" << endl;
//cin >> name >> color >> age;
//pAni->initAni(name, color, age);
//if (!pPeo->BuyAnimal(pAni, ani_cat)) {//买动物如果失败,将new的动物回收
// delete pAni;
// pAni = nullptr;
//}
BUY_ANIMAL(CCat, ani_kind::ani_cat)
}
else if (cmd == CMD_BUY_DOG) {
BUY_ANIMAL(CDog, ani_kind::ani_dog)
}
else if (cmd == CMD_BUY_SNA) {
BUY_ANIMAL(CSnake, ani_kind::ani_sna)
}
if (pAni) { //动物买成功了,建造笼子装动物,装房子
CCage* pCage = pPeo->CreatCage(); //建造笼子
pPeo->PushAniToCage(pCage, pAni); //装动物
pPeo->PushCageToHouse(pHouse, pCage);//装房子
}
if (CMD_KILL_ANI == cmd) {
int id = 0;
cout << "请输入要杀死动物的笼子id" << endl;
cin >> id;
pPeo->KillAnimal(pHouse, id);
}
if (CMD_DESTORY_ANI == cmd) {
int id = 0;
cout << "请输入要销毁的笼子id" << endl;
cin >> id;
pPeo->DestoryCage(pHouse, id);
}
if (CMD_LOOK_ONE == cmd) {
int id = 0;
cout << "请输入要查看的笼子的id" << endl;
cin >> id;
pPeo->LookOne(pHouse,id);
}
if (CMD_LOOK_ALL == cmd) {
pPeo->LookAll(pHouse);
}
if (CMD_PLAY == cmd) {
int id = 0;
cout << "请输入要玩耍的动物的笼子" << endl;
cin >> id;
pPeo->PlayWithAnimal(pHouse, id);
}
if (CMD_SHOW == cmd) {
pPeo->showPeople();
}
if (CMD_QUIT == cmd) {
cout << "程序即将退出..." << endl;
Sleep(2000);
break;
}
if (CMD_HELP == cmd) {
help();
}
getline(cin, Return);
}
//-------------------------------------------------------
delete pHouse;
pHouse = nullptr;
delete pPeo;
pPeo = nullptr;
return 0;
}