登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

VC++深入详解:类的继承  

2008-11-29 09:32:05|  分类: VC++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

我们定义一个动物类,对于动物来说,它应该具有吃、睡觉和呼吸的方法。

  class animal

  {

  public:

    void eat()

    {

      cout<<"animal eat"<<endl;

    }

    void sleep()

    {

      cout<<"animal sleep"<<endl;

    }

    void breathe()

    {

      cout<<"animal breathe"<<endl;

    }

  };

  我们再定义一个鱼类,对于鱼来说,它也应该具有吃、睡觉和呼吸的方法。

  class fish

  {

  public:

     void eat()

     {

       cout<<"fish eat"<<endl;

     }

     void sleep()

     {

       cout<<"fish sleep"<<endl;

     }

     void breathe()

     {

       cout<<"fish breathe"<<endl;

     }

  };

  如果我们再定义一个绵羊类,对于绵羊来说,它也具有吃、睡觉和呼吸的方法,我们是否又重写一遍代码呢?既然鱼和绵羊都是动物,是否可以让鱼和绵羊继承动物的方法呢?在C++中,提供了一种重要的机制,就是继承。类是可以继承的,我们可以基于animal这个类来创建fish类,animal称为基类(Base Class,也称为父类),fish称为派生类(Derived Class,也称为子类)。派生类除了自己的成员变量和成员方法外,还可以继承基类的成员变量和成员方法。

重写animal和fish类,让fish从animal继承,代码如例2-11所示(EX05.CPP)。

  例2-11

  #include <iostream.h>

  class animal

  {

  public:

     void eat()

     {

       cout<<"animal eat"<<endl;

     }

void sleep()

     {

       cout<<"animal sleep"<<endl;

     }

     void breathe()

     {

       cout<<"animal breathe"<<endl;

     }

  };

  class fish:public animal

  { 

  };

  void main()

  {

     animal an;

     fish fh;

     an.eat();

     fh.eat();

  }

  虽然fish类没有显式地编写一个方法,但fish从animal已经继承eat、sleep、breathe方法,我们通过编译运行可以看到结果。

  下面,我们在animal类和fish类中分别添加构造函数和析构函数,然后在main函数中定义一个fish类的对象fh,看看在构造fish类的对象时,animal类的构造函数是否被调用;如果调用,animal类和fish类的构造函数的调用顺序是怎样的。完整代码如例2-12所示(EX06.CPP)。

  例2-12

  #include <iostream.h>

  class animal

  {

  public:

     animal()

     {

       cout<<"animal construct"<<endl;

     }

     ~animal()

     {

       cout<<"animal destruct"<<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类的对象要先构造。在析构时,正好相反。

VC++深入详解:类的继承 - wmnmtm - 我们你们它们

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

 

 

 

  评论这张
 
阅读(1540)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018