C++中的多态和模板

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;
}
                                                                       
                                                                       
                                                                       
相关推荐
DBWYX36 分钟前
c++项目 网络聊天服务器 实现;QPS测试
c++
小萌新上大分1 小时前
SpringCloudGateWay
java·开发语言·后端·springcloud·springgateway·cloudalibaba·gateway网关
XYY3692 小时前
前缀和 一维差分和二维差分 差分&差分矩阵
数据结构·c++·算法·前缀和·差分
PacosonSWJTU2 小时前
python基础-13-处理excel电子表格
开发语言·python·excel
froginwe112 小时前
Perl 条件语句
开发语言
longlong int2 小时前
【每日算法】Day 16-1:跳表(Skip List)——Redis有序集合的核心实现原理(C++手写实现)
数据库·c++·redis·算法·缓存
24白菜头2 小时前
C和C++(list)的链表初步
c语言·数据结构·c++·笔记·算法·链表
啥都鼓捣的小yao2 小时前
利用C++编写操作OpenCV常用操作
开发语言·c++·opencv
灼华十一2 小时前
Golang系列 - 内存对齐
开发语言·后端·golang
程序媛学姐3 小时前
SpringRabbitMQ消息模型:交换机类型与绑定关系
java·开发语言·spring