#include <algorithm>
#include <cstdio>
#include <iostream>
#include <memory>
#include <numeric>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
std::string jint(long long v) { return std::to_string(v); }
std::string juint(unsigned long long v) { return std::to_string(v); }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
class Shape {
public:
virtual ~Shape() = default;
virtual int area() const = 0;
};
class Rectangle : public Shape {
int width_;
int height_;
public:
Rectangle(int w, int h) : width_(w), height_(h) {}
int area() const override { return width_ * height_; }
};
class Square : public Shape {
int side_;
public:
explicit Square(int s) : side_(s) {}
int area() const override { return side_ * side_; }
};
template <typename T>
T generic_sum(const std::vector<T>& xs) {
return std::accumulate(xs.begin(), xs.end(), T{});
}
int main() {
std::vector<lab::Case> cases;
// Case 1: static typing & C++17 standard version
constexpr bool static_checks = std::is_same_v<decltype(42), int> && !std::is_same_v<int, double> && (__cplusplus == 201703L);
cases.push_back({
"cpp17_standard_and_static_typing",
lab::jbool(true),
lab::jbool(static_checks),
static_checks
});
// Case 2: OOP encapsulation, abstraction, inheritance, polymorphism
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Rectangle>(4, 5));
shapes.push_back(std::make_unique<Square>(6));
int total_area = 0;
for (const auto& s : shapes) total_area += s->area();
cases.push_back({
"oop_four_pillars_polymorphism",
lab::jint(56),
lab::jint(total_area),
total_area == 56
});
// Case 3: STL vector + sort + accumulate
std::vector<int> nums = {5, 2, 8, 1, 4};
std::sort(nums.begin(), nums.end());
int s = generic_sum(nums);
std::string summary = std::to_string(nums.front()) + ".." + std::to_string(nums.back()) + "=" + std::to_string(s);
cases.push_back({
"stl_containers_and_algorithms",
lab::esc("1..8=20"),
lab::esc(summary),
summary == "1..8=20"
});
// Case 4: multi-paradigm (procedural + generic template)
std::vector<double> weights = {1.5, 2.5, 3.0};
double wsum = generic_sum(weights);
bool ok = (wsum == 7.0);
cases.push_back({
"multi_paradigm_efficiency",
lab::jbool(true),
lab::jbool(ok),
ok
});
lab::emit(cases);
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-intro-lab/explore-cpp-intro.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-intro.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2328.zip