利用继承与派生创造一个鹰头马身有翼兽类

利用继承与派生的知识创造一个鹰头马身有翼兽类(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;
//}

运行结果截图:
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值