2301_78163545 2023-06-08 13:10 采纳率: 73.1%
浏览 79
已结题

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

img

img


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

  • 写回答

1条回答 默认 最新

  • Loup&卡普 2023-06-08 14:11
    关注
    #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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月16日
  • 已采纳回答 6月8日
  • 创建了问题 6月8日

悬赏问题

  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么