继承的意义(三十六)

        今天我们来讲下 C++ 三大特性之继承。我们首先来思考下,类与类之间是否存在直接的关联关系呢?我们还是以之前的讲解的电脑为例,说下组合关系,组合便是整体与部分的关系,如下

图片.png

        我们以这个关系为例,用代码来描述下

#include <iostream>
#include <string>

using namespace std;

class Memory
{
public:
    Memory()
    {
        cout << "Memory()" << endl;
    }
    ~Memory()
    {
        cout << "~Memory()" << endl;
    }
};

class Disk
{
public:
    Disk()
    {
        cout << "Disk()" << endl;
    }
    ~Disk()
    {
        cout << "~Disk()" << endl;
    }
};

class CPU
{
public:
    CPU()
    {
        cout << "CPU()" << endl;
    }
    ~CPU()
    {
        cout << "~CPU()" << endl;
    }
};

class MainBoard
{
public:
    MainBoard()
    {
        cout << "MainBoard()" << endl;
    }
    ~MainBoard()
    {
        cout << "~MainBoard()" << endl;
    }
};

class Computer
{
    Memory mMem;
    Disk mDisk;
    CPU mCPU;
    MainBoard mMainBoard;
public:
    Computer()
    {
        cout << "Computer()" << endl;
    }
    void power()
    {
        cout << "power()" << endl;
    }
    void reset()
    {
        cout << "reset()" << endl;
    }
    ~Computer()
    {
        cout << "~Computer()" << endl;
    }
};

int main()
{
    Computer c;
    
    return 0;
}

        我们看到电脑是由硬盘、内存、CPU和主板构成的,我们看看编译结果

图片.png

        确实在生成电脑这个对象时,先是生成了硬盘、内存、CPU和主板一系列的东西,在析构的时候顺便将这些东西也都清理了。那么我们来看看组合关系的特点a> 将其它类的对象作为当前类的成员使用;b> 当前类的对象与成员对象的声命周期相同;c> 成员对象在用法上与普通对象完全一致

        那么继承又是怎样的关系呢?继承关系通俗来讲就是父子关系,如下

图片.png

        面向对象中的继承便是指类之间的父子关系,子类拥有父类的所有属性和行为, 子类就是这一种特殊的父类。子类对象可以当做父类对象使用,子类中可以添加父类中没有的方法和属性。C++ 中通过下面的方式描述继承关系

图片.png

        下来我们通过示例代码来看看继承

#include <iostream>
#include <string>

using namespace std;

class Parent
{
    int mi;
public:
    Parent()
    {
        cout << "Parent()" << endl;
        mi = 100;
    }
    void method()
    {
        cout << "mi = " << mi << endl;
    }
};

class Child : public Parent
{
public:
    void hello()
    {
        cout << "I'm Child calss!" << endl;
    }
};

int main()
{
    Child d;
    Child d1;
    
    Parent p = d1;

    d.hello();
    d.method();

    return 0;
}

        我们直接用类 Child 生成的对象 d 去调用类  Parent 的成员函数 method,而且在第 35 行直接用对象 d1 去初始化对象 p。我们看看编译是否会通过

图片.png

        我们看到没有报错,并且运行成功。我们之前说过子类拥有父类的一切属性,子类对象可以当做特殊的父类对象,所以这就不难解释了。子类对象便可以直接使用父类中的所有资源,这便有点类似于我们在 C 语言中说的代码复用了(其实继承的本质就是复用父类的代码)。那么继承有几条规则:a> 子类便是一个特殊的父类;b> 子类对象是可以直接初始化父类对象的;c> 子类对象可以直接赋值给父类对象。

        那么继承的意义是什么呢?继承是 C++ 中代码复用的重要手段。通过继承,可以获得父类的所有功能,并且可以在子类中重写已有的功能,或者添加新功能

        下来我们再通过一个示例代码来加深下对继承的理解

#include <iostream>
#include <string>

using namespace std;

class Memory
{
public:
    Memory()
    {
        cout << "Memory()" << endl;
    }
    ~Memory()
    {
        cout << "~Memory()" << endl;
    }
};

class Disk
{
public:
    Disk()
    {
        cout << "Disk()" << endl;
    }
    ~Disk()
    {
        cout << "~Disk()" << endl;
    }
};

class CPU
{
public:
    CPU()
    {
        cout << "CPU()" << endl;
    }
    ~CPU()
    {
        cout << "~CPU()" << endl;
    }
};

class MainBoard
{
public:
    MainBoard()
    {
        cout << "MainBoard()" << endl;
    }
    ~MainBoard()
    {
        cout << "~MainBoard()" << endl;
    }
};

class Computer
{
    Memory mMem;
    Disk mDisk;
    CPU mCPU;
    MainBoard mMainBoard;
public:
    Computer()
    {
        cout << "Computer()" << endl;
    }
    void power()
    {
        cout << "power()" << endl;
    }
    void reset()
    {
        cout << "reset()" << endl;
    }
    ~Computer()
    {
        cout << "~Computer()" << endl;
    }
};

class HPBook : public Computer
{
    string mOS;
public:
    HPBook()
    {
        mOS = "Windows 10";
    }
    void install(string os)
    {
        mOS = os;
    }
    void OS()
    {
        cout << mOS << endl;
    }
};

class MacBook : public Computer
{
public:
    void OS()
    {
        cout << "Mac OS" << endl;
    }
};

int main()
{
    HPBook hp;
    
    hp.power();
    hp.install("Ubuntu 16.04");
    hp.OS();
    
    cout << endl;
    
    MacBook mac;
    
    mac.OS();
    
    return 0;
}

        我们在电脑的基础上新定义了 hp 和 Mac 两种电脑。给 hp 重装了 Ubuntu 系统,然后打印了它的系统。直接打印 Mac 电脑的系统,看看是否如我们所愿

图片.png

        确实是这样的。通过对继承的学习,总结如下:1、继承是面向对象中类之间的一种关系;2、子类拥有父类的所有属性和行为;3、子类对象可以当做父类对象使用;4、子类中可以添加父类中没有的方法和属性;5、继承是面向对象中代码复用的重要手段


        欢迎大家一起来学习 C++ 语言,可以加我QQ:243343083

猜你喜欢

转载自blog.51cto.com/12810168/2120521
今日推荐