rap歌手:谁知道?!!!

来源:百度文库 编辑:高考问答 时间:2024/04/28 06:49:43
请问谁知道这个程序(用vc写的)为什么会输出2次animal?
#include <iostream.h>
class Animal
{
public:
Animal()
{
cout<<"animal"<<endl;
}
void eat()
{
cout<<"animal eat"<<endl;
}

void sleep()
{
cout<<"animal sleep"<<endl;
}

void breathe()
{
cout<<"animal breathe"<<endl;
}
};
class Fish: public Animal
{
public:
Fish()
{
cout<<"fish"<<endl;
}
void shit()
{
cout<<"fish shit!"<<endl;
}

};

void main()
{
Animal an;
Fish fh;
}

Fish是从Animal继承的,它的构造函数会隐式调用父类的构造函数