|
| 1 | +// |
| 2 | +// Created by zing on 6/7/2020. |
| 3 | +// |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +class Animal { |
| 10 | +public: |
| 11 | + int m_Age; |
| 12 | +}; |
| 13 | + |
| 14 | +//继承前加virtual关键字后,变为虚继承 |
| 15 | +//此时公共的父类Animal称为虚基类 |
| 16 | +class Sheep : virtual public Animal { |
| 17 | +}; |
| 18 | + |
| 19 | +class Tuo : virtual public Animal { |
| 20 | +}; |
| 21 | + |
| 22 | +class SheepTuo : public Sheep, public Tuo { |
| 23 | +}; |
| 24 | + |
| 25 | +void test01() { |
| 26 | + SheepTuo st; |
| 27 | + st.Tuo::m_Age = 200; |
| 28 | + st.Sheep::m_Age = 100; |
| 29 | + |
| 30 | + cout << "st.Sheep::m_Age = " << st.Sheep::m_Age << endl; |
| 31 | + cout << "st.Tuo::m_Age = " << st.Tuo::m_Age << endl; |
| 32 | + cout << "st.m_Age = " << st.m_Age << endl; |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +class Animal2 { |
| 37 | +public: |
| 38 | + //Speak函数就是虚函数 |
| 39 | + //函数前面加上virtual关键字,变成虚函数,那么编译器在编译的时候就不能确定函数调用了。 |
| 40 | + virtual void speak() { |
| 41 | + cout << "动物在说话" << endl; |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +class Cat : public Animal2 { |
| 46 | +public: |
| 47 | + void speak() { |
| 48 | + cout << "小猫在说话" << endl; |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +class Dog : public Animal2 { |
| 53 | +public: |
| 54 | + |
| 55 | + void speak() { |
| 56 | + cout << "小狗在说话" << endl; |
| 57 | + } |
| 58 | + |
| 59 | +}; |
| 60 | +//我们希望传入什么对象,那么就调用什么对象的函数 |
| 61 | +//如果函数地址在编译阶段就能确定,那么静态联编 |
| 62 | +//如果函数地址在运行阶段才能确定,就是动态联编 |
| 63 | + |
| 64 | +void DoSpeak(Animal2 &animal) { |
| 65 | + animal.speak(); |
| 66 | +} |
| 67 | +// |
| 68 | +//多态满足条件: |
| 69 | +//1、有继承关系 |
| 70 | +//2、子类重写父类中的虚函数 |
| 71 | +//多态使用: |
| 72 | +//父类指针或引用指向子类对象 |
| 73 | + |
| 74 | +void test02() { |
| 75 | + Cat cat; |
| 76 | + DoSpeak(cat); |
| 77 | + |
| 78 | + Dog dog; |
| 79 | + DoSpeak(dog); |
| 80 | +} |
| 81 | + |
| 82 | +//普通实现 |
| 83 | +class Calculator { |
| 84 | +public: |
| 85 | + int getResult(string oper) const { |
| 86 | + if (oper == "+") { |
| 87 | + return m_Num1 + m_Num2; |
| 88 | + } else if (oper == "-") { |
| 89 | + return m_Num1 - m_Num2; |
| 90 | + } else if (oper == "*") { |
| 91 | + return m_Num1 * m_Num2; |
| 92 | + } |
| 93 | + //如果要提供新的运算,需要修改源码 |
| 94 | + } |
| 95 | + |
| 96 | +public: |
| 97 | + int m_Num1; |
| 98 | + int m_Num2; |
| 99 | +}; |
| 100 | + |
| 101 | +void test03() { |
| 102 | + //普通实现测试 |
| 103 | + Calculator c; |
| 104 | + c.m_Num1 = 10; |
| 105 | + c.m_Num2 = 10; |
| 106 | + cout << c.m_Num1 << " + " << c.m_Num2 << " = " << c.getResult("+") << endl; |
| 107 | + cout << c.m_Num1 << " - " << c.m_Num2 << " = " << c.getResult("-") << endl; |
| 108 | + cout << c.m_Num1 << " * " << c.m_Num2 << " = " << c.getResult("*") << endl; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +//多态实现 |
| 113 | +//抽象计算器类 |
| 114 | +//多态优点:代码组织结构清晰,可读性强,利于前期和后期的扩展以及维护 |
| 115 | +class AbstractCalculator { |
| 116 | +public : |
| 117 | + |
| 118 | + virtual int getResult() { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + int m_Num1; |
| 123 | + int m_Num2; |
| 124 | +}; |
| 125 | + |
| 126 | +//加法计算器 |
| 127 | +class AddCalculator : public AbstractCalculator { |
| 128 | +public: |
| 129 | + int getResult() { |
| 130 | + return m_Num1 + m_Num2; |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +//减法计算器 |
| 135 | +class SubCalculator : public AbstractCalculator { |
| 136 | +public: |
| 137 | + int getResult() { |
| 138 | + return m_Num1 - m_Num2; |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +//乘法计算器 |
| 143 | +class MulCalculator : public AbstractCalculator { |
| 144 | +public: |
| 145 | + int getResult() { |
| 146 | + return m_Num1 * m_Num2; |
| 147 | + } |
| 148 | +}; |
| 149 | + |
| 150 | + |
| 151 | +void test04() { |
| 152 | + //创建加法计算器 |
| 153 | + AbstractCalculator *abc = new AddCalculator; |
| 154 | + abc->m_Num1 = 10; |
| 155 | + abc->m_Num2 = 10; |
| 156 | + cout << abc->m_Num1 << " + " << abc->m_Num2 << " = " << abc->getResult() << endl; |
| 157 | + delete abc; //用完了记得销毁 |
| 158 | + |
| 159 | + //创建减法计算器 |
| 160 | + abc = new SubCalculator; |
| 161 | + abc->m_Num1 = 10; |
| 162 | + abc->m_Num2 = 10; |
| 163 | + cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc->getResult() << endl; |
| 164 | + delete abc; |
| 165 | + |
| 166 | + //创建乘法计算器 |
| 167 | + abc = new MulCalculator; |
| 168 | + abc->m_Num1 = 10; |
| 169 | + abc->m_Num2 = 10; |
| 170 | + cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc->getResult() << endl; |
| 171 | + delete abc; |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +int main() { |
| 176 | + test01(); |
| 177 | + test02(); |
| 178 | + test03(); |
| 179 | + test04(); |
| 180 | + return 0; |
| 181 | +} |
0 commit comments