#include <iostream>
#include <string>
class Box {
public:
Box(double l, double b, double h) : length_(l), breadth_(b), height_(h) {}
double getVolume() const { return length_ * breadth_ * height_; }
private:
double length_;
double breadth_;
double height_;
};
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 Student {
private:
std::string name_;
int age_;
public:
Student(std::string studentName, int studentAge)
: name_(std::move(studentName)), age_(studentAge > 0 ? studentAge : 1) {}
std::string getName() const { return name_; }
int getAge() const { return age_; }
void setName(std::string studentName) { name_ = std::move(studentName); }
bool setAge(int studentAge) {
if (studentAge > 0) {
age_ = studentAge;
return true;
}
return false;
}
};
class BaseVault {
private:
int pin_ = 4242;
protected:
int tier_ = 2;
public:
int verifyPin(int attempt) const { return attempt == pin_ ? 1 : 0; }
friend class VaultAuditor;
};
class DerivedVault : public BaseVault {
public:
int upgradeTier() { return ++tier_; }
};
class VaultAuditor {
public:
static int auditPinLength(const BaseVault& v) { return v.pin_ > 999 ? 4 : 0; }
};
int main() {
// 1. Private Box & Adder encapsulation
Box b(5.0, 6.0, 7.0);
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
std::string act1 = "vol=" + std::to_string(static_cast<int>(b.getVolume())) +
",total=" + std::to_string(a.getTotal());
const std::string exp1 = "vol=210,total=60";
bool ok1 = (act1 == exp1);
// 2. Student getter/setter validation rejecting invalid age
Student st("Alice", 20);
bool rejected = !st.setAge(-5);
bool accepted = st.setAge(22);
st.setName("Bob");
std::string act2 = st.getName() + ":" + std::to_string(st.getAge()) +
":rej=" + (rejected ? "1" : "0") + ":acc=" + (accepted ? "1" : "0");
const std::string exp2 = "Bob:22:rej=1:acc=1";
bool ok2 = (act2 == exp2);
// 3. Protected member accessible to derived class while private stays encapsulated
DerivedVault dv;
int new_tier = dv.upgradeTier();
int pin_ok = dv.verifyPin(4242);
std::string act3 = "tier=" + std::to_string(new_tier) + ",pin_ok=" + std::to_string(pin_ok);
const std::string exp3 = "tier=3,pin_ok=1";
bool ok3 = (act3 == exp3);
// 4. Friend class controlled inspection tradeoff
int digits = VaultAuditor::auditPinLength(dv);
bool ok4 = (digits == 4);
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"box_and_adder_private_state\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"student_setter_invariant_guard\", \"expected\": \"" << exp2
<< "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"protected_vs_private_boundary\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"friend_class_access_boundary\", \"expected\": 4, \"actual\": "
<< digits << ", \"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-encapsulation-lab/explore-cpp-data-encapsulation.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-data-encapsulation.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2315.zip