7.30作业

一.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
 
using namespace std;
 
class Bird{
private:
 
public:
	virtual void run(){}
 
	virtual ~Bird() {}
};
 
class Qq:public Bird{
public:
	void run(){
		cout << "企鹅屁颠屁颠的跑走了" << endl;
	}
};
 
class Tuoniao:public Bird{
public:
	void run(){
		cout << "鸵鸟风一般的跑走了" << endl;
	}
};
 
class Laoying:public Bird{
public:
	void run(){
		cout << "老鹰展翅翱翔的飞走了" << endl;
	}
};
 
class Observer{
private:
	Bird* list[10];
	int len = 0;
public:
	// 将对象放进数组中
	void append(Bird* bird) {
		list[len++] = bird;
	}
 
	Observer& operator<<(Bird& bird) {
		append(&bird);
		return *this;
	}
 
	// 打开鸟笼
	void open_cage() {
		for (int i = 0; i < len ; i++) {
			list[i]->run();
		}
	}
};
 
 
 
int main(int argc,const char** argv){
	Qq q;
	Tuoniao t;
	Laoying l;
	Observer o;
	o << q << t << l;
	o.open_cage();
	return 0;
}

二.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
 
using namespace std;
 
class Stu{
private:
 
public:
 
};
 
// 武器 
class Weapon{
private:
	
public:
 
    // 掉落武器
    virtual void drop_out() = 0;  
 
 
	// 装备武器
	virtual void equip() = 0;
 
	
	virtual ~Weapon(){}
};  
 
// 短剑
class Dagger:public Weapon{
private:
 
public:
 
    // 掉落武器
    void drop_out() {
        cout << "掉落短剑" << endl;
    }   
 
	
	// 装备武器
	void equip () {
		cout << "英雄装备短剑" << endl;
	}
 
};  
 
// 长剑
class Rapier:public Weapon{
private:
 
public:
 
    // 掉落武器
    void drop_out() {
        cout << "掉落长剑" << endl;
    }   
 
	// 装备武器
	void equip () {
		cout << "英雄装备长剑" << endl;
	}
 
};  
 
// 弓箭
class Arrow:public Weapon{
private:
 
public:
 
    // 掉落武器
    void drop_out() {
        cout << "掉落弓箭" << endl;
    }
 
	// 装备武器
	void equip () {
		cout << "英雄装备弓箭" << endl;
	}
};
 
// 工厂类
class Factory {
private:
public:
    Weapon* create_object(const string& monstor) {
		if (monstor == "xiaobing") {
			return new Dagger;
		} else if (monstor == "buff") {
			return new Rapier;
		} else if (monstor == "xiaolong") {
			return new Arrow;
		} else {
			return nullptr;
		}
	}    
};
 
class Hero {
private:
	Weapon* weapon = nullptr;
	int hp;
	int atk;
	int dep;
public:
 
	Hero (int hp = 0, int atk = 0, int dep = 0):hp(hp), atk(atk), dep(dep){}
 
	// 选择武器
	void select_weapon(Weapon* weapon) {
		this->weapon = weapon;
	}
 
	// 装备武器
	void equip_weapon() {
		weapon->equip();
	}
 
	// 查看属性值
	void show() {
		cout << "hp:" << hp << endl;
		cout << "atk:" << atk << endl;
		cout << "dep:" << dep << endl;
	}
 
	int gethp(){
		return hp;
	}
 
	void sethp(int hp) {
		this->hp = hp;
	} 
 
	int getatk() {
		return atk;
	}
 
	void setatk(int atk) {
		this->atk = atk;
	}
 
	int getdep() {
		return dep;
	}
 
	void setdep(int dep) {
		this->dep = dep;
	}
};
 
 
int main(int argc,const char** argv){
 
	Hero h;
	while (1) {
		
		int selected;
		cout << "1.打怪" << endl;
		cout << "2.升级装备" << endl;
		cout << "请选择:";
		cin >> selected;
 
		if (1 == selected) {
			cout << "1.小兵" << endl;
			cout << "2.buff" << endl;
			cout << "3.小龙" << endl;
			cout << "请选择要打的怪:";
 
			cin >> selected;
			cout << endl;
 
			Factory f;
			Weapon* weapon = nullptr;
			switch (selected) {
				case 1:
					weapon = f.create_object("xiaobing");
					weapon->drop_out();
					break;
				case 2:
					weapon = f.create_object("buff");
					weapon->drop_out();
					break;
				case 3:
					weapon = f.create_object("xiaolong");
					weapon->drop_out();
					break;
			}
 
			cout << endl;
		} else if (2 == selected){
			
			while (1) {
				cout << "1.装备短剑" << endl;
				cout << "2.装备长剑" << endl;
				cout << "3.装备弓箭" << endl;
				cout << "4.查看属性" << endl;
				cout << "5.返回上一级" << endl;
 
				cout << "请选择:";
				cin >> selected;
				
				if (1 == selected) {
					h.select_weapon(new Dagger);
					h.equip_weapon();
					h.sethp(2);
				} else if (2 == selected) {
					h.select_weapon(new Rapier);
					h.equip_weapon();
					h.setatk(2);
				} else if (3 == selected) {
					h.select_weapon(new Arrow);
					h.equip_weapon();
					h.setdep(2);
				} else if (4 == selected) {
					h.show();
				} else {
					break;
				}
			}
		}
		
 
						
	}
 
	
 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值