//多态
//动物类
class Animal
{
public:
//函数前面加上virtual关键字,变成虚函数,那么编译器在编译的时候就不能确定函数调用了。
virtual void speak()
{
cout << "动物在说话" << endl;
}
};
//猫类
class Cat :public Animal
{
public:
//重写,函数返回值类型 函数名 参数列表 完全相同
void speak()
{
cout << "小猫在说话" << endl;
}
};
//狗类
class Dog :public Animal
{
public:
void speak()
{
cout << "小狗在说话" << endl;
}
};
//执行说话的函数
//地址早绑定 编译阶段就能确定函数地址
//如果想执行让猫说话,那么这个函数地址就不能提前绑定,需要在运行阶段进行绑定,地址晚绑定
//动态多态满足条件
// 1、有继承关系
//2、子类重写父类的虚函数
//动态多态使用
//父类的指针或者引用 指向子类对象
void doSpeak(Animal& animal) //Animal& animal=cat;
{
animal.speak();
}
void test01()
{
Cat cat;
doSpeak(cat);
Dog dog;
doSpeak(dog);
}
int main()
{
test01();
system("pause");
return 0;
}
不加virtual关键字:
加virtual关键字:
为什么加上 virtual关键字之后会变成四个字节? (指针)vfptr
写了一个虚函数,类的内部结构发生改变,多了一个指针(虚函数表指针),指向虚函数表,虚函数表的内部写的是虚函数的函数入口地址,当子类重写了虚函数,会把自身的虚函数表中的函数替换掉,替换成子类的虚函数,当用父类的引用去指向子类对象的时候,会从子类对象找函数接口入口地址。
两个类的内部结构如图如下:
验证:
不加virtual关键字:
加virtual关键字:
当子类没有发生重写的时候:
当子类发生重写的时候:
#include
#include
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;
}
//如果要提供新的运算,需要修改源码
//在真是开发中 提倡 开闭原则
//开闭原则:对扩展进行开发,对修改进行关闭
}
int m_Num1;
int m_Num2;
};
void test01()
{
//创建计算器的对象
Calculator c;
c.m_Num1 = 10;
c.m_Num2 = 10;
cout << c.m_Num1 << " + " << c.m_Num2 << " = " << c.getResult("+") <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 = 100;
abc->m_Num2 = 100;
cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc -> getResult() << endl;
delete abc;
//创建乘法计算器
abc = new MulCalculator;
abc->m_Num1 = 100;
abc->m_Num2 = 100;
cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc -> getResult() << endl;
delete abc;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
纯虚函数语法: virtual 返回值类型 函数名 (参数列表) = 0 ;
class Base
{
public:
//纯虚函数
//类中只要有一个纯虚函数就称为抽象类
//1 抽象类无法实例化对象
//2 抽象类的子类 必须重写父类中的纯虚函数,否则也属于抽象类
virtual void func() = 0;
};
class Son :public Base
{
public:
virtual void func()
{
cout << "func函数的调用" << endl;
};
};
void test01()
{
/*Base b; //错误,抽象类无法实例化对象
new Base;*/ //错误,抽象类无法实例化对象
Son s; //子类必须重写父类中的纯虚函数,否则无法实例化对象
Base* base = new Son; //父类的指针或者引用指向子类对象
base->func();
}
int main()
{
test01();
system("pause");
return 0;
}
//抽象制作饮品
class AbstractDrinking
{
public:
//煮水
virtual void Boil() = 0;
//冲泡
virtual void Brew() = 0;
//倒入杯中
virtual void PourInCup() = 0;
//加入辅料
virtual void PutSomething() = 0;
//规定流程(制作流程)
void makeDrink() {
Boil();
Brew();
PourInCup();
PutSomething();
}
};
//制作咖啡
class Coffee :public AbstractDrinking
{
public:
//煮水
virtual void Boil()
{
cout << "煮农夫山泉!" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡咖啡!" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "将咖啡倒入杯中!" << endl;
}
//加入辅料
virtual void PutSomething()
{
cout << "加入糖和牛奶!" << endl;
}
};
//制作茶叶
class Tea :public AbstractDrinking
{
public:
//煮水
virtual void Boil()
{
cout << "煮矿泉水!" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡茶叶!" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "将茶叶倒入杯中!" << endl;
}
//加入辅料
virtual void PutSomething()
{
cout << "加入枸杞!" << endl;
}
};
//业务函数
void doWork(AbstractDrinking* abs)
{
abs->makeDrink();
delete abs; //释放
}
void test01()
{
doWork(new Coffee);
cout << "--------------" << endl;
doWork(new Tea);
}
int main()
{
test01();
system("pause");
return 0;
}
#include
using namespace std;
#include
//虚析构和纯虚析构
class Animal
{
public:
Animal()
{
cout << "Animal 构造函数调用!" << endl;
}
//析构函数加上virtual关键字,变成虚析构函数
//利用虚析构可以解决 父类指针释放子类对象时不干净的问题
/* virtual ~Animal()
{
cout << "Animal 析构函数调用!" << endl;
}*/
//纯虚析构 需要声明也需要实现
//和包含普通纯虚函数的类一样,包含了纯虚析构函数的类也是一个抽象类。不能够被实例化。
virtual ~Animal() = 0;
//纯虚函数
virtual void speak() = 0;
};
Animal:: ~Animal()
{
cout << "Animal 纯虚析构函数调用!" << endl;
}
class Cat :public Animal
{
public:
Cat(string name)
{
cout << "Cat构造函数调用!" << endl;
m_Name = new string(name); //堆区
}
virtual void speak()
{
cout << *m_Name << "小猫在说话!" << endl;
}
~Cat()
{
cout << "Cat析构函数调用!" << endl;
if (this->m_Name != NULL) {
delete m_Name;
m_Name = NULL;
}
}
string* m_Name;
};
void test01()
{
Animal* animal = new Cat("Tom");
animal->speak();
//父类指针在析构时候 不会调用子类中析构函数,导致子类如果有堆区属性,出现内存泄漏
//通过父类指针去释放,会导致子类对象可能清理不干净,造成内存泄漏
//怎么解决?给基类增加一个虚析构函数
//虚析构函数就是用来解决通过父类指针释放子类对象
delete animal;
}
int main()
{
test01();
system("pause");
return 0;
}
//多态案例三 - 电脑组装
//抽象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 InterCPU :public CPU
{
public:
virtual void calculate()
{
cout << "Intel的CPU开始计算了!" << endl;
}
};
class IntelVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "Intel的显卡开始显示了!" << endl;
}
};
class IntelMemory :public Memory
{
public:
virtual void storage()
{
cout << "Intel的内存条开始存储了!" << endl;
}
};
//Lenovo厂商
class LenovoCPU :public CPU
{
public:
virtual void calculate()
{
cout << "Lenovo的CPU开始计算了!" << endl;
}
};
class LenovoVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "Lenovo的显卡开始显示了!" << endl;
}
};
class LenovoMemory :public Memory
{
public:
virtual void storage()
{
cout << "Lenovo的内存条开始存储了!" << endl;
}
};
void test01()
{
//第一台电脑零件
CPU* intelCpu = new InterCPU;
VideoCard* intelCard = new IntelVideoCard;
Memory* intelMem = new IntelMemory;
cout << "第一台电脑开始工作:" << endl;
//创建第一台电脑
Computer* computer1 = new Computer(intelCpu, intelCard, intelMem);
computer1->work();
delete computer1;
cout << "-----------------------" << endl;
cout << "第二台电脑开始工作:" << endl;
//第二台电脑组装
Computer* computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);;
computer2->work();
delete computer2;
cout << "-----------------------" << endl;
cout << "第三台电脑开始工作:" << endl;
//第三台电脑组装
Computer* computer3 = new Computer(new LenovoCPU, new IntelVideoCard, new LenovoMemory);;
computer3->work();
delete computer3;
}