有一个圆形游泳池,现在需要在其周围建一过道,并在其四周围上矩形栅栏,如图所示。若过道造价为20元/平方米。要求计算并输出过道的造价。请定义一个Circle类,内有私有数据成员radius表示半径,并有若干成员函数;定义一个Rectangle类,内有私有数据成员length、width表示长和宽,并有若干成员函数。(设圆周率PI = 3.14159,所有数据均为double类型)
#include<iostream>
using namespace std;
const double PI=3.14159;
class Circle {
private: double radius;
public:
Circle(double r){
radius = r;
}
double Circle_cost(){
return PI*radius*radius*20;
}
};
class Rectangle{
private: double width,length;
public:
Rectangle(double w,double l){
width = w;
length = l;
}
double Rectangle_cost(){
return width*length*20;
}
};
int main(){
double a,b,c;
cin>>a>>b>>c;
Circle s1(a);
Rectangle s2(b,c);
double s;
s=s2.Rectangle_cost()-s1.Circle_cost();
cout<<s<<endl;
}