4.7.1 多态的基本概念
多态是c++面向对象三大特性之一
多态分为两类
- 静态多态:函数重载 和运算符重载属于静态多态 复用函数名
- 动态多态:派生类和虚函数实现运行时多态
静态多态和动态多态区别
- 静态多态的函数地址早绑定-编译阶段确定函数地址
- 动态多态的函数地址晚绑定-运行阶段确定函数地址
#include <iostream>
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;
}
};
//地址早绑定 在编译阶段确定函数地址
//如果想执行让猫说话 那么这个函数地址就不能提前绑定,需要在函数执行后再绑定
//动态多态满足条件
//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;
}
4.7.2 动态的原理刨析
4.7.3多态案例--计算器类
#include <iostream>
using namespace std;
//分别用普通写法和多态技术实现计算器
//普通写法
class Calculate
{
public:
int GetResult(string oper)
{
if (oper == "+")
{
return m_Num1 + m_Num2;
}
if (oper == "-")
{
return m_Num1 - m_Num2;
}
if (oper == "*")
{
return m_Num1 * m_Num2;
}
}
//如果想要扩展新的功能,需求修改源码
//在真实的开发中,提倡开闭原则
//开闭原则:对拓展进行开放 对修改进行关闭
int m_Num1;
int m_Num2;
};
void test01()
{
Calculate c1;
c1.m_Num1 = 10;
c1.m_Num2 = 10;
cout << "m_Num1" << "+" << "m_Num2" << "=" << c1.GetResult("+") << endl;
cout << "m_Num1" << "-" << "m_Num2" << "=" << c1.GetResult("-") << endl;
cout << "m_Num1" << "*" << "m_Num2" << "=" << c1.GetResult("*") << endl;
}
//利用多态实现计算器
//多态好处
//1.组织结构非常清晰
//2.可读性强
//3.对于前期和后期拓展维护性高
class BaseCaculate
{
public:
virtual int GetResult()
{
return 0;
}
int m_Num1;
int m_Num2;
};
//加法类
class AddCaculate:public BaseCaculate
{
public:
int GetResult()
{
return m_Num1+m_Num2;
}
};
//减法类
class SubCaculate :public BaseCaculate
{
public:
int GetResult()
{
return m_Num1 - m_Num2;
}
};
//乘法类
class MulCaculate :public BaseCaculate
{
public:
int GetResult()
{
return m_Num1 * m_Num2;
}
};
void test02()
{
BaseCaculate *cal = new AddCaculate;
cal->m_Num1 = 100;
cal->m_Num2 = 100;
cout << cal->m_Num1 << "+" << cal->m_Num2 << "=" << cal->GetResult() << endl;
delete cal;
cal = new SubCaculate;
cal->m_Num1 = 100;
cal->m_Num2 = 100;
cout << cal->m_Num1 << "-" << cal->m_Num2 << "=" << cal->GetResult() << endl;
delete cal;
cal = new MulCaculate;
cal->m_Num1 = 100;
cal->m_Num2 = 100;
cout << cal->m_Num1 << "*" << cal->m_Num2 << "=" << cal->GetResult() << endl;
delete cal;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
4.7.3 纯虚函数和抽象类
在多态中,调用父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容
因此可以将虚函数改为纯虚函数
纯虚函数语法:virtual 返回值类型 函数名 {参数列表}=0;
当类中有了纯虚函数,这个类也称为抽象类
抽象类特点:
无法实例化对象
子类必须重写抽象类中的纯虚函数,否则也属于抽象类
#include <iostream>
using namespace std;
//纯虚函数和抽象类
class Base
{
public:
//纯虚函数
//只要有一个纯虚函数,这个类称为抽象类
//抽象类特点
//1.无法实例化对象
//2.抽象类的子类,必须要重写父类中的纯虚函数,否则也为抽象类
virtual void func() = 0;
};
class Son :public Base
{
public:
//重写父类虚函数
void func()
{
cout << "func 函数调用" << endl;
}
};
void test01()
{
//Base b ;//抽象类无法实例化对象
//new Base;//抽象类无法实例化对象
Son s;//子类必须重写父类中的纯虚函数,否则无法实例化对象
Base *base = new Son;
base->func();
delete base;
}
int main()
{
test01();
system("pause");
return 0;
}
4.7.4 制作饮品实例
#include <iostream>
using namespace std;
//制作饮料
//创建基函数
class AbstractDrinking
{
public:
//煮水
virtual void boil() = 0;
//冲泡
virtual void brew() = 0;
//倒入
virtual void pullincup() = 0;
//加东西
virtual void putsometing() = 0;
void make()
{
boil();
brew();
pullincup();
putsometing();
}
};
//制作咖啡
class Coffee :public AbstractDrinking
{
public:
//煮水
virtual void boil()
{
cout << "煮农夫山泉水" << endl;
}
//冲泡
virtual void brew()
{
cout << "冲泡咖啡" << endl;
}
//倒入
virtual void pullincup()
{
cout << "倒入马克杯中" << endl;
}
//加东西
virtual void putsometing()
{
cout << "加入牛奶和白糖" << endl;
}
};
class Tea :public AbstractDrinking
{
public:
//煮水
virtual void boil()
{
cout << "煮泉水" << endl;
}
//冲泡
virtual void brew()
{
cout << "冲泡茶叶" << endl;
}
//倒入
virtual void pullincup()
{
cout << "倒入玻璃中" << endl;
}
//加东西
virtual void putsometing()
{
cout << "加入枸杞" << endl;
}
};
void doWork(AbstractDrinking *abc)
{
abc->make();
delete abc;
}
void test01()
{
doWork(new Coffee);
cout << "------------------" << endl;
doWork(new Tea);
cout << "------------------" << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.7.5 虚析构和纯虚析构
多态使用时,如果子类有属性开辟到堆区,那么父类指针在释放时无法调用子类的析构代码
解决方式:将父类中的析构函数改为虚析构或者纯虚析构
虚析构和纯虚析构共性:
- 可以解决父类指针释放子类对象
- 都需要具体的函数实现
虚析构和纯虚析构区别:
如果是纯虚析构,该类属于抽象类,无法实例对象
#include <iostream>
using namespace std;
#include<string>
//虚析构和纯虚析构
class Animal
{
public:
Animal()
{
cout << "animal构造函数在调用" << endl;
}
//利用虚析构可以解决父类指针释放子类对象不干净的问题
/*virtual ~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;
m_Name = new string(name);
}
void speak()
{
cout << *m_Name<<"小猫在说话" << endl;
}
string *m_Name;//将姓名开辟到堆区
~Cat()
{
if (m_Name != NULL)
{
cout << "cat析构函数调用" << endl;
delete m_Name;
m_Name = NULL;
}
}
};
void test01()
{
Animal *abc = new Cat("Tom");
abc->speak();
//父类指针在析构时候,不会调用子类中析构代码,故出现内存泄露
delete abc;
}
int main()
{
test01();
system("pause");
return 0;
}
虚析构语法:
virtual ~类名(){};
纯虚析构语法:
virtual ~类名()=0;
类名::~类名(){};
总结:
- 虚析构或纯虚析构就是用来解决父类指针释放子类对象
- 如果子类没有堆区数据,可以不写为虚析构或纯虚析构
- 拥有纯虚析构的类也属于抽象类
4.7.6电脑组装具体实现
#include <iostream>
using namespace std;
//抽象不同零件类
//抽象CPU类
class CPU
{
public:
virtual void Calculate() = 0;
};
//抽象显卡类
class VideoCard
{
public:
virtual void Show() = 0;
};
//抽象内存条类
class Memory
{
public:
virtual void Storage() = 0;
};
//电脑类
class Computer
{
public:
Computer (CPU *cpu,VideoCard *vd,Memory *mr)
{
m_cpu = cpu;
m_vd = vd;
m_mr = mr;
}
//提供工作的函数
void work()
{
m_cpu->Calculate();
m_vd->Show();
m_mr->Storage();
}
~Computer()
{
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
if (m_mr != NULL)
{
delete m_mr;
m_mr = NULL;
}
if (m_vd != NULL)
{
delete m_vd;
m_vd = NULL;
}
}
private:
CPU *m_cpu;
VideoCard *m_vd;
Memory *m_mr;
};
//创建Intel厂商
class IntelCPU :public CPU
{
public:
virtual void Calculate()
{
cout << "IntelCPU开始计算啦" << endl;
}
};
class IntelVideoCard :public VideoCard
{
public:
virtual void Show()
{
cout << "IntelVideoCard开始显示啦" << endl;
}
};
class IntelMemory :public Memory
{
public:
virtual void Storage()
{
cout << "IntelMemory开始存储啦" << endl;
}
};
//创建Lenovo厂商
class LenovoCPU :public CPU
{
public:
virtual void Calculate()
{
cout << "LenovoCPU开始计算啦" << endl;
}
};
class LenovoVideoCard :public VideoCard
{
public:
virtual void Show()
{
cout << "LenovoVideoCard开始显示啦" << endl;
}
};
class LenovoMemory :public Memory
{
public:
virtual void Storage()
{
cout << "LenovoMemory开始存储啦" << endl;
}
};
void test01()
{
//第一台电脑零件
CPU *cpu = new IntelCPU;
VideoCard *vd = new IntelVideoCard;
Memory *mr = new IntelMemory;
//第一台电脑
Computer *computer1 = new Computer(cpu,vd,mr);
computer1->work();
delete computer1;
cout << "------------" << endl;
Computer *computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);
//computer2.work();
computer2->work();
delete computer2;
}
int main()
{
test01();
system("pause");
return 0;
}
类与对象的三大特性:封装、继承、多态到现在为止就学完啦。