#include <iostream>
#include <string>
#include <utility>
class Box {
public:
double length = 0.0;
double breadth = 0.0;
double height = 0.0;
double get() const;
void set(double len, double bre, double hei);
Box& scale(double factor) {
this->length *= factor;
this->breadth *= factor;
this->height *= factor;
return *this;
}
};
double Box::get() const {
return length * breadth * height;
}
void Box::set(double len, double bre, double hei) {
length = len;
breadth = bre;
height = hei;
}
class LifecycleWidget {
public:
static int ctor_cnt;
static int copy_cnt;
static int move_cnt;
static int dtor_cnt;
static int alive;
explicit LifecycleWidget(int v) : value_(v) { ++ctor_cnt; ++alive; }
LifecycleWidget(const LifecycleWidget& o) : value_(o.value_) { ++copy_cnt; ++alive; }
LifecycleWidget(LifecycleWidget&& o) noexcept : value_(o.value_) { o.value_ = 0; ++move_cnt; ++alive; }
~LifecycleWidget() { ++dtor_cnt; --alive; }
inline int value() const { return value_; }
static int liveCount() { return alive; }
friend int inspectSecret(const LifecycleWidget& w);
private:
int value_;
};
int LifecycleWidget::ctor_cnt = 0;
int LifecycleWidget::copy_cnt = 0;
int LifecycleWidget::move_cnt = 0;
int LifecycleWidget::dtor_cnt = 0;
int LifecycleWidget::alive = 0;
int inspectSecret(const LifecycleWidget& w) {
return w.value_ * 2;
}
int main() {
// 1. Class blueprint, direct member access (.) & out-of-class member functions
Box b1, b2, b3;
b1.height = 5.0; b1.length = 6.0; b1.breadth = 7.0;
b2.height = 10.0; b2.length = 12.0; b2.breadth = 13.0;
b3.set(16.0, 8.0, 12.0);
std::string act1 = std::to_string(static_cast<int>(b1.get())) + "," +
std::to_string(static_cast<int>(b2.get())) + "," +
std::to_string(static_cast<int>(b3.get()));
const std::string exp1 = "210,1560,1536";
bool ok1 = (act1 == exp1);
// 2. Constructor, copy constructor, move constructor, and destructor symmetry
{
LifecycleWidget w1(10);
LifecycleWidget w2(w1);
LifecycleWidget w3(std::move(w2));
(void)w3.value();
}
std::string act2 = "c=" + std::to_string(LifecycleWidget::ctor_cnt) +
",cp=" + std::to_string(LifecycleWidget::copy_cnt) +
",mv=" + std::to_string(LifecycleWidget::move_cnt) +
",d=" + std::to_string(LifecycleWidget::dtor_cnt) +
",alive=" + std::to_string(LifecycleWidget::liveCount());
const std::string exp2 = "c=1,cp=1,mv=1,d=3,alive=0";
bool ok2 = (act2 == exp2);
// 3. this pointer chaining & pointer-to-object (->)
Box b4;
b4.set(2.0, 3.0, 4.0);
Box* ptr = &b4;
Box* chained = &ptr->scale(2.0);
std::string act3 = std::to_string(static_cast<int>(ptr->get())) + ":" +
(chained == ptr ? "same_this" : "diff");
const std::string exp3 = "192:same_this";
bool ok3 = (act3 == exp3);
// 4. friend function, inline getter, and static member tracking
int peak_alive = 0;
int friend_val = 0;
{
LifecycleWidget a(21);
LifecycleWidget b(9);
peak_alive = LifecycleWidget::liveCount();
friend_val = inspectSecret(a) + b.value();
}
std::string act4 = "peak=" + std::to_string(peak_alive) +
",friend=" + std::to_string(friend_val) +
",end=" + std::to_string(LifecycleWidget::liveCount());
const std::string exp4 = "peak=2,friend=51,end=0";
bool ok4 = (act4 == exp4);
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"box_class_blueprint_and_methods\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"ctor_copy_move_dtor_lifecycle\", \"expected\": \"" << exp2
<< "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"this_pointer_and_object_pointer\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"friend_inline_and_static_members\", \"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-classes-objects-lab/explore-cpp-classes-objects.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-classes-objects.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2311.zip