2023.4.25
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 }