栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++:结合容器多态继承的(宠物小屋)实例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++:结合容器多态继承的(宠物小屋)实例

目录

一、题目要求:

二、自我理解题意

三、类视图详解

 四、代码

1.主函数

2.头文件

3.源文件

五、总结

一、题目要求

Anne的宠物小屋有若干个笼子,每个笼子可以放不同的动物,但只能放1只或0只,包括:猫Cat、狗Dog、蛇Snake要求:
1)实现一个简单的管理系统。包括空笼子),买一些笼子,带着动物出去玩。

二、自我理解题意

动物类:猫(Cat)、狗(Dog)、蛇(Snake)。(每个动物各是一个类)

包含名字  颜色。

笼子类:Cage负责装动物,每个笼子的编号不能相同,一个笼子只装一个动物,与动物类为关联关系。

包含Animal* pAnimal

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

包含了链表容器。

人类:Anne 相当于控制类  (在这里我们没有做单例模式),负责花钱买动物,造笼子,将动物放在笼子中,存放到房子里,杀死某个笼子动物,销毁某个笼子(如果有动物一并销毁),查看某个或所有笼子动物,和某个动物玩耍。

三、类视图详解

 四、代码

1.主函数
#include "Cat.h"
#include "Dog.h"
#include "Snake.h"
#include "House.h"
#include "Anne.h"
#include "Anne.h"

int main()
{

	Cat c;
	c.Init("小猫", "黄色", "母");
	c.Show();
	c.Play();

	//Dog* d = new Dog;
	//d->Init("小狗", "黑色", "公");
	//d->Show();
	//d->Play();


	//

	//Cage* pCage = new Cage;
	//pCage->pAnimal = d;
	//House house;
	//house.lstcage.push_back(pCage);

	Cat* pCat = new Cat;
	pCat->Init("小白", "白色", "母");

	Dog* pDog = new Dog;
	pDog->Init("小黑","黑色","公");

	Snake* pSnake = new Snake;
	pSnake->Init("小青","绿色","母");

	House house;

	Anne anne;
	anne.BuyCage(house, 5);
	


	anne.PushAnimal(house, 1, pCat);
	anne.PushAnimal(house, 2, pDog);
	anne.PushAnimal(house, 4, pSnake);

	anne.PopAnimal(house, 1);

	anne.See(house);
	anne.PopAnimal(house, 1);

	anne.PlayWithAnimal(house, 1);
	anne.PlayWithAnimal(house, 4);
	anne.PlayWithAnimal(house, 11);
	return 0;
}

2.头文件
#pragma once
#include 
#include 
using namespace std;


class Animal
{
private:
	string strName;
	string strColor;
	string strSex;
public:
	Animal();
public:
	virtual void Play() = 0;
	void Init(string strName, string strColor, string strSex);
	void Show();
};
#pragma once
#include "House.h"
class Anne
{
public:
	void BuyCage(House& house, int nCount);
	void PushAnimal(House& house, int nId, Animal* pAnimal);
	void PopAnimal(House& house, int nId);
	void See(House& house);
	void PlayWithAnimal(House& house, int nId);
};

#pragma once
#include "Animal.h"
class Cage
{
public:
	Animal* pAnimal;
	int Id;
public:
	Cage();
	virtual ~Cage();
};

#pragma once
#include "Animal.h"
class Cat :
    public Animal
{
public:
    virtual void Play();
};

#pragma once
#include "Animal.h"
class Dog :
    public Animal
{
public:
    virtual void Play();
};

#pragma once
#include "Cage.h"
#include 

class House
{
public:
	listlstcage;
public:
	virtual ~House();
};

#pragma once
#include "Animal.h"
class Snake :
    public Animal
{
public:
    virtual void Play();
};

3.源文件
#include "Animal.h"


Animal::Animal()
{
	strName = "";
	strColor = "";
	strSex = "";
}

void Animal::Init(string strName, string strColor, string strSex)
{
	this->strName = strName;
	this->strColor = strColor;
	this->strSex = strSex;
}
void Animal::Show()
{
	cout << strName << "  " << strColor << "  " << strSex << "  " << endl;
}
#include "Anne.h"
#include "House.h"

void Anne::BuyCage(House& house, int nCount)
{
	
	for (int i = 0; i < nCount; i++)
	{
		//根据买的数量创建笼子
		 Cage* pCage = new Cage;
		 //编号自动生成
		 pCage->Id = house.lstcage.size() + 1;
		 pCage->pAnimal = nullptr;
		//添加到房子的链表中
		 house.lstcage.push_back(pCage);
	}
	

}
void Anne::PushAnimal(House& house, int nId, Animal* pAnimal)
{
	//找到指定房间编号的笼子
	
	//判断笼子中是否有动物
		//没有 放入
		
		// 有  提示
	list::iterator ite = house.lstcage.begin();
	while (ite != house.lstcage.end())
	{
		if ((*ite)->Id == nId)
		{
			if ((*ite)->pAnimal == nullptr)
			{
				(*ite)->pAnimal = pAnimal;
			}
			else
			{
				cout << "编号为【" << nId << "】的笼子有动物了" << endl;
			}
			return;
		}
		ite++;
	}
	cout << "没有找到编号为" << nId << "的笼子" << endl;
}
void Anne::PopAnimal(House& house, int nId)
{
	//找到指定房间编号的笼子

	//判断笼子中是否有动物
		//没有 提示
		
		// 有  提删除
	list::iterator ite = house.lstcage.begin();
	while (ite != house.lstcage.end())
	{
		if ((*ite)->Id == nId)
		{
			if ((*ite)->pAnimal != nullptr)
			{
				delete (*ite)->pAnimal;
				(*ite)->pAnimal = nullptr;
			}
			else
			{
				cout << "编号为【" << nId << "】的笼子没有动物" << endl;
			}
			return;
		}
		ite++;
	}
	cout << "没有找到编号为" << nId << "的笼子" << endl;
}
void Anne::See(House& house)
{
	//遍历房间中笼子
	
	//有动物 Show

	//没有动物  提示
	list::iterator ite = house.lstcage.begin();
	while (ite != house.lstcage.end())
	{
		if ((*ite)->pAnimal != nullptr)
		{
			(*ite)->pAnimal->Show();
		
		}
		else
		{
			cout << "----NULL----" << endl;
		}
		cout << "-----------------------------------------" << endl;
		ite++;
	}
	
}
void Anne::PlayWithAnimal(House& house, int nId)
{
	//找到指定房间编号的笼子

	//判断笼子中是否有动物
		//有  

		//没有
	list::iterator ite = house.lstcage.begin();
	while (ite != house.lstcage.end())
	{
		if ((*ite)->Id == nId)
		{
			if ((*ite)->pAnimal != nullptr)
			{
				(*ite)->pAnimal->Play();
				cout << "跟小动物玩真开心" << endl;
			}

			else
			{
				cout << "编号为【" << nId << "】的笼子里没有小动物" << endl;
				cout << "自己玩不开心" << endl;
			}
			return;
		}
		ite++;
	}
	cout << "没有找到编号为" << nId << "的笼子" << endl;
	cout << "自己玩不开心" << endl;
}
#include "Cage.h"

Cage::Cage()
{
	pAnimal = nullptr;
	Id = 0;
}
Cage::~Cage()
{
	if (pAnimal != nullptr)
	{
		delete pAnimal;
		pAnimal = nullptr;
	}
}
#include "Cat.h"

void Cat::Play()
{
	cout << "玩毛球" << endl;
}
#include "Dog.h"




void Dog::Play()
{
	cout << "接飞盘" << endl;
}
#include "House.h"



House:: ~House()
{
	list::iterator ite = lstcage.begin();
	while (ite != lstcage.end())
	{
		delete* ite;
		ite++;
	}
}
#include "Snake.h"


void Snake::Play()
{
	cout << "伸舌头" << endl;
}

五、总结

通过此次实例更加熟悉的类图整个框架的制作以及4个横向关系(组合、依赖、聚合,关联)和继承的使用,并且熟悉掌握了纯虚函数的用法,对于多态的深入理解,最后知道了容器的使用方法和迭代器的使用。要熟练掌握他们之间的逻辑结构。例如每个容器都有专属的迭代器,而算法通过迭代器对容器中的元素进行操作,容器能够通过模版的方法,装下各种类型的节点元素。

所以迭代器是一种行为类似指针的对象。迭代器所指向的对象为容器中元素(结构体)的节点。

如定义一个 class   List  的容器 ,容器的节点为ListItem,那么迭代器就应该指向(的对象)容器中的ListItem节点。

而迭代器通过重载 * (解引用)运算符,从而就可以得到节点的值。通过重载operator -> 运算符就可以得到容器节点地址(指针)。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011985.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号