ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第43课 继承的概念和意义

2020-02-26 12:37:44  阅读:251  来源: 互联网

标签:关系 Parent 继承 子类 43 概念 对象 父类


本文内容来自于对狄泰学院 唐佐林老师 C++深度解析 课程的学习总结

组合关系

在这里插入图片描述

实验编程

以电脑为例,编程描述 组合关系

#include <iostream>

using namespace std;

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

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

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


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

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

class Computer
{
private:
    MainBoard m_mainboard;
    CPU m_cpu;
    DispCard m_dispcard;
    Memery m_memery;
    Disk m_disk;
public:
    Computer()
    {
        cout << "Compurter(): " << endl;
    }

    ~ Computer()
    {
        cout << "~Compurter(): " << endl;
    }
};

int main()
{
    Computer c;

    return 0;
}

运行结果
在这里插入图片描述

类之间的组合关系

  • 组合关系的特点

其它类的对象 作为 当前类的成员 使用
当前类的对象与成员对象的 生命期相同
成员对象在 用法 上与普通对象 完全一致




惊艳的继承

面向对象中的 继承 指类之间的 父子关系

子类拥有父类的 所有属性和行为
子类就是一种特殊的父类
子类对象可以当作父类对象使用
子类中可以 添加父类没有的 方法和属性

关于继承关系的简单示例
在这里插入图片描述
C++中通过下面的方式描述继承关系
在这里插入图片描述

编程实验

根据上面的语法,编写程序测试类之间的继承关系

#include <iostream>

using namespace std;

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

    void method()
    {
        cout << "method() " << endl;
    }
    ~Parent()
    {
        cout << "~Parent() " << endl;
    }
};

class Child : public Parent
{
public:
    Child()
    {
        cout << "Child()" << endl;
    }

    void hello()
    {
        cout << "hello()" << endl;
    }
    ~Child()
    {
        cout << "~Child() " << endl;
    }
};

int main()
{
    Child c;

    c.hello();
    c.method();

    return 0;
}

运行结果
在这里插入图片描述

实现结果:Child 类继承了 Parent 类,Child 对象可以访问 Parent 类中的 method() 函数

重要的规则:

子类就是一个 特殊的父类
子类对象可以 直接初始化 父类对象
子类对象可以 直接赋值给 父类对象

继承的意义:

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




小结

继承是面向对象中 类之间的一种关系
子类拥有父类的 所有属性和行为
子类对象可以当作父类对象使用
子类中可以 添加父类没有的 方法的属性
继承是面向对象中 代码复用的重要手段

lzg2011 发布了39 篇原创文章 · 获赞 0 · 访问量 857 私信 关注

标签:关系,Parent,继承,子类,43,概念,对象,父类
来源: https://blog.csdn.net/lzg2011/article/details/104513881

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有