C++学习笔记(24)

255. 如何取代虚函数

虚函数在执行时会跳转两次:一次查找对象的虚函数表(vtable),再通过表中的地址找到实际的函数地址。这会导致 CPU 每次跳转时预取指令失效,降低效率。因此,使用 std::function 代替虚函数,通过绑定子类的成员函数,实现类似虚函数的功能。代码如下:

复制代码
#include <iostream>
#include <functional>
using namespace std;

struct Hero {  // 英雄基类
    function<void()> m_callback;  // 用于绑定子类的成员函数

    // 注册子类成员函数,子类成员函数没有参数
    template<typename Fn, typename ...Args>
    void callback(Fn&& fn, Args&&... args) {
        m_callback = bind(forward<Fn>(fn), forward<Args>(args)...);
    }

    void show() { m_callback(); }  // 调用子类的成员函数
};

struct XS : public Hero {  // 西施派生类
    void show() { cout << "西施释放了技能。\n"; }
};

struct HX : public Hero {  // 韩信派生类
    void show() { cout << "韩信释放了技能。\n"; }
};

int main() {
    int id = 0;  // 英雄的 id
    cout << "请输入英雄(1-西施;2-韩信):";
    cin >> id;

    Hero* ptr = nullptr;
    if (id == 1) {
        ptr = new XS;
        ptr->callback(&XS::show, static_cast<XS*>(ptr));  // 注册子类成员函数
    }
    else if (id == 2) {
        ptr = new HX;
        ptr->callback(&HX::show, static_cast<HX*>(ptr));  // 注册子类成员函数
    }

    if (ptr != nullptr) {
        ptr->show();  // 调用子类的成员函数
        delete ptr;  // 释放派生类对象
    }
}

299. 课后作业

一、选出妃子、宫女和嬷嬷
  1. 妃子:年龄 18-25 岁,身高 165-178cm,身材火辣,颜值漂亮。
  2. 宫女:年龄 18-30 岁,身高 160-165cm,身材火辣或普通,颜值漂亮。
  3. 嬷嬷:年龄 35-40 岁,身高 155-165cm,身材普通或飞机场,颜值一般。
复制代码
#include <iostream>
#include <cstring>
using namespace std;

struct st_girl {  // 超女结构体
    int age;
    int height;
    char sc[31];  // 身材
    bool yz;      // true-漂亮,false-一般
} stgirl;

int main() {
    // 先输入超女全部的数据项
    cout << "请输入年龄:"; cin >> stgirl.age;
    cout << "请输入身高:"; cin >> stgirl.height;
    cout << "请输入身材:"; cin >> stgirl.sc;
    cout << "请输入颜值(1-漂亮,0-一般):"; cin >> stgirl.yz;

    cout << "age=" << stgirl.age << ", height=" << stgirl.height << ", sc=" << stgirl.sc << ", yz=" << stgirl.yz << endl;

    // 判断妃子
    if (stgirl.age >= 18 && stgirl.age <= 25 && stgirl.height >= 165 && stgirl.height <= 178 && strcmp(stgirl.sc, "火辣") == 0 && stgirl.yz) {
        cout << "妃子\n";
    }
    // 判断宫女
    else if (stgirl.age >= 18 && stgirl.age <= 30 && stgirl.height >= 160 && stgirl.height <= 165 && (strcmp(stgirl.sc, "火辣") == 0 || strcmp(stgirl.sc, "普通") == 0) && stgirl.yz) {
        cout << "宫女\n";
    }
    // 判断嬷嬷
    else if (stgirl.age >= 35 && stgirl.age <= 40 && stgirl.height >= 155 && stgirl.height <= 165 && (strcmp(stgirl.sc, "普通") == 0 || strcmp(stgirl.sc, "飞机场") == 0) && !stgirl.yz) {
        cout << "嬷嬷\n";
    }
}
二、根据数字判断月份
  1. 使用 if-else if 语句实现。
  2. 使用 switch 语句实现。
  3. 使用字符串数组实现。
复制代码
#include <iostream>
using namespace std;

int main() {
    int month;
    cout << "请输入月份:"; 
    cin >> month;
    cout << "month = " << month << endl;

    // 使用字符串数组实现
    string montharr[12] = {
        "January", "February", "March", "April", "May", "June", 
        "July", "August", "September", "October", "November", "December"
    };

    if (month >= 1 && month <= 12)
        cout << montharr[month - 1] << endl;
    else
        cout << "输入的数字不正确。\n";
}
相关推荐
绯樱殇雪5 分钟前
编程题 03-树2 List Leaves【PAT】
c++·pat考试
✿ ༺ ོIT技术༻36 分钟前
笔试强训:Day5
c++·算法
oneDay++1 小时前
# IntelliJ IDEA企业版集成AI插件「通义灵码」全流程详解:从安装到实战
java·经验分享·学习·intellij-idea·学习方法
代码小将1 小时前
Leetcode76覆盖最小子串
笔记·学习·算法
z35026037061 小时前
dockers笔记
笔记
努力的小帅1 小时前
C++_STL_map与set
开发语言·数据结构·c++·学习·leetcode·刷题
田梓燊1 小时前
数学复习笔记 15
笔记·线性代数·机器学习
邝邝邝邝丹1 小时前
React学习———React Router
前端·学习·react.js
yuhouxiyang1 小时前
学习海康VisionMaster之直方图工具
学习·计算机视觉
双叶8361 小时前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)
c语言·开发语言·数据结构·c++·windows