西北农林科技大学OJ (C++)动物类的继承与派生(虚基类)

Description

设计一个动物类CAnimal和它的派生类马类CHorse、鸟类CBird,并基于马类和鸟类派多重派生出飞马类CPegasus,要求:

(1)CAnimal类中有string类型数据成员name和int类型数据成员年龄age,构造函数可实现name和age的初始化和输出,输出格式为:cout<<"Animal constructor: "<<name<<"" <<age<<endl,析构函数输出格式为:cout<< "Animal destructor "<< endl;

(2)CHorse类虚继承于CAnimal类,新增int类型数据成员马力power并调用其构造函数实现初始化,输出格式为:cout<<"Horse constructor: "<< power<<endl,新增成员函数Talk(),输出格式为:cout << "Whinny..." << endl,析构函数输出格式为:cout<< "Horse destructor" << endl;

(3)CBird类虚继承于CAnimal类,新增float类型数据成员翼展wingspan并调用其构造函数实现初始化,输出格式为:cout<<"Bird constructor: "<< wingspan<<endl,新增成员函数Talk(),输出格式为:cout<< "Chirp..."<<endl,析构函数输出格式为:cout<<"Bird destructor" << endl;

(4)CPegasus类多重继承于CHorse类和CBird类,其构造函数输出格式为:cout<<"Pegasus constructor"<<endl,重新定义void Talk(){ CHorse::Talk();}函数覆盖其直接基类中的两个Talk函数,析构函数输出格式为:cout<< "Pegasus destructor" << endl;

在main函数中,先输入派生类对象个数n,再依次输入派生类类别,其中字符'H'、'B'、'P'分别表示CHorse类、CBird类、CPegasus类,如果输入其他字符则输出"Input error! ",然后输入对应数据成员信息构造相应对象,最后依次调用n个对象的Talk成员函数实现输出。

Input

派生类对象个数n,派生类类型,数据成员初始化信息

Output

构造函数输出信息

各对象的Talk函数调用输出结果

析构函数输出信息

Sample Input 1 

5
H horse1 3 2000
B bird1 2 1.5
P pegasus1 5 10000 3.6
H horse2 4 3500
An animal1 4

Sample Output 1

Animal constructor: horse1 3
Horse constructor: 2000
Animal constructor: bird1 2
Bird constructor: 1.5
Animal constructor: pegasus1 5
Horse constructor: 10000
Bird constructor: 3.6
Pegasus constructor
Animal constructor: horse2 4
Horse constructor: 3500
Input error!
Whinny...
Chirp...
Whinny...
Whinny...
Horse destructor
Animal destructor
Bird destructor
Animal destructor
Pegasus destructor
Bird destructor
Horse destructor
Animal destructor
Horse destructor
Animal destructor

**************************************************************************************************************

代码实现

#include <iostream>
#include <string>
using namespace std;

class animal {
private:
    string name;
    int age;
public:
    animal() {}
    animal(string nam, int ag) : name(nam), age(ag) {
        cout << "Animal constructor: " << name << " " << age << endl;
    }
    virtual ~animal() {
        cout << "Animal destructor" << endl;
    }
    virtual  void talk() = 0;

};

class horse : virtual public animal {
    int power;
public:
    horse(string a, int b, int pow) : animal(a, b), power(pow) {
        cout << "Horse constructor: " << power << endl;
    }
    void talk() {
        cout << "Whinny..." << endl;
    }

    ~horse() {
        cout << "Horse destructor" << endl;
    }
};

class bird : virtual public animal {
    float wingspan;
public:

    bird(string a, int b, float c) : animal(a, b), wingspan(c) {
        cout << "Bird constructor: " << wingspan << endl;
    }
    void talk() {

        cout << "Chirp..." << endl;
    }
    ~bird() {
        cout << "Bird destructor" << endl;
    }
};

class CPegasus : public horse, public bird {
public:
    CPegasus(string a, int b, int c, float d) : animal(a, b), horse(a, b, c), bird(a, b, d) {
        cout << "Pegasus constructor" << endl;
    }
    void talk() {
        horse::talk();
    }
    ~CPegasus() {
        cout << "Pegasus destructor" << endl;
    }
};

int main() {

    string a;
    int b, m;
    float c;
    int n;
    cin >> n;
    
    int p = n;
    int k = 0;
    animal** ani = new animal * [n];
    while (n--) {
        string type;
        cin >> type;

        if (type == "H") {
            cin >> a >> b >> m;
            ani[k++] = new horse(a, b, m);
        }
        else if (type == "B") {
            cin >> a >> b >> c;

            ani[k++] = new bird(a, b, c);
        }
        else if (type == "P") {
            cin >> a >> b >> m >> c;
            ani[k++] = new CPegasus(a, b, m, c);
        }
        else {
            string sb;
            getline(cin,sb);
            cout << "Input error!" << endl;
        }
    }
    for (int i = 0; i < k; i++) {
        ani[i]->talk();

    }
    for (int i = 0; i <k; i++) {
        delete ani[i];
    }
    delete[]ani;


    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值