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

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

  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程序的运行结果

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值