当前位置:   article > 正文

《c++》多态案例一.电脑组装_c++电脑组装例子

c++电脑组装例子

一.代码展示

  1. #include <iostream>
  2. using namespace std;
  3. class CPU {
  4. public:
  5. //抽象计算函数
  6. virtual void calculate() = 0;
  7. };
  8. class CVideoCard {
  9. public:
  10. //抽象显示函数
  11. virtual void display() = 0;
  12. };
  13. class Memory {
  14. public:
  15. //抽象存储函数
  16. virtual void storage() = 0;
  17. };
  18. class Computer {
  19. public:
  20. Computer(CPU* cpu, CVideoCard* vc, Memory* mem) {
  21. m_cpu = cpu;
  22. m_vc = vc;
  23. m_mem = mem;
  24. }
  25. //提供工作函数
  26. void doWork() {
  27. m_cpu->calculate();
  28. m_vc->display();
  29. m_mem->storage();
  30. }
  31. //析构函数
  32. ~Computer() {
  33. if (m_cpu != NULL) {
  34. delete m_cpu;
  35. m_cpu = NULL;
  36. }
  37. if (m_vc != NULL) {
  38. delete m_vc;
  39. m_vc = NULL;
  40. }
  41. if (m_mem != NULL) {
  42. delete m_mem;
  43. m_mem = NULL;
  44. }
  45. }
  46. private:
  47. CPU* m_cpu;//CPU的零件指针
  48. CVideoCard* m_vc;//显卡的零件指针
  49. Memory* m_mem;//内存条的零件指针
  50. };
  51. class IntelCPU : public CPU
  52. {
  53. virtual void calculate()
  54. {
  55. cout << "Intel CPU 正在计算" << endl;
  56. }
  57. };
  58. class IntelVideoCard : public CVideoCard
  59. {
  60. virtual void display()
  61. {
  62. cout << "Intel 显卡正在显示" << endl;
  63. }
  64. };
  65. class IntelMemory : public Memory
  66. {
  67. virtual void storage()
  68. {
  69. cout << "Intel 内存条正在存储" << endl;
  70. }
  71. };
  72. class AmdCPU : public CPU
  73. {
  74. virtual void calculate()
  75. {
  76. cout << "Amd CPU 正在计算" << endl;
  77. }
  78. };
  79. class AmdVideoCard : public CVideoCard {
  80. virtual void display() {
  81. cout << "Amd 显卡正在显示" << endl;
  82. }
  83. };
  84. class AmdMemory : public Memory {
  85. virtual void storage() {
  86. cout << "Amd 内存条正在存储" << endl;
  87. }
  88. };
  89. void test01() {
  90. CPU* Intelcpu = new IntelCPU();
  91. CVideoCard* Intelvc = new IntelVideoCard();
  92. Memory* Intelmem = new IntelMemory();
  93. //组装第一台电脑
  94. Computer* c1 = new Computer(Intelcpu, Intelvc, Intelmem);
  95. c1->doWork();
  96. delete c1;
  97. cout << "----------------------" << endl;
  98. //组装第二台电脑
  99. CPU* Amdcpu = new AmdCPU();
  100. CVideoCard* Amdvc = new AmdVideoCard();
  101. Memory* Amdmem = new AmdMemory();
  102. Computer* c2 = new Computer(Amdcpu, Amdvc, Amdmem);
  103. c2->doWork();
  104. delete c2;
  105. }
  106. int main() {
  107. test01();
  108. return 0;
  109. }

二.代码解析

2.1构建电脑各零件类

  1. class CPU {
  2. public:
  3. //抽象计算函数
  4. virtual void calculate() = 0;
  5. };
  6. class CVideoCard {
  7. public:
  8. //抽象显示函数
  9. virtual void display() = 0;
  10. };
  11. class Memory {
  12. public:
  13. //抽象存储函数
  14. virtual void storage() = 0;
  15. };

在这里用了组装电脑中比较重要的零件分别是CPU(处理器)Cvideocard(显卡)Memory内存条)分别创建了3个类。

在每个类中都用到纯虚函数方便子类后续进行继承

纯虚函数是为了实现接口或抽象基类而设计的,强制派生类必须重写该函数。

2.2创建电脑类

  1. class Computer {
  2. public:
  3. Computer(CPU* cpu, CVideoCard* vc, Memory* mem) {
  4. m_cpu = cpu;
  5. m_vc = vc;
  6. m_mem = mem;
  7. }
  8. //提供工作函数
  9. void doWork() {
  10. m_cpu->calculate();
  11. m_vc->display();
  12. m_mem->storage();
  13. }
  14. //析构函数
  15. ~Computer() {
  16. if (m_cpu != NULL) {
  17. delete m_cpu;
  18. m_cpu = NULL;
  19. }
  20. if (m_vc != NULL) {
  21. delete m_vc;
  22. m_vc = NULL;
  23. }
  24. if (m_mem != NULL) {
  25. delete m_mem;
  26. m_mem = NULL;
  27. }
  28. }
  29. private:
  30. CPU* m_cpu;//CPU的零件指针
  31. CVideoCard* m_vc;//显卡的零件指针
  32. Memory* m_mem;//内存条的零件指针
  33. };

1.创建变量

首先要在Computer类中创建3个零件的指针去接收传入的零件指针

  1. private:
  2. CPU* m_cpu;//CPU的零件指针
  3. CVideoCard* m_vc;//显卡的零件指针
  4. Memory* m_mem;//内存条的零件指针

这里之所以用private(保护权限)是因为编程习惯,通常在一个类中存放变量的权限用private(保护权限)防止随意被更改。

2.创建构造函数

  1. Computer(CPU* cpu, CVideoCard* vc, Memory* mem) {
  2. m_cpu = cpu;
  3. m_vc = vc;
  4. m_mem = mem;
  5. }

创建一个构造函数用来接受传入的CPU(处理器)、VideoCard(显卡) 以及 Memory(硬盘)的参数。

3.创建工作函数

  1. //提供工作函数
  2. void doWork() {
  3. m_cpu->calculate();
  4. m_vc->display();
  5. m_mem->storage();
  6. }

创建一个工作函数(doWork),该函数主要功能是提示每个零件是否正常工作。在该函数中要调用每个零件的功能,列如在Cpu类中我们创建了一个calculate(计算)函数,那么我们就要在doWork函数中调用该功能。

  1. class CPU {
  2. public:
  3. //抽象计算函数
  4. virtual void calculate() = 0;
  5. };

4.创建析构函数

  1. //析构函数
  2. ~Computer() {
  3. if (m_cpu != NULL) {
  4. delete m_cpu;
  5. m_cpu = NULL;
  6. }
  7. if (m_vc != NULL) {
  8. delete m_vc;
  9. m_vc = NULL;
  10. }
  11. if (m_mem != NULL) {
  12. delete m_mem;
  13. m_mem = NULL;
  14. }
  15. }

首先要知道为什么要创建析构函数,因为我们创建了3个指针变量,而指针变量一般是存放在内存里的堆区里,在堆区中系统不会自动释放该内存,这就导致会很容易出现内存泄漏的情况,所以我要进行手动的释放内存。

2.3创建各品牌的零件

  1. class IntelCPU : public CPU
  2. {
  3. virtual void calculate()
  4. {
  5. cout << "Intel CPU 正在计算" << endl;
  6. }
  7. };
  8. class IntelVideoCard : public CVideoCard
  9. {
  10. virtual void display()
  11. {
  12. cout << "Intel 显卡正在显示" << endl;
  13. }
  14. };
  15. class IntelMemory : public Memory
  16. {
  17. virtual void storage()
  18. {
  19. cout << "Intel 内存条正在存储" << endl;
  20. }
  21. };
  22. class AmdCPU : public CPU
  23. {
  24. virtual void calculate()
  25. {
  26. cout << "Amd CPU 正在计算" << endl;
  27. }
  28. };
  29. class AmdVideoCard : public CVideoCard {
  30. virtual void display() {
  31. cout << "Amd 显卡正在显示" << endl;
  32. }
  33. };
  34. class AmdMemory : public Memory {
  35. virtual void storage() {
  36. cout << "Amd 内存条正在存储" << endl;
  37. }
  38. };

1.Inter系列的零件

  1. class IntelCPU : public CPU
  2. {
  3. virtual void calculate()
  4. {
  5. cout << "Intel CPU 正在计算" << endl;
  6. }
  7. };

例如我们要创建一个inter系列的cpu,那首先创建一个类名为 InrelCPU

因为他是CPU类中的一个子类,所以要继承CPU类中的属性,也就是

class intelCPU : public CPU

因为在CPU类中我们创建了一个关于calculate(计算)的虚函数

  1. class IntelCPU : public CPU
  2. {
  3. virtual void calculate()
  4. {
  5. cout << "Intel CPU 正在计算" << endl;
  6. }
  7. };

因此子类也要使用这个虚函数,并给这个虚函数重载一下,所以代码为:
virtual void calculate() 
{
    cout << "Intel CPU 正在计算" << endl;
}


2.其他零件创建方式跟1相差不大,这里不再赘述

2.4创建一个主机

  1. void test01() {
  2. CPU* Intelcpu = new IntelCPU();
  3. CVideoCard* Intelvc = new IntelVideoCard();
  4. Memory* Intelmem = new IntelMemory();
  5. //组装第一台电脑
  6. Computer* c1 = new Computer(Intelcpu, Intelvc, Intelmem);
  7. c1->doWork();
  8. delete c1;
  9. cout << "----------------------" << endl;
  10. //组装第二台电脑
  11. CPU* Amdcpu = new AmdCPU();
  12. CVideoCard* Amdvc = new AmdVideoCard();
  13. Memory* Amdmem = new AmdMemory();
  14. Computer* c2 = new Computer(Amdcpu, Amdvc, Amdmem);
  15. c2->doWork();
  16. delete c2;
  17. }

1.创建一个函数

如代码所示,首先创建一个test01函数用来测试。

2.创建各品牌零件

  1. CPU* Intelcpu = new IntelCPU();
  2. CVideoCard* Intelvc = new IntelVideoCard();
  3. Memory* Intelmem = new IntelMemory();

在这段代码中,CPU* Intelcpu = new IntelCPU();   
CVideoCard* Intelvc = new IntelVideoCard();
以及Memory* Intelmem = new IntelMemory();
这些语句分别创建了 IntelCPUIntelVideoCardIntelMemory 类的对象,并将它们的指针赋值给了 IntelcpuIntelvcIntelmem 指针变量。

这些对象的创建和赋值是为了模拟组装一台计算机所需的不同零件,其中 IntelCPU 代表 Intel 品牌的 CPU,IntelVideoCard 代表 Intel 品牌的显卡,IntelMemory 代表 Intel 品牌的内存条。

类似地,后续代码中的 AmdCPUAmdVideoCardAmdMemory 分别代表 AMD 品牌的 CPU、显卡和内存条。通过这种方式,你可以轻松地组装出不同品牌的计算机,并测试它们的工作状态。

3.组装一个电脑

  1. Computer* c1 = new Computer(Intelcpu, Intelvc, Intelmem);
  2. c1->doWork();
  3. delete c1;

创建一个电脑类的对象名为 c1  将上文创建好的3个品牌零件赋值给c1。

赋值完成后调用c1中的doWork函数来检查电脑各零件是否在工作状态中。

最后创建完成电脑后记得释放内存

delete c1;

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号