简体   繁体   English

无法弄清楚如何在给定代码中计算圆的半径

[英]Cannot figure out how to calculate radius of the circle in the given code

I need to calculate the radius of the circle in the code.我需要在代码中计算圆的半径。

#include <iostream>

#include <cmath>

using namespace std;

class Circle {

float radius;
const float PI=3.14;

public:
Circle(float radius){

    }
    Circle getRadius()
    {
        return radius;
    }
    Circle setRadius(float radius)
    {
        this->radius = radius / PI;
        return radius;
    }
    
    float Area()
    {
        return radius*radius*PI;
    }
    
    float Perimeter()
    {
    
        return 2*radius*PI;
    }
    int equal()
    {
        return Area()==Perimeter();
    
    }

};
int main() {
float r;

    cin >> r;
    Circle c(r);
    cout << c.Perimeter() << endl;
    cout << c.Area() << endl;
    cout << c.equal() <<endl;
    
    
    return 0;

}

i tried to use pointers but i cannot figure it out.我尝试使用指针,但我无法弄清楚。

In

Circle(float radius){

}

a parameter with the same name as a member variable is not that member variable.与成员变量同名的参数不是该成员变量。 Instead, it shadows the member variable, effectively replacing it unless you know where to look.相反,它隐藏了成员变量,有效地替换了它,除非你知道去哪里找。 Passing a value into a parameter named radius will not change the value of the member variable named radius , and this leaves the member radius uninitialized.将值传递给名为radius的参数不会更改名为radius的成员变量的值,这会使成员radius保持未初始化状态。

This means that这意味着

float Perimeter()
{
    return 2*radius*PI;
}

and the other functions, excluding setRadius , operate on an uninitialized variable and the results will be undefined.和其他函数,不包括setRadius ,对未初始化的变量进行操作,结果将是未定义的。

A possible solution is to ensure the member radius is initialized to the value of the parameter radius .一种可能的解决方案是确保将成员radius初始化为参数radius的值。

You cannot simply你不能简单地

Circle(float radius){
    radius = radius;
}

because there is only one radius , the member has been shadowed, so the parameter radius assigns its value to itself.因为只有一个radius ,该成员已经被遮蔽,所以参数radius将其值赋给自己。 This is legal code so it compiles, but often the compiler will issue a warning.这是合法的代码,所以它可以编译,但编译器通常会发出警告。

Instead use而是使用

Circle(float radius) : radius(radius) {

}

This makes use of the Member Initializer List , one of the most important (and seemingly least-taught) features of the C++ programming language.这利用了Member Initializer List ,它是 C++ 编程语言最重要(而且似乎是教得最少的)功能之一。 It allows you to safely reuse the term radius because the first radius must be the member radius .它允许您安全地重复使用术语radius ,因为第一个radius必须是成员radius

You could also你也可以

Circle(float radius)  {
    this->radius = radius;
}

to explicitly use the member radius , but for more complicated members this approach can be inefficient.显式使用成员radius ,但对于更复杂的成员,这种方法可能效率低下。 All member variables and base classes are initialized before entering the body of the constructor.所有的成员变量和基类都在进入构造函数体之前被初始化。 Afterward the best you can do is assign.之后你能做的最好的就是分配。 This means the member will be default-initialized (if a default constructor exists; if it doesn't you must use the member initializer list), then a temporary variable must be constructed and this temporary is then assigned to the member.这意味着成员将被默认初始化(如果存在默认构造函数;如果不存在,则必须使用成员初始化列表),然后必须构造一个临时变量,然后将这个临时变量分配给成员。 potentially a lot of extra work for no benefit.可能有很多额外的工作没有任何好处。

Side note:边注:

Keep an eye on the this->radius = radius / PI;注意this->radius = radius / PI; in setRadius .setRadius中。 It is unusual and probably a bug.这是不寻常的,可能是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM