新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > C++函数的覆盖与再现例子

C++函数的覆盖与再现例子

作者:时间:2016-12-01来源:网络收藏
/**********************
子类覆盖基类某个函数的方法是定义子类之后在子类重新声明
子类要将要覆盖的这个函数,记得要声明!比如本例中①处eat()之前不能
省略void。在子类②处在写法还可以重载基类eat()函数。
************************/
#include
class animal
{
public:
eat();
};
animal::eat()
{
cout<<"我是基类的eat()"<
}
class pig:public animal
{
public:
void eat(); // ①
};
void pig::eat()
{
animal::eat(); //②
cout<<"我是pig类的eat(),我覆盖了基类animal的eat()"<
}
int main()
{
pig stp;
stp.eat();
}


关键词: C++函数覆盖与再

评论


相关推荐

技术专区

关闭