当前位置: 首页 > news >正文

C++中的多态和模板

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



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

相关文章:

  • 3. go-zero中如何使用redis
  • C#“与AI的奇妙结合”
  • Immutable.js 完全指南:不可变数据的艺术与实践
  • 接口自动化学习五:mock工具使用
  • MyBatis查询语句专题、动态SQL、MyBatis的高级映射及延迟加载
  • Java线程生命周期探秘:六种状态的转换与核心机制
  • 【C语言】内存函数 (续)
  • 模拟算法(一):一维数组模拟
  • HTML零基础入门笔记:狂神版
  • 最短路基础模板题
  • ctfshow VIP题目限免(前10题)
  • Kotlin语言进阶:协程、Flow、Channel详解(二)
  • 在K8S中,内置的污点主要有哪些?
  • 透视表(pivot table)中的某类型数据进行降序排列
  • HTML5
  • 《C++探幽:STL(string类源码的简易实现(上))》
  • 免费送源码:Java+ssm+MySQL 校园二手书销售平台设计与实现 计算机毕业设计原创定制
  • lower_bound 和 upper_bound 是两个强大的二分查找函数
  • Vue基础知识21-30
  • 55.跳跃游戏
  • 网站建设入什么科目/广东省人大常委会
  • 物业公司网站建设/seo关键词优化技术
  • 广州外贸营销型网站/竞价推广工具
  • 济南市住房和城乡建设局网站/最新百度快速排名技术
  • 天津网站建设wangzhii/链爱交易平台
  • 湖南营销型网站建设 A磐石网络/seo技术 快速网站排名