cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
// 武器基类
class Weapon {
public:
virtual ~Weapon() {}
virtual string getName() const = 0; // 获取武器名称
virtual int getAtk() const = 0; // 获取武器攻击力
};
// 具体武器类
class Sword : public Weapon {
public:
string getName() const override { return "Sword"; }
int getAtk() const override { return 10; }
};
class Blade : public Weapon {
public:
string getName() const override { return "Blade"; }
int getAtk() const override { return 7; }
};
class Axe : public Weapon {
public:
string getName() const override { return "Axe"; }
int getAtk() const override { return 15; }
};
// 英雄类
class Hero {
public:
enum class Profession { Warrior, Archer, Mage };
Hero(Profession profession) : profession(profession) {}
// 获取英雄职业
Profession getProfession() const { return profession; }
private:
Profession profession;
};
class Monster {
public:
Monster() {
// 初始化随机种子
srand(static_cast<unsigned int>(time(0)));
}
// 怪物死亡时掉落武器
Weapon* die(const Hero& hero) {
cout << "Monster died!" << endl;
// 根据英雄职业或随机规则决定掉落武器
Weapon* droppedWeapon = dropWeapon(hero);
cout << "Dropped weapon: " << droppedWeapon->getName() << end
return droppedWeapon;
}
private:
// 根据英雄职业或随机掉落武器
Weapon* dropWeapon(const Hero& hero) {
if (hero.getProfession() == Hero::Profession::Warrior) {
return new Axe(); // 战士掉落斧头
} else if (hero.getProfession() == Hero::Profession::Archer)
return new Sword(); // 弓箭手掉落长剑
} else if (hero.getProfession() == Hero::Profession::Mage) {
return new Blade(); // 法师掉落短剑
}
// 如果是其他职业,随机掉落武器
int randChoice = rand() % 3;
if (randChoice == 0) {
return new Sword();
} else if (randChoice == 1) {
return new Blade();
} else {
return new Axe();
}
}
};
int main() {
// 创建不同职业的英雄
Hero warrior(Hero::Profession::Warrior);
Hero archer(Hero::Profession::Archer);
Hero mage(Hero::Profession::Mage);
// 创建怪物
Monster monster;
// 模拟怪物死亡并掉落武器
Weapon* weapon1 = monster.die(warrior); // 战士掉落斧头
Weapon* weapon2 = monster.die(archer); // 弓箭手掉落长剑
Weapon* weapon3 = monster.die(mage); // 法师掉落短剑
// 删除掉落的武器,防止内存泄漏
delete weapon1;
delete weapon2;
delete weapon3;
return 0;
}
cpp
#include <iostream>
using namespace std;
template <typename T>
class List {
public:
struct Node {
T val;
Node* next;
Node* prev;
};
// 构造函数
List() : head(nullptr), tail(nullptr) {}
// 析构函数
~List() {
clear();
}
// 向链表末尾添加元素
void push_back(const T& value) {
Node* newNode = new Node{value, nullptr, tail}; // 创建新节点
if (tail) {
tail->next = newNode; // 如果链表非空,将新的节点连接到尾节
}
tail = newNode; // 更新尾节点
if (!head) {
head = newNode; // 如果链表为空,更新头节点
}
}
// 访问链表中指定位置的元素
T& operator[](size_t index) {
Node* current = head;
size_t count = 0;
while (current && count < index) {
current = current->next;
count++;
}
if (current) {
return current->val;
}
throw out_of_range("Index out of range");
}
// 打印链表内容
friend ostream& operator<<(ostream& os, const List<T>& list) {
Node* current = list.head;
while (current) {
os << current->val << " ";
current = current->next;
}
return os;
}
// 清除链表
void clear() {
Node* current = head;
while (current) {
Node* nextNode = current->next;
delete current; // 删除当前节点
current = nextNode; // 移动到下一个节点
}
head = tail = nullptr; // 头尾指针置空
}
private:
Node* head; // 链表头
Node* tail; // 链表尾
};
int main() {
List<int> myList;
myList.push_back(10);
myList.push_back(20);
myList.push_back(30);
cout << "链表内容: " << myList << endl;
cout << "访问索引1的元素: " << myList[1] << endl;
return 0;
}