多态和虚函数

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

  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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值