64,685
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
//#include <windows.h>
using namespace std;
const double PI = 3.14159;
const float Stockade_cost = 35; //每米的栅栏价格
const float Passage_cost = 20; //过道价格
class cal
{
public:
cal(float r); //构造函数
float perimeter() ; //栅栏长度
float area() const ; //过道面积
private:
float R; //半径
};
cal::cal(float r)
{
R = r;
}
float cal::perimeter()
{
return 2 * PI * R;
}
float cal::area()const
{
return PI * R * R;
}
int main()
{
float R;
float Stockade_cost;
float Passage_cost;
cout << "输入半径:";
cin >> R;
cal pool(R);
cal maxpool(R + 3);
Stockade_cost = maxpool.perimeter()*Stockade_cost;
cout << "栅栏造价:" << Stockade_cost << endl;
Passage_cost = (maxpool.area() - pool.area())*Passage_cost;
cout << "过道造价:" << Passage_cost << endl;
//system("pause");
return 0;
}
参考一下吧;
float Stockade_cost;
float Passage_cost;
这两个变量没有初始化,将会是随机值float cal::perimeter
{return 2 * PI * R; }
float cal::area const
{return PI * R * R;}
错了,声明在哪儿? 函数括号了?