64,642
社区成员
发帖
与我相关
我的任务
分享
下面这段代码必须有的,老师给的
class Zoo
{
public: Zoo(int max);
~Zoo()
{delete residents; }
int Accept(Animal *a);
void ListAnimals();
private: int maxAnimals; //动物园能最多圈养的动物数
int numAnimals; //动物园当前圈养的动物数
Animal **residents; // 指向动物园圈养的动物对象的指针数组
};
error C2440: 'initializing' : cannot convert from 'class Dog *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
执行 cl.exe 时出错.
还有我对为什么&D1,如果这样是不是没有接收其他类比如老虎,猫,能在帮忙看看吗// 构造函数
Zoo::Zoo(int max)
{
numAnimals = 0; // 原始动物数
maxAnimals = max; // 初始化最大圈养动物数
residents = new Animal*[max]; // 申请指针数据
}
主函数增加Accept函数的调用:
int main()
{
Zoo z(100);
Dog D1("小黄");
Tiger T1("小黑");
Cat C1("小咪");
z.Accept(&D1);
z.ListAnimals();
return 0;
}
residents = new Animal*[max];
这个我写错了#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Animal{ //Animal类
public:
Animal(){}
Animal(string name1)
{name=name1;}
virtual void identify()=0;
protected:
string name;
};
class Cat:public Animal{ //Cat类
public:
Cat(string name):Animal(name)
{}
void identify()
{cout<<setw(8)<<"Cat"<<name<<endl;}
};
class Dog:public Animal{ //Dog类
public:
Dog(string name):Animal(name)
{}
void identify()
{cout<<setw(8)<<"Dog"<<name<<endl;}
};
class Tiger:public Cat{ //Tigre类,父类为Cat
public:
Tiger(string name):Cat(name)
{}
void identify()
{cout<<setw(8)<<"Tiger"<<name<<endl;}
};
class Zoo
{
public:
Zoo(int max);
~Zoo()
{delete residents; }
int Accept(Animal *a);
void ListAnimals();
private:
int maxAnimals; //动物园能最多圈养的动物数
int numAnimals; //动物园当前圈养的动物数
Animal **residents; // 指向动物园圈养的动物对象的指针数组
};
// 构造函数
Zoo::Zoo(int max)
{
maxAnimals = max; // 初始化最大圈养动物数
residents = new Animal[max]; // 申请指针数据
}
int Zoo::Accept(Animal *a)
{
if (numAnimals == maxAnimals) // 判断是否已经到大最大圈养数
{
return -1;
}
residents[numAnimals] = a;
numAnimals++;
return 0;
}
void Zoo::ListAnimals()
{
for (int i=0; i<numAnimals; i++)
{
residents[i]->identify(); // 调用动物类中描述函数
}
}
int main()
{
Zoo z(100);
Dog D1("小黄");
Tiger T1("小黑");
Cat C1("小咪");
z.ListAnimals();
return 0;
}
class Zoo
{
public:
Zoo(int max);
~Zoo()
{
delete residents;
}
int Accept(Animal *a);
void ListAnimals();
private:
int maxAnimals; //动物园能最多圈养的动物数
int numAnimals; //动物园当前圈养的动物数
Animal **residents; // 指向动物园圈养的动物对象的指针数组
};
// 构造函数
Zoo::Zoo(int max)
{
maxAnimals = max; // 初始化最大圈养动物数
residents = new Animal[max]; // 申请指针数据
}
int Zoo::Accept(Animal *a)
{
if (numAnimals == maxAnimals) // 判断是否已经到大最大圈养数
{
return -1;
}
residents[numAnimals] = a;
numAnimals++;
return 0;
}
void Zoo::ListAnimals()
{
for (int i=0; i<numAnimals; i++)
{
residents[i]->identify(); // 调用动物类中描述函数
}
}