47.多态

1.多态基本概念

  多态是面向对象程序设计语言中数据抽象和继承之外的第三个基本特征。

  多态性(polymorphism)提供接口与具体实现之间的另一层隔离,从而将”what”和”how”分离开来。多态性改善了代码的可读性和组织性,同时也使创建的程序具有可扩展性,项目不仅在最初创建时期可以扩展而且当项目在需要有新的功能时也能扩展

  c++支持编译时多态(静态多态)和运行时多态(动态多态),运算符重载和函数重载就是编译时多态,而派生类和虚函数实现运行时多态。

  静态多态和动态多态的区别就是函数地址是早绑定(静态联编)还是晚绑定(动态联编)。如果函数的调用,在编译阶段就可以确定函数的调用地址,并产生代码,就是静态多态(编译时多态),就是说地址是早绑定的。而如果函数的调用地址不能编译不能在编译期间确定,而需要在运行时才能决定,这这就属于晚绑定(动态多态,运行时多态)。

//计算器
class Caculator{
public:
	void setA(int a){
		this->mA = a;
	}
	void setB(int b){
		this->mB = b;
	}
	void setOperator(string oper){
		this->mOperator = oper;
	}
	int getResult(){
		
		if (this->mOperator == "+"){
			return mA + mB;
		}
		else if (this->mOperator == "-"){
			return mA - mB;
		}
		else if (this->mOperator == "*"){
			return mA * mB;
		}
		else if (this->mOperator == "/"){
			return mA / mB;
		}
	}
private:
	int mA;
	int mB;
	string mOperator;
};

//这种程序不利于扩展,维护困难,如果修改功能或者扩展功能需要在源代码基础上修改
//面向对象程序设计一个基本原则:开闭原则(对修改关闭,对扩展开放)

//抽象基类
class AbstractCaculator{
public:
	void setA(int a){
		this->mA = a;
	}
	virtual void setB(int b){
		this->mB = b;
	}
	virtual int getResult() = 0;
protected:
	int mA;
	int mB;
};

//加法计算器
class PlusCaculator : public AbstractCaculator{
public:
	virtual int getResult(){
		return mA + mB;
	}
};

//减法计算器
class MinusCaculator : public AbstractCaculator{
public:
	virtual int getResult(){
		return mA - mB;
	}
};

//乘法计算器
class MultipliesCaculator : public AbstractCaculator{
public:
	virtual int getResult(){
		return mA * mB;
	}
};

void DoBussiness(AbstractCaculator* caculator){
	int a = 10;
	int b = 20;
	caculator->setA(a);
	caculator->setB(b);
	cout << "计算结果:" << caculator->getResult() << endl;
	delete caculator;
}

2.视频内容

程序1:

#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS 1
//2022年10月18日20:03:17
#include <iostream>
using namespace std;

class People
{
public:
    //虚函数
    virtual void Mypro()
    {

    }
};

class wangzhaojun:public People
{
public:
    //重写父类的虚函数
    virtual void Mypro()
    {
        cout << "约王昭君" << endl;
    }
};

class diaochan:public People
{
public:
    //重写父类的虚函数
    virtual void Mypro()
    {
        cout << "约貂蝉" << endl;
    }
};

class xishi :public People
{
public:
    //重写父类的虚函数
    virtual void Mypro()
    {
        cout << "约西施" << endl;
    }
};

//同一个操作
void doLogin(People *pro)
{
    pro->Mypro();
}

void test()
{
    People *pro = NULL;
    pro = new xishi;
    doLogin(pro);//不同的对象
    delete pro;

    pro = new wangzhaojun;
    doLogin(pro);//不同的对象
    delete pro;

    pro = new diaochan;
    doLogin(pro);//不同的对象
    delete pro;
}
int main()
{
    test();

    system("pause");
    return EXIT_SUCCESS;
}

输出结果:

约西施
约王昭君
约貂蝉
请按任意键继续. . .


1.多态(重点难点)

1.什么是多态:

同一个操作作用于不同的对象,可以有不同的解释,会产生不同的效果,这就是多态。

2.多态有什么用

1.可以解决项目中的紧耦合问题,提供程序的可扩展性

2.应用程序不必为每一个子类的功能调用编写代码,只需要对抽象的父类进行处理

3.多态发生的三个条件

1.有继承。2.重写父类的虚函数。3.父类指针指向子类对象

参考资料

参考资料来源于黑马程序员等

原文链接: https://www.cnblogs.com/codemagiciant/p/17149712.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    47.多态

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/319941

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月24日 下午3:31
下一篇 2023年2月24日 下午3:32

相关推荐