创建狼类和羊类,包括其访问控制,符合待抽象的问题。模拟抽象狼和羊两种动物,设计抽象出应有的成员变量,如动物的一些生命特征。然后针对狼吃羊这种依赖关系,在狼类里成员函数实现狼吃羊的功能,并根据狼和羊的生命状态进行设计,生成出狼和羊的对象来实现这一功能。
类图:
羊类:(以下所有代码分为.h文件和.cpp文件,红色标记为依赖关系的体现)
1、.h
#include<string>
class Sheep
{
public:
static int m_sumSheep;//记录羊数
Sheep();
~Sheep();
void setSheep(int age, double health);
int getage();
double gethealth();
private:
int m_Yage;
double m_Yhealth;
};
2、.cpp
#include"Sheep.h"
#include<iostream>
using namespace std;
Sheep::Sheep()
{
m_Yage = 0;
m_Yhealth = 0;
}
void Sheep::setSheep(int age, double health)
{
m_Yage = age;
m_Yhealth = health;
m_sumSheep++;
}
Sheep::~Sheep()
{
m_sumSheep--;//羊类对象撤销时,羊数减一
}
int Sheep::getage()
{
return m_Yage;
}
double Sheep::gethealth()
{
return m_Yhealth;
}
狼类:
1、.h文件
#include<string>
#include"Sheep.h"
using namespace std;
class Wolf
{
public:
Wolf();
Wolf(int lage, double lhealth, double m_distance);
~Wolf();
void eat(Sheep&a);
double judge(Sheep&a);
void result(double Yhealth);
private:
string m_descrition;
int m_age;
Sheep p1;
double m_health;
double m_distance;
};
2、.cpp文件
#include "Wolf.h"
#include "math.h"
#include"Sheep.h"
#include<iostream>
using namespace std;
//无参构造函数,给二者清零
Wolf::Wolf()
{
m_age = 0;
m_health = 0;
m_distance = 0;
}
//含参构造函数
Wolf::Wolf(int lage, double lhealth, double ldistance)
{
m_age = lage;
m_health = lhealth;
m_distance = ldistance;
}
Wolf::~Wolf()
{
;
}
double Wolf::judge(Sheep&a)
{
double Yhealth;
if (m_age <= 15 && m_age >= 5 && a.getage() >= 5)
{
Yhealth = a.gethealth() - m_health * (1 / m_distance) - a.getage();//此处可随意定义生命值的多少算法,同理下方距离计算也可随意定义
}
else
{
Yhealth = 5;
}
return Yhealth;
}
void Wolf::eat(Sheep&a)
{
if (m_distance > 20)
{
cout << "羊和狼的距离太远,狼无法抓住羊" << endl;
}
else if (m_age <=15 &&m_age>=5&& a.getage()>= 5)
{
judge(a);
}
else if (m_age > 15 && a.getage() < 5)
{
cout << "该羊太小不会被吃且该狼太老抓不到羊" << endl;
}
else if (a.getage() < 5)
{
cout << "该羊太小不会被吃" << endl;
}
else if (m_age > 15)
{
cout << "该狼太老抓不到羊" << endl;
}
else if (m_age <5)
{
cout << "该狼太小抓不到羊" << endl;
}
}
void Wolf::result(double Yhealth)
{
if (Yhealth > 0)
{
cout << "该狼无法吃掉该羊" << endl;
}
else
{
cout << "该羊被该狼残忍杀害" << endl;
}
}
主函数:
#include "Wolf.h"
#include"Sheep.h"
#include <iostream>
using namespace std;
int Sheep::m_sumSheep = 0;//静态函数成员初始化
int main()
{
int i;
int age, lage;
double health, lhealth, ldistance;
char sex, lsex;
int a;
for (i = 1; i < 10; i++)
{
cout << "请输入狼" << i << "的年龄" << endl;
cin >> lage;
cout << "攻击力" << endl;
cin >> lhealth;
cout << "请输入羊" << i << "的年龄" << endl;
cin >> age;
cout << "生命值" << endl;
cin >> health;
cout << "请输入羊和狼的距离" << endl;
cin >> ldistance;
system("pause");
system("cls");
cout << "*********************************" << endl;
cout << "狼" << i << "的年龄是:" << lage << " " << "攻击力是:" << lhealth << endl;
cout << "羊" << i << "的年龄是:" << age << " " << "生命值是:" << health << endl;
cout << "羊" << i << "和狼" << i << "之间的距离为:" << ldistance<<"米" << endl;
Wolf wss;
Wolf wss1(lage, lhealth, ldistance);
Sheep p1;
p1.setSheep(age, health);
wss1.eat(p1);
double w = wss1.judge(p1);
wss1.result(w);
wss1.judge(p1);
cout << "*********************************" << endl;
system("pause");
system("cls");
cout << endl;
cout << "是否继续?若继续请按1,退出请按0" << endl;
cin >> a;
if (a == 1)
{
continue;
}
else if (a == 0)
{
break;
}
}
return 0;
}