【发布时间】:2016-11-04 05:37:08
【问题描述】:
问题#1:我如何构建一个构造函数来设置
(R,PoF,PoR)的值?我想了解构造函数的工作原理,但我想我不太明白。问题 #2:我可以用这种方式构建析构函数,而不是我在程序中使用的方式吗?
Circle::~Circle()
{
std::cout << "The fence would cost " << SwimmingPool.PerimeterP(r) << std::endl;
std::cout << "The road would cost " << SwimmingPool.AreaP(r) << std::endl;
std::cout << "Present by FF" << std::endl;
}
我只想让成本自己出来,但我不知道我应该如何创建析构函数来做到这一点。
这是我的完整代码:
#include "stdafx.h"
#include "iostream"
const double PI = 3.1415926;
class Circle
{
public:
Circle();
double AreaP(int r);
double PerimeterP(int r);
~Circle();
private:
int R;
int PoF;
int PoR;
};
double Circle::AreaP(int r)
{
return ((r + R)*(r + R) - r*r)*PI*PoR;
}
double Circle::PerimeterP(int r)
{
return (r + R) * 2 * PI*PoF;
}
Circle::Circle()
{
int R = 3;
int PoF = 35;
int PoR = 20;
}
Circle::~Circle()
{
std::cout << "Present by FF" << std::endl;
}
int main()
{
int r;
Circle SwimmingPool;
std::cout << "Please input the radius of the Swimming Pool." << std::endl;
std::cin >> r;
std::cout << "The fence would cost " << SwimmingPool.PerimeterP(r) << std::endl;
std::cout << "The road would cost " << SwimmingPool.AreaP(r) << std::endl;
return 0;
}
【问题讨论】:
-
如果你们能解释一下如何构建这两个东西就好了。我很困惑。我不知道如何解释我的问题,但我就是无法彻底思考这两件事......
-
我不知道 PoF 和 PoR 代表什么。
-
该程序旨在解决一个现实世界的程序。 PoF 代表围栏价格,PoR 代表道路价格。
标签: c++ constructor destructor