專注電子技術學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

C++函數(shù)的覆蓋與再現(xiàn)例子

作者:黃波海   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2014年03月06日   【字體:

 

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

子類覆蓋基類某個函數(shù)的方法是定義子類之后在子類重新聲明
子類要將要覆蓋的這個函數(shù),記得要聲明!比如本例中①處eat()之前不能
省略void。在子類②處在寫法還可以重載基類eat()函數(shù)。
 
 
  ************************/
 
#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();
}
關閉窗口

相關文章