4.7 多态
4.7.1 多态的基本语法
示例代码如下:
#include <iostream>
using namespace std;
//多态的基本概念
//满足动态多态的条件:1、有继承的关系 2、子类要重写父类的虚函数
//重写:函数返回值类型 函数名 参数列表 完全相同
//动态多态的使用:
//父类的指针或者引用 执行子类对象
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()
{
test01();
system("pause");
return 0;
}
4.7.2 多态的原理剖析
这四个字节是一个指针。
当没有发生重写的时候:
发生重写:
虚拟函数表的 父类speak函数换位了子类的speak函数。
4.7.3 多态案例1-计算机类
多态写法:
优点:
代码量变大虽然变大,但是结构清晰,而且再添加新的功能时,不需要对之前的功能源码进行修改。方便修改和使用。
普通写法示例代码:
#include <iostream>
#include <string>
using namespace std;
//多态案例1-计算机类-普通写法和多态实现计算器
//普通写法
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 = 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;
}
int main()
{
test01();
system("pause");
return 0;
}
多态示例代码:
//多态写法
//实现计算机抽象类
class AbsractCalculator
{
public:
virtual int getResult()
{
return 0;
}
int m_Num1;
int m_Num2;
};
//加法计算器类
class AddCalculator :public AbsractCalculator
{
public:
int getResult()
{
return m_Num1 + m_Num2;
}
};
//减法计算器类
class SubCalculator :public AbsractCalculator
{
public:
int getResult()
{
return m_Num1 - m_Num2;
}
};
//乘法计算器类
class MulCalculator :public AbsractCalculator
{
public:
int getResult()
{
return m_Num1 * m_Num2;
}
};
void test02()
{
//多态的使用条件
//父类指针或者引用指向子类对象
AbsractCalculator* 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 = 10;
cout << abc->m_Num1 << "-" << abc->m_Num2 << "=" << abc->getResult() << endl;
delete abc;
}
4.7.4 纯虚函数和抽象类
示例代码如下:
#include <iostream>
#include <string>
using namespace std;
//纯虚函数和抽象类
class Base
{
public:
virtual void func() = 0; //纯虚函数 Base类就称为抽象类
};
class Son :public Base
{
public:
virtual void func()
{
cout << "func调用" << endl;
}
};
void test01()
{
//Son s; //子类必须重写父类中的纯虚函数
Base * base = new Son;
base->func();
}
int main()
{
test01();
system("pause");
return 0;
}
4.7.5 多态案例2-制作饮品
示例代码如下:
#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 makeDrink()
{
Boil();
Brew();
PourInCup();
Putsomething();
}
};
//制作咖啡
class Coffee :public AbstractDrink
{
public:
//煮水
virtual void Boil()
{
cout << "煮矿泉水" << endl;
}
//冲泡
virtual void Brew()
{
cout << "放入咖啡" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入杯中" << endl;
}
//加入辅料
virtual void Putsomething()
{
cout << "加入牛奶、冰糖" << endl;
}
};
//制作茶
class Tea :public AbstractDrink
{
public:
//煮水
virtual void Boil()
{
cout << "煮泉水" << endl;
}
//冲泡
virtual void Brew()
{
cout << "放入茶叶" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入杯中" << endl;
}
//加入辅料
virtual void Putsomething()
{
cout << "加入红糖" << endl;
}
};
//制作函数
void doWork(AbstractDrink * abs)
{
abs->makeDrink();
}
void test02()
{
//制作咖啡
doWork(new Coffee);
cout << "------------------------------" << endl;
//制作茶
doWork(new Tea);
}
void test01()
{
AbstractDrink* abc1 = new Coffee;
abc1->makeDrink();
delete abc1;
cout << "------------------------------" << endl;
AbstractDrink* abc2 = new Tea;
abc2->makeDrink();
delete abc2;
cout << "------------------------------" << endl;
cout << "------------------------------" << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
运行结果:
标签:Num1,Num2,int,void,编程,多态,C++,public From: https://blog.csdn.net/qq_56896418/article/details/140632373