需要帮助使用类获取矩形和圆形的面积
Need help getting the area of a rectangle and a circle using a classes
它们需要是类,getArea 需要成为形状的一部分,方法 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(( 无法访问"
默认情况下,继承是私有的,这意味着 getArea 将无法访问。改为作为公共基础继承。
class circle: public shape
{
/* snip */
};
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 需要帮助使用类获取矩形和圆形的面积
- C++获取std::variant当前帮助的类型的std::typeindex
- 需要帮助从数组中获取特定值并将它们从最高到最低排序
- 看到' - '字符时我无法结束 getline,有人可以帮助我在 c++ 中获取 getline() ifstream
- 写入文件时需要帮助获取新行
- 需要帮助从文件中获取字符和整数数据
- 等待从获取锁的函数调用的帮助程序函数中的条件变量
- C++ 指针帮助缺少参数列表使用 &帐户::获取余额
- 如何在CTreeCtrl中获取所选项目的文件类型?文件扩展名可能被隐藏,因此 GetItemText 没有帮助
- 为不明确的符号获取错误,需要帮助将其删除
- 在获取CPU频率方面需要一些帮助
- 无法使用Qt助手从Qt应用程序内部获取自定义帮助文件
- 需要帮助获取数组中最小的三个数字
- HW帮助:获取字符而不是获取行c++
- 获取卷帮助
- GDAL GeoTiff获取标高在指定像素帮助c++
- 需要帮助获取ifstream::open以打开文件并从中获取行