首页 > 其他分享 >2023.4.25

2023.4.25

时间:2023-04-25 21:48:26浏览次数:38  
标签:25 10 mem void virtual public 2023.4 class

 1 #include <iostream>
 2 using namespace std;
 3 //利用多态,实现计算器
 4 //多态的好处
 5 //1、组织结构清晰
 6 //2、可读性强
 7 //3、对于前期和后期扩展以及维护性高
 8 class AbstractCalculator
 9 {
10 public:
11     int m_Num1;
12     int m_Num2;
13     virtual int getResult()
14     {
15         return 0;
16     }
17 };
18 class AddCalculate : public AbstractCalculator
19 {
20     int getResult()
21     {
22         return m_Num1 + m_Num2;
23     }
24 };
25 class SubCalculate : public AbstractCalculator
26 {
27     int getResult()
28     {
29         return m_Num1 - m_Num2;
30     }
31 };
32 class MulCalculate : public AbstractCalculator
33 {
34     int getResult()
35     {
36         return m_Num1 * m_Num2;
37     }
38 };
39 void test()
40 {
41     //创建加法计算器
42     AbstractCalculator *abc = new AddCalculate;
43     abc->m_Num1 = 10;
44     abc->m_Num2 = 10;
45     cout<<abc->m_Num1<<" + "<<abc->m_Num2<<" = "<<abc->getResult()<<endl;
46     delete abc;
47     //创建加法计算器
48     abc = new SubCalculate;
49     abc->m_Num1 = 10;
50     abc->m_Num2 = 10;
51     cout<<abc->m_Num1<<" - "<<abc->m_Num2<<" = "<<abc->getResult()<<endl;
52     //创建乘法计算器
53     abc = new MulCalculate;
54     abc->m_Num1 = 10;
55     abc->m_Num2 = 10;
56     cout<<abc->m_Num1<<" * "<<abc->m_Num2<<" = "<<abc->getResult()<<endl;
57 }
58 int main()
59 {
60     test();
61     return 0;
62 }
 1 #include <iostream>
 2 using namespace std;
 3 //纯虚函数和抽象类
 4 //多态案例-制作饮品(咖啡和茶叶)
 5 //煮水->冲泡->倒入杯中->加入辅料
 6 class AbstractMakeDrink
 7 {
 8 public:
 9     virtual void Boil() = 0;
10     virtual void Brew() = 0;
11     virtual void Pow() = 0;
12     virtual void Putsth() = 0;
13     void makeDrink()
14     {
15         Boil();
16         Brew();
17         Pow();
18         Putsth();
19     }
20 };
21 class Coffee :public AbstractMakeDrink
22 {
23 public:
24     virtual void Boil()
25     {
26         cout<<"煮沸农夫山泉"<<endl;
27     }
28     virtual void Brew()
29     {
30         cout<<"冲泡咖啡"<<endl;
31     }
32     virtual void Pow()
33     {
34         cout<<"倒入杯中"<<endl;
35     }
36     virtual void Putsth()
37     {
38         cout<<"加入糖和牛奶"<<endl;
39     }
40 };
41 class Tea :public AbstractMakeDrink
42 {
43 public:
44     virtual void Boil()
45     {
46         cout<<"煮沸山泉"<<endl;
47     }
48     virtual void Brew()
49     {
50         cout<<"冲泡茶叶"<<endl;
51     }
52     virtual void Pow()
53     {
54         cout<<"倒入茶杯中"<<endl;
55     }
56     virtual void Putsth()
57     {
58         cout<<"加入柠檬"<<endl;
59     }
60 };
61 void DoWork(AbstractMakeDrink *abs)
62 {
63     abs->makeDrink();
64 }
65 void test()
66 {
67     DoWork(new Coffee);
68     cout<<"---------------------"<<endl;
69     DoWork(new Tea);
70 }
71 int main()
72 {
73     test();
74     return 0;
75 }
  1 #include <iostream>
  2 using namespace std;
  3 //虚析构和纯虚析构
  4 //多态案例-电脑组装
  5 //电脑主要组成部件CPU、显卡、内存条
  6 //将每个零件封装出抽象类,并且提供不同的厂商生产不同的零件
  7 //创建电脑类提供让电脑工作的函数,并且调用每个零件的接口
  8 /////////////////////////////////////////////////////////////
  9 //抽象不同的零件类
 10 class CPU
 11 {
 12 public:
 13     virtual void calculate() = 0;
 14 };
 15 class VideoCard
 16 {
 17 public:
 18     virtual void display() = 0;
 19 };
 20 class Memory
 21 {
 22 public:
 23     virtual void storage() = 0;
 24 };
 25 //电脑类
 26 class Computer
 27 {
 28 public:
 29     Computer(CPU* cpu,VideoCard* vc,Memory* mem)
 30     {
 31         m_cpu = cpu;
 32         m_vc = vc;
 33         m_mem = mem;
 34     }
 35     void work()
 36     {
 37         m_cpu->calculate();
 38         m_vc->display();
 39         m_mem->storage();
 40     }
 41     ~Computer()
 42     {
 43         if(m_cpu != NULL)
 44         {
 45             delete m_cpu;
 46             m_cpu = NULL;
 47         }
 48         if(m_vc != NULL)
 49         {
 50             delete m_vc;
 51             m_vc = NULL;
 52         }
 53         if(m_mem != NULL)
 54         {
 55             delete m_mem;
 56             m_mem = NULL;
 57         }
 58     }
 59 private:
 60     CPU* m_cpu;
 61     VideoCard* m_vc;
 62     Memory* m_mem;
 63 };
 64 //厂商
 65 //Intel
 66 class IntelCPU : public CPU
 67 {
 68 public:
 69     void calculate()
 70     {
 71         cout<<"Intel的CPU开始计算"<<endl;
 72     }
 73 };
 74 class IntelMemory : public Memory
 75 {
 76 public:
 77     void storage()
 78     {
 79         cout<<"Intel的内存开始存储"<<endl;
 80     }
 81 };
 82 class IntelVideoCard : public VideoCard
 83 {
 84 public:
 85     void display()
 86     {
 87         cout<<"Intel的显卡开始显示"<<endl;
 88     }
 89 };
 90 //Lenovo
 91 class LenovoCPU : public CPU
 92 {
 93 public:
 94     void calculate()
 95     {
 96         cout<<"Lenovo的CPU开始计算"<<endl;
 97     }
 98 };
 99 class LenovoMemory : public Memory
100 {
101 public:
102     void storage()
103     {
104         cout<<"Lenovo的内存开始存储"<<endl;
105     }
106 };
107 class LenovoVideoCard : public VideoCard
108 {
109 public:
110     void display()
111     {
112         cout<<"Lenovo的显卡开始显示"<<endl;
113     }
114 };
115 void test01()
116 {
117     //电脑零件
118      CPU * intelCPU = new IntelCPU;
119      Memory * intelMem = new IntelMemory;
120      VideoCard * intelCard = new IntelVideoCard;
121      //创建电脑
122      Computer *computer1 = new Computer(intelCPU,intelCard,intelMem);
123      computer1->work();
124      delete computer1;
125 }
126 int main()
127 {
128     test01();
129     return 0;
130 }

 

标签:25,10,mem,void,virtual,public,2023.4,class
From: https://www.cnblogs.com/muzhaodi/p/17353971.html

相关文章

  • 每日总结2023-04-25
    今天无事发生在讨论完老师对于随车买的意见后,意识到了要贴近显示,软件是为了懒人而生,很大程度上不能方便快捷的软件就是败笔。经过讨论,随手买可以加上智能一键补货,车载web端可以加上点歌功能、语音识别购买等项目。 ......
  • 4.25每日总结
    今天做了什么:存储图片到mysql数据库和从mysql读取图片并显示,尝试调用智能裁剪接口,学习新版web,掌握了sql中的连接查询结果函数遇到了哪些困难:调用返回数据尝试解析明天打算做什么:继续学习web,将app的存储图片和显示功能实现......
  • NC25045 [USACO 2007 Jan S]Balanced Lineup
    题目链接题目题目描述Forthedailymilking,FarmerJohn'sNcows(1≤N≤100,000)alwayslineupinthesameorder.OnedayFarmerJohndecidestoorganizeagameofUltimateFrisbeewithsomeofthecows.Tokeepthingssimple,hewilltakeacontiguous......
  • 23-4-25--链表--一元多项式求导
    设计函数求一元多项式的导数。输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。输入样例:34-5261-20 ......
  • 4.25学生信息管理系统1.基本框架
    一问题描述 设计一款可以存储,查询,修改,删除信息的学生信息管理系统。二设计思路采用多文件多函数三基本框架#include"StudentMangerSystem.h"intmain(){ while(1) { Welcome();//欢迎界面 charch=_getch(); switch(ch) { case'1':InputStudent();//录入学......
  • 2023.4.25编程一小时打卡
    一、问题描述:格式输出:输入一个整数,以八进制形式输入,分别以十进制和十六进制显示;输出字符串“Iamastudent!”,设置输出位宽为20,使用符号“*”填充;输出浮点数3.1415926,分别以浮点数和二进制形式进行输出,并分别设置小数点后的位数为8,6,4位。 二、解题思路:首先,根据题意定......
  • 4.25每日总结
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"&g......
  • 4.25打卡
    #include<iostream>#include<iomanip>#include<cmath>usingnamespacestd;boolsymm(unsignedn){unsignedi=n;unsignedm=0;while(i>0){m=m*10+i%10;i/=10;}returnm==n;}intmain(){......
  • 2023 4 25
     ......
  • 2023.4.25
    publicbooleanroot_IsPass(Stringid,Stringpass)throwsException{Stringpas=root_GetPassword(id);if(pas!=null){if(!pas.equals("")){if(pass!=null){......