设为首页 加入收藏

TOP

2.2.6 类的继承(1)
2013-10-07 15:20:20 来源: 作者: 【 】 浏览:62
Tags:2.2.6 继承

2.2.6  类的继承(1)

1.继承

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

  1. class animal  
  2. {  
  3. public:  
  4.     void eat()  
  5.     {  
  6.         cout<<"animal eat"<<endl;  
  7.     }  
  8.     void sleep()  
  9.     {  
  10.         cout<<"animal sleep"<<endl;  
  11.     }  
  12.     void breathe()  
  13.     {  
  14.         cout<<"animal breathe"<<endl;  
  15.     }  
  16. };  

我们再定义一个鱼类,对于鱼来说,它也应该具有吃、睡觉和呼吸的方法。
  1. class fish  
  2. {  
  3. public:  
  4.      void eat()  
  5.      {  
  6.           cout<<"fish eat"<<endl;  
  7.      }  
  8.      void sleep()  
  9.      {  
  10.           cout<<"fish sleep"<<endl;  
  11.      }  
  12.      void breathe()  
  13.      {  
  14.           cout<<"fish breathe"<<endl;  
  15.      }  
  16. };  

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

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

例2-11

  1. #include <iostream.h> 
  2. class animal  
  3. {  
  4. public:  
  5.      void eat()  
  6.      {  
  7.           cout<<"animal eat"<<endl;  
  8.      }  
  9.      void sleep()  
  10.      {  
  11.           cout<<"animal sleep"<<endl;  
  12.      }  
  13.      void breathe()  
  14.      {  
  15.           cout<<"animal breathe"<<endl;  
  16.      }  
  17. };  
  18. class fish:public animal  
  19. {     
  20. };  
  21. void main()  
  22. {  
  23.      animal an;  
  24.      fish fh;  
  25.      an.eat();  
  26.      fh.eat();  
  27. }  

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

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

例2-12

  1. #include <iostream.h> 
  2. class animal  
  3. {  
  4. public:  
  5.      animal()  
  6.      {  
  7.           cout<<"animal construct"<<endl;  
  8.      }  
  9.      ~animal()  
  10.      {  
  11.           cout<<"animal destruct"<<endl;  
  12.      }  
  13.      void eat()  
  14.      {  
  15.           cout<<"animal eat"<<endl;  
  16.      }  
  17.      void sleep()  
  18.      {  
  19.           cout<<"animal sleep"<<endl;  
  20.      }  
  21.      void breathe()  
  22.      {  
  23.           cout<<"animal breathe"<<endl;  
  24.      }  
  25. };  
  26. class fish:public animal  
  27. {  
  28. public:  
  29.      fish()  
  30.      {  
  31.           cout<<"fish construct"<<endl;  
  32.      }  
  33.      ~fish()  
  34.      {  
  35.           cout<<"fish destruct"<<endl;  
  36.      }  
  37. };  
  38. void main()  
  39. {  
  40.      fish fh;  
  41. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.2.6 类的继承(2) 下一篇2.2.5 this指针

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

Shell 基本运算符 -
Shell 函数 | 菜鸟教
Linux 常用命令集合
socket 编程如何实现
Python创建简易的Soc