7多太
7.1 多太的基本概念
多太的基本概念
多太分为两类:
静态多太:函数重载和运算符重载属于静态多太,复用了同名函数
动态多太:派生类和虚函数实现运行时多态静态多太和动态多太的区别
静态多太:编译时确定函数地址,编译器根据函数名和参数类型确定函数地址
动态多太:运行时确定函数地址,虚函数表中存放函数地址
#include <iostream>
#include <string>using namespace std;
class Animal {
public:virtual void speak() {cout << "动物在说话" << endl; }
};
class Cat : public Animal {
public:void speak() {cout << "猫在说话" << endl;}
};
class Dog : public Animal {
public:void speak() {cout << "狗在说话" << endl;}
};
void doSpeak(Animal &animal) {animal.speak();
}void test01() {Cat cat;doSpeak(cat);Dog dog;doSpeak(dog);
}
int main(int argc, char const *argv[]) {test01();return 0;
}
7.2 多太案例 - 计算器类
#include <iostream>
#include <string>using namespace std;
class Calculator {
public:int getResult(string oper) {if (oper == "+"){return m_Num1 + m_Num2;}else if (oper == "-"){return m_Num1 - m_Num2;}else if (oper == "*"){return m_Num1 * m_Num2;}return 0;}int m_Num1;int m_Num2;
};void test01() {Calculator c;c.m_Num1 = 10;c.m_Num2 = 20;cout << c.m_Num1 << " + " << c.m_Num2 << " = " << c.getResult("+") << endl;cout << c.m_Num1 << " - " << c.m_Num2 << " = " << c.getResult("-") << endl;cout << c.m_Num1 << " * " << c.m_Num2 << " = " << c.getResult("*") << endl;
}
class AbstractCalculator {
public:virtual int getResult() {return 0;}int m_Num1;int m_Num2;
};
class AddCalculator : public AbstractCalculator {
public:int getResult() {return m_Num1 + m_Num2;}
};
class SubCalculator : public AbstractCalculator {
public:int getResult() {return m_Num1 - m_Num2;}
};
class MulCalculator : public AbstractCalculator {
public:int getResult() {return m_Num1 * m_Num2;}
};void test02() {AbstractCalculator* abc = new AddCalculator;abc->m_Num1 = 10;abc->m_Num2 = 20;cout << abc->m_Num1 << " + " << abc->m_Num2 << " = " << abc->getResult() << endl;delete abc;abc = new SubCalculator;abc->m_Num1 = 10;abc->m_Num2 = 20;cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc->getResult() << endl;delete abc;abc = new MulCalculator;abc->m_Num1 = 10;abc->m_Num2 = 20;cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc->getResult() << endl;delete abc;
}int main(int argc, char const *argv[]) {test02();return 0;
}
7.3 纯虚函数和抽象类
// 纯虚函数和抽象类// 在多态中,通常父类中的实现是毫无意义的,主要是调用子类的实现。因此,父类函数一般声明为纯虚函数。// 纯虚函数语法:virtual 返回值类型 函数名 (参数列表) = 0;// 当类中拥有纯虚函数时,这个类也称为抽象类。// 抽象类的特点:
// 1. 无法实例化对象
// 2. 子类必须重写父类中的纯虚函数,否则也属于抽象类
#include <iostream>
#include <string>using namespace std;class Base {
public:virtual void func() = 0;
};class Son : public Base {
public:virtual void func() {cout << "Son func" << endl;}
};void test01() {Base *base = new Son;base->func();}int main(int argc, char const *argv[]) {test01();return 0;
}
多态案例 - 制作饮品
#include <iostream>
#include <string>using namespace std;class AbstractDrink {
public:virtual void Boil() = 0;virtual void Brew() = 0;virtual void PourInCup() = 0;virtual void PutSomething() = 0;void make() {Boil();Brew();PourInCup();PutSomething();}
};
class Coffee : public AbstractDrink {
public:virtual void Boil() {cout << "煮矿泉水" << endl;}virtual void Brew() {cout << "冲泡咖啡" << endl;}virtual void PourInCup() {cout << "倒入杯中" << endl;}void PutSomething() {cout << "加入糖和牛奶" << endl;}
};
class Tea : public AbstractDrink {
public:void Boil() {cout << "煮雪水" << endl;}void Brew() {cout << "冲泡茶叶" << endl;}void PourInCup() {cout << "倒入杯中" << endl;}void PutSomething() {cout << "加入柠檬" << endl;}
};void MakeDrink(AbstractDrink *drink) {drink->make();delete drink;
}void test01() {Coffee *coffee = new Coffee;MakeDrink(coffee);Tea *tea = new Tea;MakeDrink(tea);
}int main(int argc, char const *argv[]) {test01();return 0;
}
7.5 虚析构和纯虚析构
// 虚析构和纯虚析构//多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
//解决方式:将父类的析构函数改为虚析构或者纯虚析构//虚析构和纯虚析构的共性:
// 可以解决父类指针释放子类对象时,无法调用到子类的析构代码问题
// 都需要有具体的函数实现//虚析构和纯虚析构的区别:
// 如果是纯虚析构函数,该类属于抽象类,无法实例化
#include <iostream>
#include <string>using namespace std;class Animal {
public:Animal(){cout << "Animal构造函数调用" << endl;}virtual void speak() = 0;virtual ~Animal() = 0;
};Animal::~Animal() {cout << "Animal析构函数调用" << endl;
}class Cat : public Animal {
public:Cat(string name) {cout << "Cat构造函数调用" << endl;this->name = new string(name);}void speak() {cout << *name << "小猫在说话" << endl;}string *name;~Cat() {if (this->name != NULL){delete this->name;}cout << "Cat析构函数调用" << endl;}
};void test01() {Animal *animal = new Cat("Tom");animal->speak();delete animal;
}int main(int argc, char const *argv[]) {test01();return 0;
}
// 总结:
// 1. 虚析构和纯虚析构就是用来解决父类指针释放子类对象,会导致子类对象的析构和释放不执行的问题
// 2. 如果子类中没有堆区数据,可以不写虚析构和纯虚析构
// 3. 拥有纯虚析构函数的类也属于抽象类
7.6 多态练习-电脑组装
#include <iostream>
#include <string>using namespace std;
class CPU {
public:virtual void calculate() = 0;
};
class VideoCard {
public:virtual void display() = 0;
};
class Memory {
public:virtual void storage() = 0;
};
class Computer {
public:Computer(CPU *c, VideoCard *v, Memory *m) {cpu = c;vc = v;mem = m;}CPU *cpu;VideoCard *vc;Memory *mem;void work() {cpu->calculate();vc->display();mem->storage();}~Computer() {cout << "computer 析构函数调用" << endl;if (cpu != NULL) {delete cpu;}if (vc != NULL) {delete vc;}if (mem != NULL) {delete mem;}}
};
class IntelCPU : public CPU {
public:void calculate() {cout << "intel cpu 开始计算了" << endl;}
};
class IntelVideoCard : public VideoCard {
public:void display() {cout << "intel 显卡开始显示了" << endl;}
};
class IntelMemory : public Memory {
public:void storage() {cout << "intel 内存条开始存储了" << endl;}
};class LenovoCPU : public CPU {
public:void calculate() {cout << "Lenovo cpu 开始计算了" << endl;}
};
class LenovoVideoCard : public VideoCard {
public:void display() {cout << "Lenovo 显卡开始显示了" << endl;}
};
class LenovoMemory : public Memory {
public:void storage() {cout << "Lenovo 内存条开始存储了" << endl;}
};void test01() {Computer *computer1 = new Computer(new IntelCPU, new IntelVideoCard, new IntelMemory);computer1->work();delete computer1;cout << "-----------------" << endl;computer1 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);computer1->work();delete computer1;
}int main(int argc, char const *argv[]) {test01();return 0;
}