animal 类c++

来源:百度知道 编辑:UC知道 时间:2024/05/26 02:27:28
老师让我们写一个animal类 要求写出动物名字和种类
希望各位高手帮忙 解决一下 下面的是我自己做的 帮看看哪错了
#include <iostream.h>
class Animal{
public:
virtual void Identify()
{
cout<<"kinds."<<endl;
}
};
class Cat: public Animal{
public:
void Identify()
{
cout<<"name is cat,kinds is cat."<<endl;
}
};
class Dog: public Animal{
public:
void Identify()
{
cout<<"name is dog,kinds is dogs."<<endl;
}
};
void main()
{
Animal *ptr;
Animal g;
Cat m;
Dog d;
ptr=&g;
ptr->Identify ();
ptr=&m:
ptr->Identify ();
ptr=&d;
ptr->Inentify ();
}

我刚调试过
改动如下:
1:#include <iostream.h>改成:
#include <iostream>
using namespace std;
2:ptr=&m: 改成
ptr=&m;
3:ptr->Inentify ();改成
ptr->Identify ();

调试后编译通过 结果正确

错的地方只有两个:(1)ptr指向m时,应该是分号,你写成冒号了。ptr=&m;
(2)函数名写错,ptr->Identify(),不是Inentify。
楼上改了头文件,其实可以不用,不改也可以通过编译。不过,使用#include <iostream>
using namespace std;
更符合c++的风格。