#include <iostream>
#include <string>
class Animal {
public:
std::string eat() const { return "eat"; }
std::string sleep() const { return "sleep"; }
};
class Dog : public Animal {
public:
std::string bark() const { return "bark"; }
};
class Shape {
public:
void setWidth(int w) { width = w; }
void setHeight(int h) { height = h; }
int secretFactor() const { return secret_; }
protected:
int width = 0;
int height = 0;
private:
int secret_ = 2;
};
class PaintCost {
public:
int getCost(int area) const { return area * 70; }
};
class Rectangle : public Shape, public PaintCost {
public:
int getArea() const { return width * height; }
int scaledArea() const { return getArea() * secretFactor(); }
};
class ProtectedRect : protected Shape {
public:
void configure(int w, int h) { setWidth(w); setHeight(h); }
int area() const { return width * height; }
};
class PrivateRect : private Shape {
public:
void configure(int w, int h) { setWidth(w); setHeight(h); }
int area() const { return width * height; }
};
// Virtual inheritance resolving diamond ambiguity
class Device {
public:
int id = 10;
};
class Scanner : virtual public Device {
public:
void bumpScanner() { id += 5; }
};
class Printer : virtual public Device {
public:
void bumpPrinter() { id += 7; }
};
class Copier : public Scanner, public Printer {};
int main() {
// 1. Single public inheritance (Animal -> Dog & Shape -> Rectangle)
Dog d;
Rectangle r;
r.setWidth(5);
r.setHeight(7);
std::string act1 = d.eat() + "+" + d.sleep() + "+" + d.bark() + "|area=" + std::to_string(r.getArea());
const std::string exp1 = "eat+sleep+bark|area=35";
bool ok1 = (act1 == exp1);
// 2. Access control & public/protected/private inheritance
ProtectedRect pr;
pr.configure(4, 6);
PrivateRect pvr;
pvr.configure(3, 8);
std::string act2 = "pub_priv_via_base=" + std::to_string(r.scaledArea()) +
",prot=" + std::to_string(pr.area()) +
",priv=" + std::to_string(pvr.area());
const std::string exp2 = "pub_priv_via_base=70,prot=24,priv=24";
bool ok2 = (act2 == exp2);
// 3. Multiple inheritance (Shape + PaintCost)
int area = r.getArea();
int cost = r.getCost(area);
std::string act3 = "area=" + std::to_string(area) + ",cost=" + std::to_string(cost);
const std::string exp3 = "area=35,cost=2450";
bool ok3 = (act3 == exp3);
// 4. Virtual inheritance prevents duplicate base subobjects in diamond hierarchy
Copier c;
c.bumpScanner();
c.bumpPrinter();
bool single_base = (&static_cast<Scanner&>(c).id == &static_cast<Printer&>(c).id);
std::string act4 = "id=" + std::to_string(c.id) + ",shared=" + (single_base ? "true" : "false");
const std::string exp4 = "id=22,shared=true";
bool ok4 = (act4 == exp4);
std::cout << "{\n"
<< " \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
<< " \"cases\": [\n"
<< " {\"name\": \"single_public_inheritance\", \"expected\": \"" << exp1
<< "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
<< " {\"name\": \"inheritance_access_modes\", \"expected\": \"" << exp2
<< "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
<< " {\"name\": \"multiple_inheritance_cost\", \"expected\": \"" << exp3
<< "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
<< " {\"name\": \"virtual_inheritance_diamond\", \"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-inheritance-lab/explore-cpp-inheritance.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-inheritance.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2326.zip