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

验证 ABC 抽象类型特征、Rectangle/Triangle 具体实现、多态扩展与部分实现抽象性

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

class Shape {
public:
    virtual int getArea() const = 0;
    virtual ~Shape() = default;

    void setWidth(int w) { width = w; }
    void setHeight(int h) { height = h; }
protected:
    int width = 0;
    int height = 0;
};

class Rectangle : public Shape {
public:
    int getArea() const override { return width * height; }
};

class Triangle : public Shape {
public:
    int getArea() const override { return (width * height) / 2; }
};

class Square : public Shape {
public:
    explicit Square(int side) { setWidth(side); setHeight(side); }
    int getArea() const override { return width * height; }
};

class PluginInterface {
public:
    virtual std::string name() const = 0;
    virtual int run(int x) const = 0;
    virtual ~PluginInterface() = default;
};

class PartialPlugin : public PluginInterface {
public:
    std::string name() const override { return "partial"; }
    // run() intentionally left pure virtual -> still abstract
};

class CompletePlugin : public PartialPlugin {
public:
    int run(int x) const override { return x * 3; }
};

int main() {
    // 1. Abstract class trait check (ABC cannot be instantiated; concrete subclasses can)
    static_assert(std::is_abstract_v<Shape>, "Shape must be abstract");
    static_assert(!std::is_abstract_v<Rectangle>, "Rectangle must be concrete");
    static_assert(!std::is_abstract_v<Triangle>, "Triangle must be concrete");
    bool ok1 = std::is_abstract_v<Shape> && !std::is_abstract_v<Rectangle> && !std::is_abstract_v<Triangle>;

    // 2. Rectangle and Triangle implementing Shape::getArea()
    Rectangle rect;
    rect.setWidth(5);
    rect.setHeight(7);
    Triangle tri;
    tri.setWidth(5);
    tri.setHeight(7);
    std::string act2 = "rect=" + std::to_string(rect.getArea()) + ",tri=" + std::to_string(tri.getArea());
    const std::string exp2 = "rect=35,tri=17";
    bool ok2 = (act2 == exp2);

    // 3. Extensible polymorphic interface container
    std::vector<std::unique_ptr<Shape>> shapes;
    auto r = std::make_unique<Rectangle>();
    r->setWidth(5); r->setHeight(7);
    auto t = std::make_unique<Triangle>();
    t->setWidth(5); t->setHeight(7);
    shapes.push_back(std::move(r));
    shapes.push_back(std::move(t));
    shapes.push_back(std::make_unique<Square>(4));
    int total_area = 0;
    for (const auto& sp : shapes) {
        total_area += sp->getArea();
    }
    bool ok3 = (total_area == 68);

    // 4. Partially implemented subclass stays abstract; full implementation is concrete
    static_assert(std::is_abstract_v<PartialPlugin>, "PartialPlugin must remain abstract");
    static_assert(!std::is_abstract_v<CompletePlugin>, "CompletePlugin must be concrete");
    CompletePlugin cp;
    PluginInterface& iface = cp;
    std::string act4 = iface.name() + ":" + std::to_string(iface.run(7)) +
                       ":partial_abs=" + (std::is_abstract_v<PartialPlugin> ? "1" : "0");
    const std::string exp4 = "partial:21:partial_abs=1";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"abc_type_traits_purity\", \"expected\": true, \"actual\": "
              << (ok1 ? "true" : "false") << ", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"concrete_shape_implementations\", \"expected\": \"" << exp2
              << "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"polymorphic_interface_extensibility\", \"expected\": 68, \"actual\": "
              << total_area << ", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"partial_vs_complete_override\", \"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-interfaces-lab/explore-cpp-interfaces.cpp

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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