龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

VC++深入详解:类的继承[图](3)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
} void eat() { cout"animal eat"endl; } void sleep() { cout"animal sleep"endl; } void breathe() { cout"animal breathe"endl; } }; class fish:public animal { public: fish() { cout"fish construct"endl; }
     }
     void eat()
     {
       cout<<"animal eat"<<endl;
     }
     void sleep()
     {
       cout<<"animal sleep"<<endl;
     }
     void breathe()
     {
       cout<<"animal breathe"<<endl;
     }
  };
class fish:public animal
  {
  public:
     fish()
     {
       cout<<"fish construct"<<endl;
     }
     ~fish()
     {
       cout<<"fish destruct"<<endl;
     }
  };
  void main()
  {
     fish fh;
  }


  编译运行,出现如图2.11所示的结果。

  可以看到当构造fish类的对象fh时,animal类的构造函数也要被调用,而且在fish类的构造函数调用之前被调用。当然,这也很好理解,没有父亲就没有孩子,因为fish类从animal类继承而来,所以在fish类的对象构造之前,animal类的对象要先构造。在析构时,正好相反。

图2.11 EX06.CPP程序的运行结果

精彩图集

赞助商链接