pex3_3.cpp
来自「数据结构C++代码,经典代码,受益多多,希望大家多多支持」· C++ 代码 · 共 79 行
CPP
79 行
#include <iostream.h>
const float PI = 3.14159265;
// declare Circle class with data and method declarations
class Circle
{
private:
// data member radius is a floating point number
float radius;
public:
// constructor
Circle(float r);
// measurement functions
float Circumference(void) const;
float Area(void) const;
float SectorArea(float theta) const;
};
// class implementation
// constructor initializes the radius data member
Circle::Circle(float r): radius(r)
{ }
// return circumference
float Circle::Circumference() const
{
return 2 * PI * radius;
}
// return area
float Circle::Area() const
{
return PI * radius * radius;
}
// return area
float Circle::SectorArea(float theta) const
{
return (theta/360) * PI * radius * radius;
}
void main ()
{
float radius, numberRolls;
float fenceCost, lawnCost, lawnArea;
// set fixed point output with 2 decimal places
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
// prompt for and input radius
cout << "Enter the radius of the playground: ";
cin >> radius;
// declare the Circle objects
Circle playground(radius);
// Compute the cost of the Fence and output its value
fenceCost = playground.Circumference() * 2.40;
cout << "Fencing Cost is $" << fenceCost << endl;
lawnArea = playground.Area() - playground.SectorArea(30);
numberRolls = lawnArea / 16;
lawnCost = numberRolls * 4.00;
cout << "Lawn Cost is $" << lawnCost << endl;
}
/*
<Run>
Enter the radius of the playground: 100
Fencing Cost is $1507.96
Lawn Cost is $7199.48
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?