实验三: 类与对象定义初始化
内容提要:
1.一圆型游泳池如图1所示,现在需在其周围建一圆型过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米。过道宽度为3米,游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
图1
代码如下:
#include<iostream>
#include<string>
#define P 3.1415926
using namespace std;
class swimming_pool
{
public:
void get_r();
void gangway();//计算过道的价格并打印
void paling();//计算栅栏的价格并打印
private:
float r;
};
void swimming_pool:: get_r() {
float x;
cin >> x;
r = x;
}
void swimming_pool::gangway() {//计算过道的价格并打印
float s1, s2;//泳池面积为s1过道面积为s2
s1 = P * r * r;
s2 = P * (r + 3) * (r + 3);
cout << "过道费用:" << (s2 - s1) * 20 << "元" << endl;
}
void swimming_pool::paling()//计算输出围栏价格
{
float c;//栅栏长为c
c = 2 * P * r;
cout << "费用:" << 35 * c << "元" << endl;
}
int main()
{
swimming_pool A;
A.get_r();
A.gangway();
A.paling();
}