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;
}
                                                                       
                                                                       
                                                                       
相关推荐
海码0075 分钟前
【Hot 100】 148. 排序链表
数据结构·c++·链表·排序算法·hot100
余弦的倒数20 分钟前
C++的vector中emplace_back() 与 push_back() 的区别
开发语言·c++
鱼糕权八郎 -21 分钟前
LeetCode392_判断子序列
c++·leetcode
到底怎么取名字不会重复30 分钟前
Day16(贪心算法)——LeetCode45.跳跃游戏II&763.划分字母区间
c++·算法·leetcode·游戏·贪心算法
感谢地心引力39 分钟前
【matlab】与开发板进行串口通信
开发语言·matlab·esp8266
编程乐趣42 分钟前
基于C#开发的适合Windows开源文件管理器
开发语言·windows·c#
染指11101 小时前
18.第二阶段x64游戏实战-MFC列表框
汇编·c++·windows·游戏·游戏逆向·x64dbg
enyp801 小时前
Qt文本文件读写方法详解
开发语言·c++·算法
我的golang之路果然有问题1 小时前
快速了解Go+微服务(概念和一个例子)
开发语言·笔记·后端·学习·微服务·golang
福理原乡大王2 小时前
进程地址空间
java·开发语言·算法