1.多态的基本语法
代码
#include <iostream>
using namespace std;
/******************************************/
class Animal {
public://speak函数就是虚函数//函数前面加上virtual关键字,变成虚函数,//那么编译器在编译的时候就不能确定函数调用了。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();
}//多态满足条件:
//1、有继承关系
//2、子类重写父类中的虚函数
//多态使用:
//父类指针或引用指向子类对象void test01() {Cat cat;doSpeak(cat);Dog dog;doSpeak(dog);
}
/******************************************/
int main()
{/******************************************/test01();/******************************************/system("pause");return 0;
}
结果
2.多态的原理剖析
3.多态案例一-计算器类
(1)普通实现代码:
#include <iostream>
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;}//如果要提供新的运算,需要修改源码}
public:int m_Num1;int m_Num2;
};void test01() {//普通实现测试Calculator cal;cal.m_Num1 = 100;cal.m_Num2 = 100;cout << cal.m_Num1 << " + " << cal.m_Num2 << " = " << cal.getResult("+") << endl;cout << cal.m_Num1 << " - " << cal.m_Num2 << " = " << cal.getResult("-") << endl;cout << cal.m_Num1 << " * " << cal.m_Num2 << " = " << cal.getResult("*") << endl;
}
/******************************************/
int main()
{/******************************************/test01();/******************************************/system("pause");return 0;
}
代码结果
(2)多态实现代码:
#include <iostream>
using namespace std;
/******************************************/
//多态实现//抽象计算器类
//多态优点:代码组织结构清晰,可读性强,利于前期和后期的扩展以及维护
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 test01() {//创建加法计算器AbstractCalculator* abc = new AddCalculator;abc->m_Num1 = 100;abc->m_Num2 = 100;cout << abc->m_Num1 << " + " << abc->m_Num2 << " = " << abc->getResult() << endl;delete abc;//用完记得销毁//创建减法计算器abc = new SubCalculator;abc->m_Num1 = 10;abc->m_Num2 = 10;cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc->getResult() << endl;delete abc;//用完记得销毁//创建减法计算器abc = new MulCalculator;abc->m_Num1 = 10;abc->m_Num2 = 10;cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc->getResult() << endl;delete abc;//用完记得销毁}
/******************************************/
int main()
{/******************************************/test01();/******************************************/system("pause");return 0;
}
结果
以下是没有加virtual的情况,不是虚函数,不能形成多态,会运行AbstractCalculator类里面的getResult()函数。
4.纯虚函数和抽象类
#include <iostream>
using namespace std;
/******************************************/
//纯虚函数和抽象类
class Base {
public://纯虚函数//类中只要有一个纯虚函数就称为抽象类//抽象类无法实例化对象//子类必须重写父类中的纯虚函数,否则也属于抽象类virtual void func() = 0;
};class Son :public Base {
public:virtual void func() {cout << "func()调用" << endl;}
};void test01() {//Base base;//错误,抽象类无法实例化对象Base* base = NULL;//base = new Base;//错误,抽象类无法实例化对象base = new Son;base->func();delete base;//记得销毁
}
/******************************************/
int main()
{/******************************************/test01();/******************************************/system("pause");return 0;
}
结果
5.多态案例二-制作饮品
#include <iostream>
using namespace std;
/******************************************/
class AbstractDrinking {
public://煮水virtual void boilWater() = 0;//冲泡virtual void brew() = 0;//倒入杯中virtual void putInCup() = 0;//加入辅料virtual void putSomething() = 0;//制作饮品void makingDrink() {boilWater();brew();putInCup();putSomething();}
};//制作咖啡
class MakeCoffee :public AbstractDrinking {//煮水virtual void boilWater() {cout << "煮矿泉水" << endl;}//冲泡virtual void brew() {cout << "冲泡" << endl;}//倒入杯中virtual void putInCup() {cout << "倒入杯子" << endl;}//加入辅料virtual void putSomething() {cout << "加入蜂蜜和牛奶" << endl;}
};//制作茶水
class MakeTea:public AbstractDrinking {//煮水virtual void boilWater() {cout << "煮自来水" << endl;}//冲泡virtual void brew() {cout << "冲泡" << endl;}//倒入杯中virtual void putInCup() {cout << "倒入杯子" << endl;}//加入辅料virtual void putSomething() {cout << "倒入柠檬" << endl;}
};//制作函数
void doWork(AbstractDrinking* drinking) {drinking->makingDrink();delete drinking;
}void test01() {doWork(new MakeCoffee);cout << endl << "________________________" << endl << endl;doWork(new MakeTea);
}
/******************************************/
int main()
{/******************************************/test01();/******************************************/system("pause");return 0;
}
6.虚析构和纯虚析构
代码:
#include <iostream>
using namespace std;
/******************************************/
//虚析构和纯虚析构
class Animal {
public:Animal() {cout << "Animal构造函数" << endl;}virtual void Speak() = 0;//析构函数前面加上virtual关键字,变成虚析构函数//virtual ~Animal() {// cout << "Animal虚析构函数" << endl;//}virtual ~Animal() = 0;
};Animal::~Animal()
{cout << "Animal纯虚析构函数" << endl;
}
//和包含普通纯虚函数的类一样,包含了纯虚析构函数的类也是一个抽象类,不能够被实例化。class Cat :public Animal {
public:Cat(string name) {cout << "Cat构造函数" << endl;this->m_Name = new string(name);}virtual void Speak() {cout << *this->m_Name << "小猫在说话" << endl;}~Cat() {cout << "Cat析构函数" << endl;if (this->m_Name != NULL) {delete m_Name;this->m_Name = NULL;}}string* m_Name;
};void test01() {Animal* animal = new Cat("Tom");animal->Speak();//通过父类指针去释放,会导致子类对象可能清理不干净,造成内存泄漏//怎么解决?给基类增加一个虚析构函数//虚析构函数就是用来解决: 通过父类指针释放子类对象delete animal;
}/******************************************/
int main()
{/******************************************/test01();/******************************************/system("pause");return 0;
}
结果
7.多态案例三-电脑组装
代码
#include <iostream>
using namespace std;//抽象不同零件类
//抽象CPU类
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* cpu, VideoCard* vc, Memory* mem) {m_cpu = cpu;m_vc = vc;m_mem = mem;}//提供工作的函数void work() {//让零件工作起来,调用接口m_cpu->calculate();m_vc->display();m_mem->storage();}//提供析构函数 释放3个电脑零件~Computer() {//释放CPU零件if (m_cpu != NULL) {delete m_cpu;m_cpu = NULL;}//释放显卡零件if (m_vc != NULL) {delete m_vc;m_vc = NULL;}//释放内存条零件if (m_mem != NULL) {delete m_mem;m_mem = NULL;}}//!!!!不用虚析构,因为子类(当然啦,压根没有子类)没有成员在堆区开辟数据,不需要执行子类析构函数private:CPU* m_cpu;//CPU的零件指针VideoCard* m_vc;//显卡零件指针Memory* m_mem;//内存条零件指针
};//具体厂商
//Intel厂商
class IntelCPU: public CPU {virtual void calculate() {cout << "IntelCPU开始计算了" << endl;}
};
class IntelVideoCard: public VideoCard {virtual void display() {cout << "Intel显卡开始显示了" << endl;}
};
class IntelMemory : public Memory {virtual void storage() {cout << "Intel内存条开始存储了" << endl;}
};
//Lenovo厂商
class LenovoCPU : public CPU {virtual void calculate() {cout << "LenovoCPU开始计算了" << endl;}
};
class LenovoVideoCard : public VideoCard {virtual void display() {cout << "Lenovo显卡开始显示了" << endl;}
};
class LenovoMemory : public Memory {virtual void storage() {cout << "Lenovo内存条开始存储了" << endl;}
};void test01() {cout << "---------创建第二台电脑------------" << endl;//第一台电脑零件CPU* intelCPU = new IntelCPU;VideoCard* intelCard = new IntelVideoCard;Memory* intelMem = new IntelMemory;//创建第一台电脑Computer* computer1 = new Computer(intelCPU, intelCard, intelMem);computer1->work();delete computer1;cout << endl << "---------创建第二台电脑------------" << endl;//创建第二台电脑Computer* computer2 = new Computer(new IntelCPU, new IntelVideoCard, new IntelMemory);computer2->work();delete computer2;
}int main() {test01();system("pause");return 0;
}
结果
错误总结:
这两个名字不能一样,原因如下: