1.多态的基本语法
#include<iostream>
using namespace std ;
#include<string>
class Animal{
public:
virtual void speak() //virtual关键字构造父类中的虚函数。
{
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类(父类)的引用。
{
animal.speak();
}
void test01(){
Cat C1;
Dog D1;
dospeak(C1); //实际调用时传入Cat,Dog类(子类),可直接调用。
dospeak(D1); //通过虚函数的设计,实现了传入不同的子类,调用各自对应的同名函数。
}
int main () {
test01();
system("pause");
return 0;
}*/
/*
采用多态的条件&方法:
1.具有继承关系
2.子类中重写父类的虚函数
3.实际调用中采用父类的引用或指针指向子类,可以实现不同的子类不同的响应。
*/
2.多态的具体案例
ex1:饮品制作。
#include<iostream>
using namespace std ;
#include<string>
class Absdrink {
public:
virtual void zhushui()=0;//纯虚函数
virtual void zhuliao()=0;
virtual void fuliao()=0;
virtual void wancheng()=0;
void work(){
zhushui();
zhuliao();
fuliao();
wancheng();
cout<<"---------------------------------------------------------------"<<endl;
}
};
class coffee:public Absdrink{
public:
void zhushui(){
cout<<"煮矿泉水"<<endl;
}
void zhuliao(){
cout<<"加入咖啡"<<endl;
}
void fuliao(){
cout<<"加入糖"<<endl;
}
void wancheng() {
cout<<"制作咖啡完成"<<endl;
}
};
class tea:public Absdrink{
public:
void zhushui(){
cout<<"煮井水"<<endl;
}
void zhuliao(){
cout<<"加入茶叶"<<endl;
}
void fuliao(){
cout<<"加入金银花"<<endl;
}
void wancheng() {
cout<<"制作茶完成"<<endl;
}
};
void Makedrink(){
Absdrink *p = new coffee; //此时只能采用指针写法,因为使用的是纯虚函数,无法实例化对象,但可以创建指针。
p->work();
delete p;
p = new tea;
p->work();
delete p;
}
int main () {
Makedrink();
system("pause");
return 0;
}
ex2:电脑组装
#include<iostream>
using namespace std ;
#include<string>
class AbsCPU{
public:
virtual void JiSuan() = 0;
};
class AbsXK{
public:
virtual void XianShi() = 0;
};
class AbsNC{
public:
virtual void NeiCun() = 0;
};
class LCPU:public AbsCPU{
public:
void JiSuan(){
cout<<"联想CPU正在计算"<<endl;
}
};
class LXK:public AbsXK{
public:
void XianShi(){
cout<<"联想显卡正在显示"<<endl;
}
};
class LNC:public AbsNC{
public:
void NeiCun(){
cout<<"联想内存正在存储"<<endl;
}
};
class ICPU:public AbsCPU{
public:
void JiSuan(){
cout<<"英特尔CPU正在计算"<<endl;
}
};
class IXK:public AbsXK{
public:
void XianShi(){
cout<<"英特尔显卡正在显示"<<endl;
}
};
class INC:public AbsNC{
public:
void NeiCun(){
cout<<"英特尔内存正在存储"<<endl;
}
};
class Computer{
public:
Computer(AbsCPU *cpu,AbsXK *xk,AbsNC *nc){
m_cpu = cpu;
m_xk = xk;
m_nc = nc;
}
void work(){
m_cpu->JiSuan();
m_xk->XianShi();
m_nc->NeiCun();
cout<<"----------------------------------------------------------------"<<endl;
}
AbsCPU *m_cpu;
AbsXK *m_xk;
AbsNC *m_nc;
};
void test01(){
AbsCPU *p1 = new LCPU;
AbsXK *p2 = new LXK;
AbsNC *p3 = new LNC;
Computer C1(p1,p2,p3);
C1.work();
delete p1;
delete p2;
delete p3;
}
void test02(){
AbsCPU *p1 = new ICPU;
AbsXK *p2 = new IXK;
AbsNC *p3 = new INC;
Computer C2(p1,p2,p3);
C2.work();
delete p1;
delete p2;
delete p3;
}
void test03(){
AbsCPU *p1 = new ICPU;
AbsXK *p2 = new IXK;
AbsNC *p3 = new LNC;
Computer C3(p1,p2,p3);
C3.work();
delete p1;
delete p2;
delete p3;
}
int main () {
test01();
test02();
test03();
system("pause");
return 0;
}
本文是根据B站内容:《黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难》:https://www.bilibili.com/video/BV1et411b73Z?p=314 学习整理笔记。