#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;
}