新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > C++子类覆盖基类及virtual虚拟处理

C++子类覆盖基类及virtual虚拟处理

作者:时间:2016-12-01来源:网络收藏
#include
class animal
{
public:
animal()
{
cout<<"animal construct"<
}
~animal()
{
cout<<"construct animal"<
}
virtual void breath() //virtual定义虚函数
{
cout<<"bubble2"<
}
};
class fish:public animal //只调用animal中的animal()
{
public:
fish()
{
// cout<<"hello world"<
}
~fish()
{
// cout<<"construct helllo world"<
}
void breath()
{
//animal::breath();
//cout<<"fish bubble"<
}
};
void fn(animal *pan)
{
pan->breath();
}
void main()
{
fish fh;
//st.breath ();//如果子类和基类有两个同名函数,则最后用子类覆盖基类,这叫重载。
animal *pan;
pan=&fh; //改变指针,把animal的指针用fish替换了。但是还是输出animal。若在animal前加virtual虚拟,则输出fish
fn(pan);
}



评论


相关推荐

技术专区

关闭