【问题标题】:Returning a boolean value from a Circle Class C++从 Circle 类 C++ 返回一个布尔值
【发布时间】:2014-10-29 16:47:18
【问题描述】:

我正在尝试使用 C++ 类返回一个布尔值。它需要能够使用我添加为类中的公共成员的重载运算符 > 来检查圆 A 是否与圆 B 大小相同。在我的 int main 中,即使圆圈大小相同,它似乎总是返回 false。

谢谢,提前。

圈类:

#include <iostream>
#include <cmath>

using namespace std;

//creating a constant pi that can't be changed
const double pi = 3.14159265;

class Circle
{
    //defining the private memeber variables
    private:
            double radius, xpos, ypos;

    //defining the public member variables
    public:
            //creating a constructor that takes all of the variables
            Circle(double r, double xposition, double yposition) {
                radius = r;
                xpos = xposition;
                ypos = yposition;
            }

            //creating a constructor that takes just the radius  
            Circle(double r) {
                radius = r;
                xpos = 0;
                ypos = 0;
            } 

            //creating a contructor that initialised everything to 0
            Circle() {
                radius = 0;
                xpos = 0;
                ypos = 0;
            }

            //defining the functions for radius, X-position, Y-position and area                       
            double getRadius() {return radius;}
            double getX() {return xpos;}
            double getY() {return ypos;}
            double getArea() {return pi*radius*radius;}

            //creating an overaload operator + to add the various properties of a circle together
            Circle operator+(Circle C) {
                radius = sqrt(this->getRadius()*this->getRadius() + C.getRadius()*C.getRadius()); //calculates the radius from the area
                xpos = (this->getX() + C.getX()) / 2.; //calculating the half way x position
                ypos = (this->getY() + C.getY()) / 2.; //calculating the half way y position
                return Circle(radius, xpos, ypos);
            } 

            //created an overload operator << that outputs information about the circle in a consistent manor
            friend ostream& operator<<(ostream& os, Circle C) {
                return os << "radius = " << C.getRadius() << " at (x,y) = (" << C.getX() << "," << C.getY() << ")";
            }

            bool operator>(Circle C) {
                if (this->getRadius() > C.getRadius()) {
                    return true;
                }
                else {
                    return false;
                }
            }
};

int main()

#include "Circle.hpp"

using namespace std;

int main()
{
    //defining the circles A and B
    Circle A(4.0,2.0,1.0);
    cout << "Circle A: " << A << endl;

    Circle B(4.0,5.0,6.0);
    cout << "Circle B: " << B << endl;

    //Adds A and B using the overload operator +
    Circle C = A + B;

    //Outputs the formatted text using the overload operator <<
    cout << "Circle C: " << C << endl;


    bool test;
    Circle D(4.0,2.0,1.0);

    if (A > D) {
        test = false;
    }
    else if (D > A) {
        test = false;
    }
    else {
        test = true;
    }

    cout << boolalpha << test << endl;

    return 0;
}

【问题讨论】:

  • 请注意,在 C++ 中,您通常会重载 operator&lt; 来定义排序。您不能使用operator&gt; 并没有什么特别的原因,但是标准库中的所有算法(例如)都使用operator&lt; 而不是operator&gt;

标签: c++


【解决方案1】:

A.r4.0D.r 也是 4.0D &gt; AA &gt; D 都不是真的。 &gt; 检查实际大于。如果您想要更大或相等,请改用&gt;=

【讨论】:

    【解决方案2】:

    当您执行以下操作时,您正在更改 A 的半径值

    Circle C = A + B
    

    【讨论】:

      【解决方案3】:

      “在我的 int main 中,即使圆圈大小相同,它似乎总是返回 false”

      当圆圈的大小与您的条件相同时,它肯定会返回 false:-

        if ( this->getRadius() > C.getRadius() ) 
            return true;
      

      除此之外的所有内容都将返回 false。如果您想在您的圈子大小相同时返回 true,请执行以下操作:-

        if ( this->getRadius() < C.getRadius() ) 
            return false;
        else 
            return true;
      

      针对评论进行了编辑:-

      那么如果你想测试三种不同的场景,你可能可以使用枚举:-

      if ( this->getRadius() > C.getRadius() )
          return ENUM_GREATER;
      else if ( this->getRadius() == C.getRadius() )
          return ENUM_EQUAL;
      return ENUM_LESSER;
      

      【讨论】:

      • 我认为他的意思是testfalse,只有在A &gt; DD &gt; A 时才会发生这种情况。
      • this->getRadius() > C.getRadius() 时需要返回true
      【解决方案4】:

      您的operator+ 修改了左侧操作数。所以,这一行修改了A

      Circle C = A + B;
      

      (包括radius 成员)。因此,当您将 AD 进行比较时,Aradius 不再是 4.0

      operator+ 可以修改如下来解决这个问题:

      Circle operator+(const Circle& C) const {
          double r = sqrt(this->getRadius()*this->getRadius() + C.getRadius()*C.getRadius()); //calculates the radius from the area
          double x = (this->getX() + C.getX()) / 2.; //calculating the half way x position
          double y = (this->getY() + C.getY()) / 2.; //calculating the half way y position
          return Circle(r, x, y);
      } 
      

      【讨论】:

      • 谢谢,这似乎有效:-)。有没有办法修改代码,所以它不会改变 A
      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      相关资源
      最近更新 更多