#include <iostream>
using namespace std;
// 抽象基类 Object
class Object {
public:
virtual float volume() const = 0; // 纯虚函数
};
// 派生类 Cuboid
class Cuboid : public Object {
private:
float length;
float width;
float height;
public:
Cuboid(float l, float w, float h) : length(l), width(w), height(h) {}
float volume() const {
return length * width * height;
}
};
// 派生类 Cylinder
class Cylinder : public Object {
private:
float radius;
float height;
public:
Cylinder(float r, float h) : radius(r), height(h) {}
float volume() const {
return 3.14159 * radius * radius * height;
}
};
int main() {
Cuboid cu(3, 4, 5);
Cylinder cy(4, 10);
Object* ptr[2] = {&cu, &cy};
cout << "总体积: " << ptr[0]->volume() + ptr[1]->volume() << endl;
return 0;
}