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

Need help getting the area of a rectangle and a circle using a classes

本文关键字:获取 帮助      更新时间:2023-10-16

它们需要是类,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 */
};