C/C++教程

C++ 动态多态

本文主要是介绍C++ 动态多态,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

多态

#include <iostream>
using namespace std;
#include <string>

//多态

//动物类
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();
    //test02();
    
    system("pause");
    return 0;

}

 多态案例

#include <iostream>
using namespace std;
#include <string>

//多态案例

//分别利用普通写法和多态技术实现计算器

//普通写法

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;//操作数1
    int m_Num2;//操作数2

};

void test01()
{
    //创建计算器对象
    Calculator c;
    c.m_Num1 = 10;
    c.m_Num2 = 10;

    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;
}



//利用多态实现计算器
//1、组织结构清晰
//2、可读性强
//3、对于前期和后期扩展以及维护性高
//实现计算器抽象类
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 test02()
{
    //多态使用条件
    //父类指针或者引用指向子类对象

    //加法运算
    AbstractCalculator * abc = new AddCalculator;
    abc->m_Num1 = 10;
    abc->m_Num2 = 10;

    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;

}

 

这篇关于C++ 动态多态的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!