/*
定义一个基类,哺乳动物类Mammal,其中数据成员有年龄、重量、品种,成员函数有move()、speak()等,以此表示动物的行为。
由这个基类派生出狗、猫、马、猪四种哺乳动物,它们有各自的行为。编程实现使各个动物分别表现出不同的行为。 具体要求如下:
1、从基类分别派生出各种动物类,通过虚函数实现不同动物表现出的不同行为。
2、现有动物:狗: CAIRN:3岁,3kg; DORE:4岁,2kg;
猫: CAT:5 岁,4kg; 马:HORSE,5岁,60kg;
猪: PIG,2岁,45kg。
3、对应的动作中要先显示出动物的名称,然后显示年龄、重量、品种、叫声及其他特征。
4、设置一个 Mammal 类数组,设计一个屏幕菜单,选择不同的动物或不同的品种,则显示出动物相对应的动作,直到选择结束。
*/
#include
#include
using namespace std;
class Mammal //虚基类
{
protected:
string name;
int age;
int weight;
public:
Mammal(){}
Mammal(string n, int a, int w){ name = n; age = a; weight = w; }
~Mammal(){ cout << "this is destuctor" << endl; }
virtual void move() const = 0;
virtual string speak() const = 0;
virtual void setout() const = 0;
};
class Dog :public Mammal//狗
{
public:
Dog(){}
Dog(string n, int a, int w) :Mammal(name, age, weight){}
virtual void move()const
{
cout << "狗是四只脚跑的" << endl;
}
virtual string speak()const
{
return "汪汪";
}
virtual void setout()const
{
cout << "狗的信息:";
cout << "昵称:" << name << endl;
cout << "年龄:" << age << endl;
cout << "体重:" << weight << endl;
cout << "行为:"; move();
cout << "叫声:"; speak();
}
};
class Cat :public Mammal//猫
{
public:
Cat(){}
Cat(string n, int a, int w) :Mammal(name, age, weight){}
virtual void move()const
{
cout << "嘘!喵要抓老鼠咯,安静点!" << endl;
}
virtual string speak()const
{
return "喵";
}
virtual void setout()const
{
cout << "喵的信息:";
cout << "昵称:" << name << endl;
cout << "年龄:" << age << endl;
cout << "体重:" << weight << endl;
cout << "行为:"; move();
cout << "叫声:"; speak();
}
};
class Horse :public Mammal//马
{
public:
Horse(){}
Horse(string n, int a, int w) :Mammal(name, age, weight){}
virtual void move()const
{
cout << "马儿在草原上狂奔!" << endl;
}
virtual string speak()const
{
return "马鸣风萧萧!";
}
virtual void setout()const
{
cout << "马儿的信息:";
cout << "昵称:" << name << endl;
cout << "年龄:" << age << endl;
cout << "体重:" << weight << endl;
cout << "行为:"; move();
cout << "叫声:"; speak();
}
};
class Pig :public Mammal//猪
{
public:
Pig(){}
Pig(string n, int a, int w) :Mammal(name, age, weight){}
virtual void move()const
{
cout << "咱要睡觉,不要吵!" << endl;
}
virtual string speak()const
{
return "哼哼!";
}
virtual void setout()const
{
cout << "猪的信息:";
cout << "昵称:" << name << endl;
cout << "年龄:" << age << endl;
cout << "体重:" << weight << endl;
cout << "行为:"; move();
cout << "叫声:"; speak();
}
};
int main()
{
int temp;
Dog d1("Carin", 3, 3), d2("Jrerry", 4, 2);
Cat c("CAT", 5, 4);
Horse h("HORSE", 5, 60);
Pig p("PIG", 2, 45);
cout << "***************************" << endl;
cout << "* *" << endl;
cout << "*********主菜单************" << endl;
cout << "* *" << endl;
cout << "*******1、动物列表*********" << endl;
cout << "* *" << endl;
cout << "*******2、退出*************" << endl;
cout << "* *" << endl;
cout << "***************************" << endl;
cout << endl;
cout << "输入菜单序号:";
cin >> temp;
cout << endl;
if (temp == 1)
{
cout << "1、汪星人(ps:目前有两只)" << endl;
cout << " " << endl;
cout << "2、喵咪" << endl;
cout << " " << endl;
cout << "3、汗血宝马" << endl;
cout << " " << endl;
cout << "4、一直除了吃永远在睡的猪" << endl;
cout << " " << endl;
cout << "5、不想看了,退出程序了!" << endl;
cout << " " << endl;
cout << endl;
cout << "选择你要查看动物信息的编号" << endl;
cin >> temp;
switch (temp)
{
case 1:
cout << "第一只汪:" << endl;
d1.setout();
cout << "第二只汪:" << endl;
d2.setout();
case 2:
c.setout();
case 3:
h.setout();
case 4:
p.setout();
case 5:
exit(0);
default:cout << "that's an error!!! " << endl;
}
}
else
exit(0);
system("pause");
return 0;
}