蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

验证函数重载决策及二元/流/一元/前后置自增/赋值/下标/调用/箭头运算符重载

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-overloading.cpp
explore-cpp-overloading.cpp127 行
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

class PrintData {
public:
    std::string format(int i) const { return "int:" + std::to_string(i); }
    std::string format(double f) const {
        std::ostringstream oss;
        oss << "double:" << std::fixed << std::setprecision(3) << f;
        return oss.str();
    }
    std::string format(const char* c) const { return std::string("str:") + c; }
};

class Box {
public:
    Box(double l = 0.0, double b = 0.0, double h = 0.0) : length_(l), breadth_(b), height_(h) {}
    double getVolume() const { return length_ * breadth_ * height_; }

    Box operator+(const Box& rhs) const {
        return Box(this->length_ + rhs.length_, this->breadth_ + rhs.breadth_, this->height_ + rhs.height_);
    }

    friend bool operator==(const Box& a, const Box& b) {
        return a.length_ == b.length_ && a.breadth_ == b.breadth_ && a.height_ == b.height_;
    }

    friend std::ostream& operator<<(std::ostream& os, const Box& b) {
        return os << static_cast<int>(b.getVolume());
    }
private:
    double length_, breadth_, height_;
};

class Counter {
public:
    explicit Counter(int v = 0) : val_(v) {}
    Counter operator-() const { return Counter(-val_); }
    Counter& operator++() { ++val_; return *this; }
    Counter operator++(int) { Counter old(*this); ++val_; return old; }
    Counter& operator=(const Counter& rhs) {
        if (this != &rhs) {
            val_ = rhs.val_;
        }
        return *this;
    }
    int get() const { return val_; }
private:
    int val_;
};

struct Payload {
    int score = 95;
};

class SmartArray {
public:
    int data[3] = {10, 20, 30};
    Payload payload;

    int& operator[](std::size_t idx) { return data[idx]; }
    int operator()(int mul) const { return (data[0] + data[1] + data[2]) * mul; }
    Payload* operator->() { return &payload; }
};

int main() {
    // 1. Function overload resolution by parameter type
    PrintData pd;
    char text[] = "Hello C++";
    std::string act1 = pd.format(5) + "|" + pd.format(500.263) + "|" + pd.format(text);
    const std::string exp1 = "int:5|double:500.263|str:Hello C++";
    bool ok1 = (act1 == exp1);

    // 2. Member binary operator+ and non-member friend operator== / operator<<
    Box b1(6.0, 7.0, 5.0);
    Box b2(12.0, 13.0, 10.0);
    Box b3 = b1 + b2;
    Box expected_b3(18.0, 20.0, 15.0);
    std::ostringstream oss;
    oss << b1 << "," << b2 << "," << b3 << ",eq=" << ((b3 == expected_b3) ? "1" : "0");
    std::string act2 = oss.str();
    const std::string exp2 = "210,1560,5400,eq=1";
    bool ok2 = (act2 == exp2);

    // 3. Unary -, prefix ++, postfix ++, and copy assignment operator=
    Counter c(5);
    Counter neg = -c;
    Counter pre = ++c;   // c becomes 6, pre is 6
    Counter post = c++;  // post is 6, c becomes 7
    Counter assigned;
    assigned = c;
    std::string act3 = "neg=" + std::to_string(neg.get()) +
                       ",pre=" + std::to_string(pre.get()) +
                       ",post=" + std::to_string(post.get()) +
                       ",after=" + std::to_string(assigned.get());
    const std::string exp3 = "neg=-5,pre=6,post=6,after=7";
    bool ok3 = (act3 == exp3);

    // 4. operator[], operator(), and operator->
    SmartArray sa;
    sa[1] = 25;
    int scaled_sum = sa(2);
    int score = sa->score;
    std::string act4 = "sub=" + std::to_string(sa[1]) +
                       ",call=" + std::to_string(scaled_sum) +
                       ",arrow=" + std::to_string(score);
    const std::string exp4 = "sub=25,call=130,arrow=95";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"function_overload_resolution\", \"expected\": \"" << exp1
              << "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"binary_and_stream_operators\", \"expected\": \"" << exp2
              << "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"unary_inc_and_assignment_operators\", \"expected\": \"" << exp3
              << "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"subscript_call_and_arrow_operators\", \"expected\": \"" << exp4
              << "\", \"actual\": \"" << act4 << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
              << "  ]\n"
              << "}\n";
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。

入口文件:projects/cplusplus-cpp-overloading-lab/explore-cpp-overloading.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-overloading.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2387.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习