⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 singelhorse_1.cpp

📁 经典著作《设计模式》中将策略模式定义为:定义一系列的算法
💻 CPP
字号:
// SingelHorse_1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//武器类
class Weapon
{
public:
	virtual void Assault() = 0; //纯虚函数
};
//长枪类
class Lance : public Weapon
{
public:
	virtual void Assault()
	{
		cout << " I kill enemy with the Lance and can kill 10 every time!" << endl;
	}
};
//宝剑类
class Sword : public Weapon
{
public:
	virtual void Assault()
	{
		cout << " I kill enemy with the sword and can kill 20 every time!" << endl;
	}
};
//武将类
class General
{
private:
	string m_strName;
	Weapon *m_pWeapon;
public:
	General(string strName):m_strName(strName),m_pWeapon(new Lance())
	{
	}
	~General()
	{
		if ( m_pWeapon != NULL ) delete m_pWeapon; 
	}
	void SetWeapon(Weapon *pWeapon)
	{
		if ( m_pWeapon != NULL ) delete m_pWeapon; 

		m_pWeapon = pWeapon;
	}
	void performAssault()
	{
		m_pWeapon->Assault();
	}
	void Advance()
	{
		cout << "Go,Go,Go!!!" << endl;
	}
};

int main(int argc, char* argv[])
{
	//生成赵云对象
	General zy("Zhao Yun");
	//前进
	zy.Advance();
	//攻击
	zy.performAssault();
    //更换武器
	zy.SetWeapon(new Sword());
	zy.Advance();
	zy.performAssault();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -