基类:
class human{
public:
int getHumanType{ return m_iType; }
vitual printHumanType() {};
private:
int m_iType;
};
子类:
class yellowHuman: public human{
public:
vitual printHumanType()
{
cout << "it's yellow human\n";
}
};
class whiteHuman: public human{
public:
vitual printHumanType()
{
cout << "it's white Human\n";
}
};
class blackHuman: public human{
public:
vitual printHumanType()
{
cout << "it's blackHuman\n";
}
};
// 应用
int main()
{
human* humanTest[3];
humanTest[0]= new yellowHuman;
humanTest[1]= new whiteHuman;
humanTest[2]= new blackHuman;
for(int i=0;i<3;i++)
{
humanTest[i]->printHumanType();
}
return 0;
}
结果:
it's yellow human
it's white Human
it's blackHuman
至于为什么会这么输出,请详细研究C++ virtual相关内容
评论