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();
};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值