5月4日打卡
例4-3
题目描述:
游泳池预算改造,Circle类。
一个圆形游泳池,现需在其周围建一个圆形过道,并在其四周上栅栏。栅栏的价格为35元/米,过道造价为20元/米。过道宽度为3米,游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
设计思路:
1.设计一个类用于计算圆形的周长和面积,
2.利用得到的周长和面积计算造价。
流程图:
代码部分:
#include<iostream> using namespace std; const float PI = 3.141593; const float FENCE_PRICE = 35; const float CONCRETE_PRICE = 20; class Circle { public: Circle(float r); float circumference(); float area(); private: float radius; }; Circle::Circle(float r) { radius = r; } float Circle::circumference() { return 2 * PI * radius; } float Circle::area() { return PI*radius* radius; } int main() { float radius; cout << "Enter the radius of the pool:"; cin >> radius; Circle pool(radius); Circle poolRim(radius+3); float fenceCost = poolRim.circumference() * FENCE_PRICE; cout << "Fencing Cost is $ " << fenceCost << endl; float concreteCost = (poolRim.area() - pool.area()) * CONCRETE_PRICE; cout << "Concrete Cost is $ " << concreteCost << endl; return 0; }