多态和虚函数

一、什么是多态:同一个实体,同时具有多种形式

  1. 向不同的对象发送同一条消息(即函数调用)
  2. 不同的对象在接收时会产生不同的行为(即不同的函数实现)

二、多态的两种方式

1. 函数重载的方式(静态多态或编译时多态)

	//使用函数重载的方式
	//静态多态或是编译时多态
	void MoveRole(const Warrior &warrior)
	{
		warrior.Move();
	}
	void MoveRole(const Archmage &archmage)
	{
		archmage.Move();
	}
	template <typename T> void MoveRole(const T& vec)
	{
		for (auto it : vec)
		{
			it->Move();
		}
	}

2. 函数重写的方式(动态多态或运行时多态)

  1. 要实现C++函数重写,必须要先把父类的成员函数设定为虚函数

    virtual 返回值 函数名();
    

    注意1:

    1. 派生类重写基类方法Move()时可以加上override关键字表示重写
    2. override关键字为C++11标准后新加入的,用来明确派生类重写基类继承过来的虚函数

    注意2:

    1. 为了能够让同一个函数操作不同类型的子类对象,所以我们把参数类型定义成了基类对象
    2. 当传递Hero类型的子类类型时,参数类型可以自动转化(子类对象可以赋值给父类对象)
    void MoveRole(const Hero &hero)
    {
    	//传参时可以是任意的派生类对象
    	hero.Move();
    }
    

    向上转型:把子类对象转换成父类对象

    1. 向上转型是安全的
    2. 向上转型可以自动完成(自动类型转换)
    3. 向上转型的过程中会丢失子类信息

    向下转型:把子类对象强制转换成父类对象 不安全

三、代码示例

#pragma once
#include <iostream>
#include <string>

using namespace std;

/*位置类(坐标类)*/
class Point
{
public:
	Point();
	Point(int x, int y);
	~Point();
	
	int GetX() { return m_x; }
	int GetY() { return m_y; }
	void SetX(int x) { m_x = x; }
	void SetY(int y) { m_y = y; }

	friend ostream & operator<< (ostream &out, const Point &position)
	{
		out << "(" << position.m_x << "," << position.m_y << ")";
		return out;
	}

private:
	int m_x;
	int m_y;
};

Point::Point()
{
}
Point::Point(int x, int y):m_x(x),m_y(y)
{
}

Point::~Point()
{
}

/*战斗单位类*/
class BattleUnit
{
public:
	BattleUnit();
	BattleUnit(const string &name);
	~BattleUnit();

	const string &GetName() { return m_strName; }
	void SetPoint(int x, int y) { m_point = Point(x, y); }
	const Point &GetPoint() { return m_point; }

	//纯虚函数 不能直接实例化对象
	virtual void Fight(BattleUnit &other) = 0;
	virtual void Move(int x, int y) = 0;
	virtual void Move(const Point &position) = 0;

private:
	string m_strName;
	int m_maxHp;
	int m_curHp;
	int m_attDistance;	//当前对象的攻击距离
	Point m_point;		//当前对象坐标值
};

BattleUnit::BattleUnit(const string& name):m_strName(name),m_point(0,0)
{
	m_maxHp = 100;
	m_curHp = 100;
	m_attDistance = 100;
}
BattleUnit::BattleUnit()
{
}

/*陆战队员类*/
BattleUnit::~BattleUnit()
{
}

class Marine : public BattleUnit
{
public:
	Marine();
	Marine(const string &name);
	~Marine();

	void Fight(BattleUnit &other) override;
	void Move(int x, int y) override {};
	void Move(const Point &position) override {};
private:

};

Marine::Marine()
{
}
Marine::Marine(const string &name) : BattleUnit(name)
{
}

Marine::~Marine()
{
}
void Marine::Fight(BattleUnit &other)
{
	//在子类中调用父类的同名方法
	//BattleUnit::Fight(other);
	cout << "陆战队员:" << GetName() << "正在攻击敌人" << other.GetName() << endl;
}

/*攻城坦克*/
class SiegeTank : public BattleUnit
{
public:
	SiegeTank();
	SiegeTank(const string &name);
	~SiegeTank();

	virtual void Fight(BattleUnit &other) {};
	virtual void Move(int x, int y);
	virtual void Move(const Point &position) {};

private:

};

SiegeTank::SiegeTank()
{
}
SiegeTank::SiegeTank(const string &name) : BattleUnit(name)
{

}

SiegeTank::~SiegeTank()
{
}
void SiegeTank::Move(int x, int y)
{
	SetPoint(x, y);
	cout << "攻城坦克:" << GetName() << "收到移动命令" << GetPoint() << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的继承、多函数是面向对象编程的重要概念。 继承是指一个类可以从另一个类继承属性和方法。子类可以继承父类的公有成员和保护成员,但不能继承私有成员。通过继承,子类可以重用父类的代码,并且可以添加自己的特定功能。继承可以实现代码的重用和层次化的设计。 多是指同一个函数可以根据不同的对象调用不同的实现。多可以通过函数来实现。函数是在基类中声明为拟的函数,它可以在派生类中被重写。当通过基类指针或引用调用函数时,实际调用的是派生类中的实现。这样可以实现动绑定,即在运行时确定调用的函数函数的原理是通过函数表来实现的。每个包含函数的类都有一个函数表,其中存储了函数的地址。当调用函数时,编译器会根据对象的类型在函数表中查找对应的函数地址并调用。 综上所述,C++中的继承、多函数是实现面向对象编程的重要机制,它们可以提高代码的灵活性和可扩展性。 #### 引用[.reference_title] - *1* *3* [C++多函数函数表](https://blog.csdn.net/weixin_46053588/article/details/121231465)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [c++多函数表内部原理实战详解](https://blog.csdn.net/bitcarmanlee/article/details/124830241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值