bear七 2022-05-09 23:31 采纳率: 83.3%
浏览 81
已结题

c++代码问题继承与派生

#include<iostream>
using namespace std;
class Animal{
    protected:
        unsigned int Age;double Weight;string Name;
    public:
        Animal(unsigned int age,double weight,string name)
        {
            Age=age;
            Weight=weight; 
            Name=name;
        }
        virtual void Animal()
        {
            cout << "Animal Constructor" << endl;
        }
};
class Fish:virtual public Animal{
    protected:
        bool Fim;
    public:
        Fish(unsigned int age,double weight,string name,bool Fim):Animal(age,weight,name)
        {
            this->Fim=Fim;
        }
        virtual void Fish()
        {
           cout << "Fish Constructor" << endl;
        }
};
class TerrestrialAnimal:virtual public Animal{
    protected:
        unsigned int Legs;
    public:
        TerrestrialAnimal(unsigned int age,double weight,string name,unsigned Leg):Animal(age,weight,name)
        {
            Legs=Leg;
        }
        virtual void TerrestrialAnimal()
        {
            cout << "TerrestrialAnimal Constructor" << endl;
        }
};
class Reptile:public Fish,public TerrestrialAnimal{
    public:
        Reptile(unsigned int age,double weight,string name,unsigned int Leg,bool Fim):Animal(age,weight,name),Fish(age,weight,name,Fim),TerrestrialAnimal(age,weight,name,Leg)
        {}
    virtual void Reptile()
    {
        cout << "Reptile Constructor" << endl;
    }    
    void Reptile::Print()
    {
        cout << "Reptile Age:" << Age << endl;
    }
};
int main(){
    int age;
    int weight;
    string name;
    int fim_num;
    int leg_num;
    cin >> age >> weight >> name >> fim_num >> leg_num;
    Reptile r1(age, weight, name, fim_num, leg_num);
    r1.Print();
    return 0;
}
        

代码出错了,请问一下怎么改
原本的代码要求如下
定义一个动物 (Animal) 基类,具有 Age、Weight、name 等数据成员,由 Animal 类公有派生出鱼 (Fish) 类、陆地动物 (TerrestrialAnimal) 类。 Fish 类有鳍 (Fim) 属性, TerrestrialAnimal 类有腿 (Leg) 属性。从 Fish 和 TerrestrialAnimal 公有派生出爬行动 物 (Reptile) 类。

在继承过程中,把 Animal 设置为虚基类。
Animal类的构造函数中输出信息:cout << "Animal Constructor" << endl;
Fish类的构造函数中输出信息:cout << "Fish Constructor" << endl;
TerrestrialAnimal类的构造函数中输出信息:cout << "TerrestrialAnimal Constructor" << endl;
Reptile类的构造函数中输出信息:cout << "Reptile Constructor" << endl;
Reptile类包含如下成员函数:
void Reptile::Print(){
cout << "Reptile Age:" << Age << endl;
}

  • 写回答

1条回答 默认 最新

  • SmallAntJ 2022-05-10 00:31
    关注

    类的构造函数不能是虚函数,而且没有返回值。你写了两种构造函数,带参数的构造和默认构造。默认构造函数要写成:

    Animal()
    {
        cout << "Animal Constructor" << endl;
    }
    

    然后,题目要求构造函数要有输出,所以带参数的构造函数应该也需要输出:

    Animal(unsigned int age,double weight,string name)
            {
                Age=age;
                Weight=weight; 
                Name=name;
                cout << "Animal Constructor" << endl;
            }
    

    按这样把所有类都改一下。
    还有就是52行,在类里实现成员函数的时候不需要 :: 操作符,在类中声明类外实现才需要加,可以直接写成

        void Print()
        {
            cout << "Reptile Age:" << Age << endl;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月10日
  • 已采纳回答 5月10日
  • 创建了问题 5月9日

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码