当前位置: 首页 > news >正文

C++——多态经典案例(一)组装电脑

案例:小明打算买两台组装电脑,假设电脑零部件包括CPU、GPU和内存组成。
一台电脑使用intel的CPU、GPU和内存条
一台电脑使用Huawei的CPU、GPU和Intel的内存条

分析:使用多态进行实现
将CPU、GPU和内存条定义为抽象类,内部分别定义其对应功能的纯虚函数
Intel的CPU继承CPU,并实现其内部的纯虚函数(calculate)
Intel的GPU继承GPU,并实现其内部的纯虚函数(display)
Intel的MEMORY继承MEMORY,并实现其内部的纯虚函数(memory)
同样华为也一样继承CPU、GPU和MEMORY并实现对应的纯虚函数

封装一个Computer类,包含CPU、GPU和MEMORY,其成员属性为CPU、GPU和MEMORY的指针
内部有个work方法,用于调用CPU、GPU和MEMORY对应的方法

最后小明通过Computer类进行组装自己的电脑,并运行

#include<iostream>
class CPU 
{
public:virtual void calculate() = 0;
};class GPU
{
public:virtual void display() = 0;
};class MEMORY
{
public:virtual void memory() = 0;
};class Computer 
{
public:Computer(CPU *cpu,GPU *gpu,MEMORY *memory){m_cpu = cpu;m_gpu = gpu;m_memory = memory;}void work() {m_cpu->calculate();m_gpu->display();m_memory->memory();}~Computer(){if (m_cpu != NULL) {delete m_cpu;m_cpu = NULL;}if (m_gpu != NULL){delete m_gpu;m_gpu = NULL;}if (m_memory != NULL){delete m_memory;m_memory = NULL;}}private:CPU *m_cpu;GPU *m_gpu;MEMORY *m_memory;
};class IntelCPU :public CPU
{
public:virtual void calculate(){std::cout << "IntelCPU is calculate..." << std::endl;}
};
class IntelGPU :public GPU
{
public:virtual void display(){std::cout << "IntelGPU is display..." << std::endl;}
};
class IntelMEMORY :public MEMORY
{
public:virtual void memory(){std::cout << "IntelMEMORY is memory..." << std::endl;}
};class HuaweiCPU :public CPU
{
public:virtual void calculate(){std::cout << "HuaweiCPU is calculate..." << std::endl;}
};
class HuaweiGPU :public GPU
{
public:virtual void display(){std::cout << "HuaweiGPU is display..." << std::endl;}
};
class HuaweiMEMORY :public MEMORY
{
public:virtual void memory(){std::cout << "HuaweiMEMORY is memory..." << std::endl;}
};int main(int argc,char **argv) 
{CPU *my_CPU = new IntelCPU;GPU *my_GPU = new IntelGPU;MEMORY *my_memory = new IntelMEMORY;Computer *my_computer = new Computer(my_CPU, my_GPU, my_memory);my_computer->work();delete my_computer;Computer* my_computer_2 = new Computer(new HuaweiCPU,new HuaweiGPU,new IntelMEMORY);my_computer_2->work();return 0;
}

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

http://www.lryc.cn/news/415595.html

相关文章:

  • 从传统监控到智能化升级:EasyCVR视频汇聚平台的一站式解决方案
  • Windows下,已知程序PID,取得其窗口句柄HWND
  • Java获取exe文件详细信息:产品名称,产品版本等
  • ORB-SLAM2运行环境搭建
  • Nginx高频核心面试题2
  • 全面提升PDF编辑效率,2024年五大顶级PDF编辑器推荐!
  • 代码随想录算法训练营第二十天|235. 二叉搜索树的最近公共祖先 701.二叉搜索树中的插入操作 450.删除二叉搜索树中的节点
  • 视频美颜SDK与直播美颜插件在实时视频中的应用
  • 【Linux】yum(工具篇)
  • 3GPP入门
  • FFmpeg内存对齐简述
  • 手机号码归属地查询接口如何对接?(一)
  • DDei在线设计器-加载数据
  • NetLLM: Adapting Large Language Models for Networking.
  • 基于Yolov8面部七种表情检测与识别C++模型部署
  • 未确认融资费用含义及会计处理流程
  • Linux配置go程序为service后台开机自启动
  • 汇舟问卷:完成16份调查,挣了40美金,换算后美滋滋
  • Nacos 202407月RCE漏洞(0day)与复现
  • Dynamo修改共享参数绑定的分组——群问题整理005
  • 聚焦汽车软件开发与测试:静态代码扫描、单元测试与集成测试等方面的实践应用
  • 「队列」实现FIFO队列(先进先出队列|queue)的功能 / 手撕数据结构(C++)
  • C++ STL中 `set` 和 `multiset` 简单对比
  • 代码随想录算法训练营Day20 | Leetcode 235 二叉搜索树的最近公共祖先 Leetcode 701 二叉搜索树中的插入操作
  • 第九届世界3D渲染大赛:赛程安排、赛事规则
  • RocketMQ5.0 Consumer Group
  • vulnhub之serial
  • 卷积神经网络(CNN)简单原理与简单代码实现
  • 实时数仓分层架构详解
  • 计算机“八股文”在实际工作中是助力、阻力还是空谈?