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";
}
相关推荐
点心的游戏开发世界8 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
摇滚侠8 小时前
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 使用 JdbcTemplate 阅读笔记 32
spring boot·笔记·后端
RuoZoe9 小时前
从 2026 年 3 月 1 日开源,到 26.10.9:Jalium UI 半年时间到底走了多远?
c语言·c++
sunshine22 girl9 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
鱼子星_10 小时前
【C++】继承和多态(上)
c++·笔记
Kobebryant-Manba10 小时前
学习Bert微调
人工智能·学习·bert
xian_wwq11 小时前
【学习笔记】深度认知系列-第14讲 端侧AI崛起——为什么AI正在从云端走向本地
人工智能·笔记·学习
我爱cope11 小时前
【计算机网络 | 传输层3:TCP 协议概述:面向连接、可靠传输到底意味着什么?】
网络·网络协议·学习·tcp/ip·计算机网络·传输层
郝学胜-神的一滴12 小时前
Qt 高级编程 045:坐标体系深度实战
开发语言·c++·windows·python·qt·程序人生
码匠许师傅12 小时前
【设计模式精讲】22.中介者模式(Mediator)
c++·设计模式·软件工程·uml·中介者模式