首页 > 分享 > C++宠物小屋实例

C++宠物小屋实例

宠物小屋 - 需求
动物(Animal):猫 (Cat)、狗(Dog)、蛇(Snake)

包含名字(strName)、颜色(strColor)。
笼子类(Cage):负责装动物,每个笼子的编号不能相同,一个笼子只装一个动物。

房子类(House):负责存放装动物的笼子。

安妮(Anna):负责花钱买动物,造笼子,将动物放在笼子中,存放到房子里,查看某个或所有笼子动物,和某个动物玩耍。

Animal.h

#pragma once

#include<iostream>

#include<string>

using namespace std;

class Animal

{

private:

string strName;

string strColor;

public:

Animal();

~Animal();

public:

virtual void Play() = 0;

virtual void Say() = 0;

void Show();

void Init(string strName, string strColor);

};

Animal.cpp

#include "Animal.h"

Animal::Animal() {

strName="";

strColor="";

}

Animal::~Animal(){}

void Animal::Init(string strName, string strColor) {

this->strName = strName;

this->strColor = strColor;

};

void Animal::Show() {

cout << "name:" << strName << "color:" << strColor;

this->Say();

}

Anne.h

#pragma once

#include"House.h"

class Anne

{

public:

void BuyCage(House& house,int nCount);

void PushAnimal(House& house, Animal* pAnimal);

void PopAnimal(House& house, int nId);

void See(House& house);

void PlayWithAnimal(House& house, int nId);

};

Anne.cpp

#include "Anne.h"

#include "House.h"

#include"Animal.h"

#include<list>

void Anne::BuyCage(House& house, int nCount){

for (int i = 0; i < nCount; i++) {

Cage* pCage = new Cage;

pCage->nId = (int)house.lstCage.size() + 1;

pCage->panimal = NULL;

house.lstCage.push_back(pCage);

}

};

void Anne::PushAnimal(House& house, Animal* panimal){

list<Cage*>::iterator ite = house.lstCage.begin();

while (ite != house.lstCage.end()) {

if ((*ite)->panimal == NULL) {

(*ite)->panimal = panimal;

return;

}

ite++;

}

cout << "没有地方了,买笼子" << endl;

}

void Anne::PopAnimal(House& house, int nId){

list<Cage*>::iterator ite = house.lstCage.begin();

while (ite != house.lstCage.end()) {

if ((*ite)->nId == nId) {

if ((*ite)->panimal!=NULL) {

delete(*ite)->panimal;

(*ite)->panimal = NULL;

}

else {

cout << "编号位{"<<nId<<"}笼子无小动物" << endl;

}

}

ite++;

}

cout << "没有找到编号的小动物" << endl;

};

void Anne::See(House& house){

list<Cage*>::iterator ite = house.lstCage.begin();

while (ite != house.lstCage.end()) {

if ((*ite)->panimal!= NULL) {

(*ite)->panimal->Show();

}

else {

cout << "------" << endl;

}

ite++;

}

cout << "----------" << endl;

};

void Anne::PlayWithAnimal(House& house, int nId){

list<Cage*>::iterator ite = house.lstCage.begin();

while (ite != house.lstCage.end()) {

if ((*ite)->nId== nId) {

if ((*ite)->panimal != NULL) {

(*ite)->panimal->Play();

cout << "开心" << endl;

}

}

else {

cout << "不开心" << endl;

}

ite++;

}

}

Cage.h

#pragma once

#include "Animal.h"

using namespace std;

class Cage

{

public:

Animal* panimal;

int nId;

public:

Cage();

~Cage();

};

Cage.cpp

#include "Cage.h"

Cage::Cage() {

panimal=NULL;

nId=0;

};

Cage::~Cage() {

};

Cat.h

#include<string>

using namespace std;

class Cat :public Animal

{

public:

Cat();

~Cat();

public:

virtual void Play();

virtual void Say();

};

Cat.cpp

#include "Cat.h"

#include<iostream>

#include<string>

using namespace std;

Cat::Cat() {}

Cat::~Cat() {}

void Cat::Play()

{

cout << "Cat Play!" << endl;

}

void Cat::Say()

{

cout << "Miao!" << endl;

}

Dog.h

#include "Animal.h"

#include<iostream>

#include<string>

using namespace std;

class Dog :public Animal

{

public:

Dog();

~Dog();

public:

virtual void Play();

virtual void Say();

};

Dog.cpp

#include "Dog.h"

#include<iostream>

#include<string>

using namespace std;

Dog::Dog() {}

Dog::~Dog() {}

void Dog::Play()

{

cout << "Dog Play!" << endl;

}

void Dog::Say()

{

cout << "Wang!" << endl;

}

House.h

#pragma once

#include<list>

#include "Cage.h"

using namespace std;

class House

{

public:

list<Cage*> lstCage;

public:

House();

~House();

};

House.cpp

#include "House.h"

House::House() {};

House::~House() {

list<Cage*>::iterator ite = lstCage.begin();

while (ite != lstCage.end())

{

delete (*ite);

ite++;

}

}

Snake.h

#pragma once

#include "Animal.h"

#include<iostream>

#include<string>

using namespace std;

class Snake :public Animal

{

public:

Snake();

~Snake();

public:

virtual void Play();

virtual void Say();

};

Snake.cpp

#pragma once

#include "Animal.h"

#include<iostream>

#include<string>

using namespace std;

class Snake :public Animal

{

public:

Snake();

~Snake();

public:

virtual void Play();

virtual void Say();

};

相关知识

C++语言学习笔记8:宠物小屋
a=b++,c++和a=(b++,c++)的区别
C++动物运动会源代码资源
C++课程作业之 宠物类的创建
Python3 实例
宠物小屋管理系统 宠物小屋内共有12个笼子,每个笼子内可放不同的小动物,如猫、狗、鹦鹦等,但同一时
C++ 安妮的宠物小屋 练习
C++中+= 是什么意思
C++程序设计(上)练习
C++第二天

网址: C++宠物小屋实例 https://m.mcbbbk.com/newsview536282.html

所属分类:萌宠日常
上一篇: 一种智能宠物笼的设计与试验论文.
下一篇: 宠物健康必备——消化酵素(从宠物