ICode9

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

继承的概念和意义

2022-01-04 22:36:02  阅读:126  来源: 互联网

标签:include 意义 继承 子类 概念 对象 父类 public


继承的概念和意义

1、思考

类之间是否存在直接的关联关系

2、生活中的例子

2.1、组合关系:整体与部分的关系

image-20220104213550980

//43-1.cpp
#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;
}

组合关系特点

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

2.2、继承关系:父子关系

image-20220104214449646

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

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

3、惊艳的继承

c++中通过下面的方式描述继承关系

class Parent
{
    int mv;
public:
    void method();
};

class child : public Parent
{
    
};
//43-2.cpp
#include <iostream>
#include <string>

using namespace std;

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

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

int main()
{   
    Child c;

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

    return 0;
}

重要规则:

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

4、继承的意义

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

//43-3.cpp
#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 8";
    }
    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 LTS");
    hp.OS();

    cout << endl;
    
    MacBook mac;
    mac.OS();
    
    return 0;
}

小结

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

标签:include,意义,继承,子类,概念,对象,父类,public
来源: https://blog.csdn.net/LangLang_2020/article/details/122313192

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

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

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

ICode9版权所有