Ustinian709 2022-11-06 16:16 采纳率: 87.5%
浏览 32
已结题

不知道怎么改,求指导

img

#include
#include
using namespace std;
class Animal
{
public:
virtual void Identify()
{
cout<<"I am an animal:"<<name<<endl;
}
Animal(string a)
{
name=a;
}
protected:
string name;
};
class Cat:public Animal
{
public:
Cat(string a):Animal(a) {};
virtual void Identify()
{
cout<<"I am a cat:"<<name<<endl;
}
};
class Dog:public Animal
{
public:
Dog(string a):Animal(a) {};
virtual void Identify()
{
cout<<"I am a dog:"<<name<<endl;
}
};
class Tiger:public Cat
{
public:
Tiger(string a):Cat(a) {};
virtual void Identify()
{
cout<<"I am a cat:"<<name<<endl;
}
};
class Zoo
{
public:
Zoo(int max); //max为动物园最多能圈养动物数
~Zoo(){ delete residents; }
void Accept(Animal *d); //接受动物
void ListAnimal(); //显示动物园所有的动物
private:
int max_animals; //动物园能圈养的动物数
int num_animals; //动物园动物园当前圈养的动物数
Animal **residents; //指向动物园圈养的动物对象的指针数组
};
Zoo::Zoo(int max)
{
max_animals=max;
num_animals=0;
}
void Zoo::Accept(Animal *d)
{
if(num_animals<max_animals)
{
*residents[num_animals]=*d;
num_animals++;
}
else
{
cout<<"该动物园已满员。"<<endl;
}
}
void Zoo::ListAnimal()
{
if(num_animals==0)
{
cout<<"该动物园里没有动物。"<<endl;
}
cout<<"该动物园里有动物:"<<endl;
for(int i=0;i<num_animals;i++)
{
*residents[i]->Identify();
cout<<endl;
}
}
int main()
{
Zoo shanghai(3);
Animal *p;
Cat c("汤姆猫");
p=&c;
p->Identify();
shanghai.Accept(p);
shanghai.ListAnimal();
Dog d("汪汪狗");
p=&d;
p->Identify();
shanghai.Accept(p);
shanghai.ListAnimal();
Tiger t("贝乐虎");
p=&t;
p->Identify();
shanghai.Accept(p);
shanghai.ListAnimal();
return 0;

}

  • 写回答

1条回答 默认 最新

  • yun6853992 2022-11-06 17:23
    关注

    问题在这里:

    img

    你使用的时二维指针,但是没有给指针做初始化啊,二维指针要先给内部指针做初始化,再给外部指针做初始化,
    你想你定义一个指针类型,不malloc申请内存就去用一样的逻辑,或者你这里先换成二维数组用,再试这个
    可以思考一下,有问题再问,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月16日
  • 已采纳回答 11月8日
  • 创建了问题 11月6日

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗