定义point作为基类,在此基类上派生出圆circle类,由circle类派生出圆柱cylinder类

img

img


定义point作为基类,在此基类上派生出圆circle类,由circle类派生出圆柱cylinder类,我的这个代码运行失败,能帮我编写一个正确的代码吗

#include <iostream>
#define PI 3.14159
using namespace std;

class point {
protected:
    double x, y;
public:
    point(double x = 0, double y = 0) : x(x), y(y) {}
    point(): x(0),y(0) {};
};

class circle : public point {
protected:
    double r;
public:
    circle(double r = 0, double x = 0, double y = 0) : point(x, y), r(r) {}
    double area() { return PI * r * r; }
};

class cylinder : public circle {
protected:
    double h;
public:
    cylinder(double r = 0, double h = 0, double x = 0, double y = 0) : circle(r,x,y), h(h) {}
    double volume() { return area() * h; }
    double surface_area() { return 2 * area() + 2 * PI * r * h; }
};

int main() {
    circle c(5);
    cylinder cy(5, 10);

    cout << "Circle area: " << c.area() << endl;
    cout << "Cylinder surface area: " << cy.surface_area() << endl;
    cout << "Cylinder volume: " << cy.volume() << endl;

    return 0;
}

写了个 x,y 最后发现没用到。。。阿西吧。。。hhhh