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

验证 Adder 抽象接口、双底层实现无缝替换、多访问标签及标准库抽象

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

class Adder {
public:
    explicit Adder(int i = 0) : total(i) {}
    void addNum(int number) { total += number; }
    int getTotal() const { return total; }
private:
    int total;
};

class HistoryAdder {
public:
    explicit HistoryAdder(int i = 0) {
        if (i != 0) items_.push_back(i);
    }
    void addNum(int number) { items_.push_back(number); }
    int getTotal() const {
        int sum = 0;
        for (int v : items_) sum += v;
        return sum;
    }
private:
    std::vector<int> items_;
};

class MultiLabelAccount {
public:
    explicit MultiLabelAccount(int init) : balance_(init) {}
private:
    int balance_;
public:
    void deposit(int delta) { if (delta > 0) balance_ += delta; }
private:
    int fee_ = 1;
public:
    int netBalance() const { return balance_ - fee_; }
};

template <typename AdderType>
static int runClientWorkflow(AdderType& adder) {
    adder.addNum(10);
    adder.addNum(20);
    adder.addNum(30);
    return adder.getTotal();
}

int main() {
    // 1. Adder ADT hiding private total behind public addNum/getTotal
    Adder a;
    int t1 = runClientWorkflow(a);
    bool ok1 = (t1 == 60);

    // 2. Interface stability across two different private implementations
    Adder implA(5);
    HistoryAdder implB(5);
    int rA = runClientWorkflow(implA);
    int rB = runClientWorkflow(implB);
    std::string act2 = std::to_string(rA) + "=" + std::to_string(rB);
    const std::string exp2 = "65=65";
    bool ok2 = (act2 == exp2);

    // 3. Multiple access labels in one class definition
    MultiLabelAccount acc(100);
    acc.deposit(50);
    acc.deposit(-20);
    int act3 = acc.netBalance();
    bool ok3 = (act3 == 149);

    // 4. Standard library abstraction: std::sort & ostream interface
    std::vector<int> nums = {42, 7, 19, 3, 25};
    std::sort(nums.begin(), nums.end());
    std::ostringstream oss;
    oss << "Hello C++:";
    for (std::size_t i = 0; i < nums.size(); ++i) {
        if (i) oss << ",";
        oss << nums[i];
    }
    std::string act4 = oss.str();
    const std::string exp4 = "Hello C++:3,7,19,25,42";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"adder_public_interface_total\", \"expected\": 60, \"actual\": "
              << t1 << ", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"swappable_internal_representation\", \"expected\": \"" << exp2
              << "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"multiple_access_labels_scope\", \"expected\": 149, \"actual\": "
              << act3 << ", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"stdlib_sort_and_stream_abstraction\", \"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-data-abstraction-lab/explore-cpp-data-abstraction.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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