继承的意义(三十六)-创新互联

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

为乳山等地区用户提供了全套网页设计制作服务,及乳山网站建设行业解决方案。主营业务为网站制作、成都网站建设、乳山网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

继承的意义(三十六)

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

#include 
#include 

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和主板构成的,我们看看编译结果

继承的意义(三十六)

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

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

继承的意义(三十六)

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

继承的意义(三十六)

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

#include 
#include 

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。我们看看编译是否会通过

继承的意义(三十六)

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

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

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

#include 
#include 

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 电脑的系统,看看是否如我们所愿

继承的意义(三十六)

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

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:继承的意义(三十六)-创新互联
网站网址:http://lszwz.com/article/pjses.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款