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 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)