利用继承与派生的知识创造一个鹰头马身有翼兽类(class CHippogriff),其父类有马类(CHorse),鸟类(CBird),狮子类(CLion),要求在解题思路中绘制UML图。马类(CHorse)和鸟类(CBird)的要求与课堂内容一致,狮子类(CLion)要求有私有数据成员Mane(鬃毛),如果有鬃毛就是雄狮,无则为雌狮,雄狮负责保卫领地Fight(),雌狮负责打猎Hunt()。鹰头马身有翼兽有自己的保护数据成员AP(法术输出,默认为500),Talk()为狮子的Talk(),Show()展示马类(CHorse),鸟类(CBird),狮子类(CLion)的信息,并能打印出自己的法术输出值。
主函数I:
int main()
{
CHippogriff Hippogriff((char*)"Hippogriff", 5, 800, 2, 10000, 1, 1000);
Hippogriff.Show();
Hippogriff.Talk();
return 0;
}
运行结果I:
主函数II:
int main(){
CHippogriff Hippogriff((char*)"Hippogriff", 5, 800, 2, 10000, 0);
Hippogriff.Show();
Hippogriff.Talk();
return 0;
}
运行结果II:
解题思路:
首先定义CAnimal类,接着定义CHorse、CBird和CLion类,分别继承自CAnimal类,接着定义CHippogriff类,继承自CHorse、CBird和CLion。同时添加特有的数据成员AP(法术输出)。
代码:
#include <iostream>
#include<cstring>
using namespace std;
class CAnimal{
char name[32];
int age;
int weight;
public:
CAnimal(char *strName = (char*)"",int a = 0,int w = 0)
{
strcpy(name,strName);
age = a;
weight = w;
cout<<"Animal constructor "<<name<<endl;
}
void Show()
{
cout<<name<<" "<<age<<" "<<weight<<endl;
}
~CAnimal(){cout<<"Animal destructor "<<name<<endl;}
};
class CHorse:virtual public CAnimal{
int power;
public:
CHorse(int pow = 0,char *strName = (char*)"",int a = 0,int w = 0):CAnimal(strName,a,w)
{
power = pow;
cout<<"Horse constructor"<<endl;
}
void Show()
{
CAnimal::Show();
cout<<"Power:"<<power<<endl;
}
void Run()
{
cout<<"I can run!I run because I love to!!"<<endl;
}
void Talk()
{
cout<<"Whinny!..."<<endl;
}
~CHorse(){cout<<"Horse destructor"<<endl;}
};
class CBird:virtual public CAnimal{
int wingSpan;
public:
CBird(int ws = 0,char *strName = (char*)"",int a = 0,int w = 0):CAnimal(strName,a,w)
{
wingSpan = ws;
cout<<"Brid constructor"<<endl;
}
void Show()
{
CAnimal::Show();
cout<<"wingSpan:"<<wingSpan<<endl;
}
void Fly()
{
cout<<"I can fly!I can fly!!"<<endl;
}
void Talk()
{
cout<<"Chirp..."<<endl;
}
~CBird(){cout<<"Bird destructor"<<endl;}
};
class CLion : virtual public CAnimal {
private:
bool Mane;
public:
CLion(bool hasMane = 0,char *strName = (char*)"",int a = 0,int w = 0):CAnimal(strName,a,w),Mane(hasMane)
{
Mane = hasMane;
cout<<"Lion constructor"<<endl;
}
void Show() {
if (Mane) {
cout << "This is a lion!" << endl;
} else {
cout << "This is a female lion!" << endl;
}
}
void Talk() {
if(Mane){
cout << "I'm in charge of protecting the territory!" << endl;
cout << "I won't give in, cause I'm proud of all my scars!"<< endl;
}
else{
cout << "I do the hunting !" <<endl;
cout << "I'll get you ! You can't run away!"<<endl;
}
}
~CLion()
{
cout<<"Lion destructor"<< endl;
}
};
class CHippogriff:public CLion,public CHorse,public CBird
{
protected:
int AP;
public:
CHippogriff(char *strName = (char*)"", int a = 0, int w = 0, int ws = 0, int pow = 0, bool hasMane = 0, int ap = 500)
:CAnimal(strName, a, w), CLion(hasMane,strName,a,w), CBird(ws,strName,a,w), CHorse(pow,strName,a,w), AP(ap) {
cout << "CHippogriff constructor " << endl;
}
void Show()
{
CBird::Show();
CHorse::Show();
CAnimal::Show();
CLion::Show();
cout<<"Attack Power = "<<AP<<endl;
}
void Talk()
{
CLion::Talk();
}
~CHippogriff(){cout<<"CHippogriff destructor"<<endl;}
};
int main()
{
CHippogriff Hippogriff((char*)"Hippogriff", 5, 800, 2, 10000, 1, 1000);
Hippogriff.Show();
Hippogriff.Talk();
return 0;
}
//int main(){
// CHippogriff Hippogriff((char*)"Hippogriff", 5, 800, 2, 10000, 0);
// Hippogriff.Show();
// Hippogriff.Talk();
// return 0;
//}
运行结果截图: