// ===== CodeLab: C++ 继承与多态 =====
// 来源: https://aoerliang.dpdns.org/articles/cpp-oop
// 以下代码片段按文章出现顺序拼接, 共 5 段

// ----- 片段 1 (cpp) -----
class Animal {
public:
    void eat() { /* 所有动物都会吃 */ }
    virtual void speak() { cout << "..." << endl; }
};

class Dog : public Animal {
public:
    void speak() override { cout << "汪汪" << endl; }
};

// ----- 片段 2 (cpp) -----
class Dog : public Animal {
public:
    Dog(string n) : Animal(n) {}   // 初始化基类部分
};

// ----- 片段 3 (cpp) -----
void make_speak(Animal& a) {
    a.speak();   // 传入 Dog 就汪汪,传入 Cat 就喵喵
}

// ----- 片段 4 (cpp) -----
class Shape {
public:
    virtual double area() const = 0;   // 纯虚函数
    virtual ~Shape() = default;        // 别忘了虚析构!
};

class Circle : public Shape {
    double r;
public:
    Circle(double r) : r(r) {}
    double area() const override { return 3.14159 * r * r; }
};

// ----- 片段 5 (cpp) -----
Animal* p = new Dog("旺财");
delete p;   // 非虚析构:只析构 Animal 部分 → 泄漏
