专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

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

作者:黄波海   来源:本站原创   点击数:  更新时间:2014年03月06日   【字体:

 

/**********************

子类覆盖基类某个函数的方法是定义子类之后在子类重新声明
子类要将要覆盖的这个函数,记得要声明!比如本例中①处eat()之前不能
省略void。在子类②处在写法还可以重载基类eat()函数。
 
 
  ************************/
 
#include<iostream.h>
 
class animal
{
public:
eat();
};
 
animal::eat()
{
cout<<"我是基类的eat()"<<endl;
}
 
class pig:public animal
{
public:
   void eat();  //  ①
};
 
void pig::eat()
{
animal::eat();  //②
cout<<"我是pig类的eat(),我覆盖了基类animal的eat()"<<endl;
}
 
int main()
{
 
pig stp;
stp.eat();
}
关闭窗口

相关文章