c++ - 需要帮助使用类获取矩形和圆形的面积

标签 c++

它们需要是类并且获取区域需要是形状的一部分,方法 getArea 需要在形状中,区域需要被保护,并且在它们各自子类的形状、宽度、高度和半径部分(C++)

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <math.h>
using namespace std;

class shape {
    protected:
        double area;
    public:
        double getArea(){return area;};
};

class rectangle:shape {
    private:
        double width;
        double height;

    public:
        rectangle(){
            width=3;
            height=4;
        }
        double getHeight(){return height;};
        double getWidth(){return width;};

        void setHeight(double h){height=h;};
        void setWidth(double w){width=w;};
        void setArea(double width, double height){area=height*width;};
};

class circle:shape {
    private:
        double radius;

    public:
        circle(){
            radius=1;
        }

        double getRadius(){return radius;};
        void setRadius(double r){radius=r;};
        void setArea(double width, double height){area=M_PI*(pow(radius,2));};
};

int main () {
    rectangle miRectangulo;
    circle miCirculo;
    cout<<"Area of the rectangle is "<<miRectangulo.getArea()<<endl;
    cout<<"Area of the circle is "<<miCirculo.getArea();

    return 0;
}

他们确实需要遵循这些特定条件,现在我收到“double::shape getArea() is inaccesible”错误

最佳答案

默认情况下,继承是私有(private)的,这意味着 getArea 将不可访问。改为作为公共(public)基础继承。

class circle: public shape
{
/* snip */
};

关于c++ - 需要帮助使用类获取矩形和圆形的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59168281/

相关文章:

c++ - 递归模板参数删除

c++ - cl.exe 编译器显然接受了不正确的 C++ 代码

c++ - 错误 : conflicting declaration for uint32_t

c++ - MS Access 中 NZ 函数的 ADO 等价物?

c# - 将指向类的指针从 C# 传递到非托管 C++ 代码

c++ - 如何通知系统创建新设备?

c++ - 从控制台模拟按钮单击到 UI 类

c++ - 为什么在为Matrix类实现初始化列表构造函数时不能访问此数组?

c++ - 指向派生对象数组的基指针

c++ - 如何从 LLVM 中的 CallInst 获取间接调用的函数名称