⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 02.2.6 类的继承(1).txt

📁 网上第一本以TXT格式的VC++深入详解孙鑫的书.全文全以TXT格式,并每一章节都分了目录,清晰易读
💻 TXT
字号:
2.2.6  类的继承
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++中,提供了一种重要的机制,就是继承。类是可以继承的,我们可以基于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类的对象要先构造。在析构时,正好相反。



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

2.在子类中调用父类的带参数的构造函数
下面我们修改一下animal类的构造函数,增加两个参数height和weight,分别表示动物的高度和重量。代码如例2-13所示。

例2-13 

#include <iostream.h>

class animal

{

public:

     animal(int height, int weight)

     {

         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;

}

当我们编译这个程序时,就会出现如下错误:



那么这个错误是如何出现的呢?当我们构造fish类的对象fh时,它需要先构造animal类的对象,调用animal类的默认构造函数(即不带参数的构造函数),而在我们的程序中,animal类只有一个带参数的构造函数,在编译时,因找不到animal类的默认构造函数而出错。

因此,在构造fish类的对象时(调用fish类的构造函数时),要想办法去调用animal类的带参数的构造函数,那么,我们如何在子类中向父类的构造函数传递参数呢?可以采用如例2-14所示的方式,在构造子类时,显式地去调用父类的带参数的构造函数。

例2-14

#include <iostream.h>

class animal

{

public:

    animal(int height, int weight)

    {

        cout<<"animal construct"<<endl;

    }


};

class fish:public animal

{

public:

    fish():animal(400,300)

    {

        cout<<"fish construct"<<endl;

    }


};

void main()

{

    fish fh;

}

注意程序中以粗体显示的代码。在fish类的构造函数后,加一个冒号(:),然后加上父类的带参数的构造函数。这样,在子类的构造函数被调用时,系统就会去调用父类的带参数的构造函数去构造对象。这种初始化方式,还常用来对类中的常量(const)成员进行初始化,如下面的代码所示:

class point

{

public:

     point():x(0),y(0)

private:

     const int x;

     const int y;

};

当然,类中普通的成员变量也可以采取此种方式进行初始化,然而,这就没有必要了。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -