2.2.6 类的继承(1)
1.继承
我们定义一个动物类,对于动物来说,它应该具有吃、睡觉和呼吸的方法。
- 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++(www.cppentry.com)中,提供了一种重要的机制,就是继承。类是可以继承的,我们可以基于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;
- }