多态案例--组装电脑

1. 需求分析

使用不同厂商生产的CPU, GPU, 内存条(memory bank)来组装电脑,编写电脑的工作函数,使其可以正常工作。

2. 结构分析

  1. 创建三个零件的基类;
  2. 厂商继承基类,生产出零件;
  3. 电脑使用三个零件,执行工作函数。

3. 代码实现

#include <iostream>

using namespace std;

class CPU
{
public:
    virtual void cpuWork() = 0;
};

class GPU
{
public:
    virtual void gpuWork() = 0;
};
class MemoryBank
{
public:
    virtual void memWork() = 0;
};

class AMD_CPU :public CPU
{
public:
    void cpuWork()
    {
        cout << "\tAMD_CPU在工作" << endl;
    }
    
};
class AMD_GPU :public GPU
{
public:
    void gpuWork()
    {
        cout << "\tAMD_GPU在工作" << endl;
    }

};
class AMD_MB :public MemoryBank
{
public:
    void memWork()
    {
        cout << "\tAMD_Memory Bank在工作" << endl;
    }

};

class Intel_CPU :public CPU
{
public:
    void cpuWork()
    {
        cout << "\tIntel_CPU在工作" << endl;
    }

};
class Intel_GPU :public GPU
{
public:
    void gpuWork()
    {
        cout << "\tIntel_GPU在工作" << endl;
    }

};
class Intel_MB :public MemoryBank
{
public:
    void memWork()
    {
        cout << "\tIntel_Memory Bank在工作" << endl;
    }
};

class Computer
{
public:
    Computer(CPU*c, GPU*g, MemoryBank*mb)
    {
        m_cpu = c;
        m_gpu = g;
        m_MB = mb;
    }
    void Pack()
    {
        m_cpu->cpuWork();
        m_gpu->gpuWork();
        m_MB->memWork();
    }
    ~Computer()
    {   
        if (m_cpu != NULL)
        {
            delete m_cpu;
            m_cpu = NULL;
            cout << "\tCPU被删除" << endl;
        }
        if (m_gpu != NULL)
        {
            delete m_gpu;
            m_gpu = NULL;
            cout << "\tGPU被删除" << endl;
        }
        if (m_MB != NULL)
        {
            delete m_MB;
            m_MB = NULL;
            cout << "\tMemory Bank被删除" << endl;
        }
    }
private:
    CPU* m_cpu;
    GPU* m_gpu;
    MemoryBank* m_MB;
};

void test()
{
    cout << "组装第一台电脑:" << endl;
    Computer* comp1 = new Computer(new AMD_CPU, new AMD_GPU, new AMD_MB);
    comp1->Pack();
    delete comp1;

    cout << "\n==============================\n" << endl;
    cout << "组装第二台电脑:" << endl;
    Computer* comp2 = new Computer(new Intel_CPU, new Intel_GPU, new Intel_MB);
    comp2->Pack();
    delete comp2;

    cout << "\n==============================\n" << endl;
    cout << "组装第三台电脑:" << endl;
    Computer* comp3 = new Computer(new Intel_CPU, new AMD_GPU, new Intel_MB);
    comp3->Pack();
    delete comp3;
}

int main()
{
    test();

    system("pause");
    return 0;
}

运行截图:在这里插入图片描述

4. 程序分析

原本设想是将三个零件都封装到厂商类里面,即:

class AMD :public CPU, public GPU, public MemoryBank
{
public:
    void cpuWork()
    {
        cout << "\tAMD_CPU在工作" << endl;
    }
    void gpuWork()
    {
        cout << "\tAMD_GPU在工作" << endl;
    }
    void memWork()
    {
        cout << "\tAMD_Memory Bank在工作" << endl;
    }
};
class Intel :public CPU, public GPU, public MemoryBank
{
public:
    void cpuWork()
    {
        cout << "\tIntel_CPU在工作" << endl;
    }
    void gpuWork()
    {
        cout << "\tIntel_GPU在工作" << endl;
    }
    void memWork()
    {
        cout << "\tIntel_Memory Bank在工作" << endl;
    }
};

测试代码如下:

void test()
{
    cout << "组装第一台电脑:" << endl;
    Computer* comp1 = new Computer(new AMD, new AMD, new AMD);
    comp1->Pack();
    delete comp1;

    // ...
}

但是在执行Computer类的析构函数时,发生了错误:
在这里插入图片描述
原因??未知。。。T_T

在之后修改的时候,将各个厂商的零件分开写,然后再组装,即上面成功运行的代码,这个时候可以运行成功。。。

原因??未知。。。T_T

这让我想到了。。程序员灵魂之问:
“为什么运行不成功?”
“为什么可以运行成功?”

希望有大佬可以帮帮忙,球球了~。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值