C++-2025.7.30

发布于:2025-08-04 ⋅ 阅读:(14) ⋅ 点赞:(0)

#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;
}


网站公告

今日签到

点亮在社区的每一天
去签到