C ++重载==比较不同类的对象

时间:2018-10-30 03:39:19

标签: c++ class overloading

对于实验室,我必须重载+运算符以添加同一类的对象,而==运算符以重载来自两个不同类的对象。重载==运算符的函数给我带来了很多麻烦(可能是因为我正在使用它比较不同类的对象区域)。我一直在不懈地寻找解决方案,并且尝试了所有发现的建议,但都没有成功,因此我不得不使用我的特定代码进行询问:

regionBootstrap.disable()

现在,我没有使用重载的==来比较矩形和圆形区域,因为这样我会遇到很多编译器错误。你们能为我提供的将// OBJECTIVES: // Add areas of 2 circles // Add areas of 2 rectangles // Compare areas of a circle and a rectangle #include <iostream> using namespace std; // **********************Header********************* class circle { friend bool operator==(const circle& , const circle&); friend circle operator+(const circle& , const circle&); public: double radius, area; void calcArea(); }; class rect { friend bool operator==(const rect& , const rect&); friend rect operator+(const rect& , const rect&); public: double length, width, area; void calcArea(); }; void displayMenu(); // ************************************************** // **********************Program********************* int main() { int selection; // variable for menu selection circle firstCircle; // objects from circle class circle secondCircle; rect firstRect; // objects from rect class rect secondRect; do { displayMenu(); cin >> selection; cout << endl; if (selection == 1) // add area of 2 circles { firstCircle.calcArea(); secondCircle.calcArea(); circle thirdCircle = firstCircle + secondCircle; cout << "The sum of your two circles is: " ; cout << thirdCircle.area; cout << endl; } else if (selection == 2) // add area of 2 rectangles { firstRect.calcArea(); secondRect.calcArea(); rect thirdRect = firstRect + secondRect; cout << "The sum of your two rectangles is: " ; cout << thirdRect.area; cout << endl; } else if (selection == 3) // compare areas of a circle and a rectangle { firstCircle.calcArea(); firstRect.calcArea(); if (firstCircle.area == firstRect.area) { cout << "The area of your circle is equal to that of your rectangle." << endl; } else { cout << "The area of your circle is not equal to that of your rectangle." << endl; } } else if (selection == 4) // exit program { return 0; } else { cout << "Please enter a valid selection."; cout << endl; continue; } } while (1); return 0; } // ************************************************** // ******************Implementation****************** void circle::calcArea() // compute circle area { cout << "Enter a radius: "; cin >> radius; area = 3.14159265359 * radius * radius; } void rect::calcArea() // compute rectangle area { cout << "Enter a length: "; cin >> length; cout << "Enter a width: "; cin >> width; area = length * width; } bool operator==(const circle& firstCircle, // compare areas of objects const rect& firstRect) // from different classes { return (firstCircle.area == firstRect.area && firstCircle.area == firstRect.area); } circle operator+ (const circle& firstCircle, // overload + for circle class const circle& secondCircle) { circle circleSum; circleSum.radius = firstCircle.radius + secondCircle.radius; circleSum.area = firstCircle.area + secondCircle.area; return circleSum; } rect operator+ (const rect& firstRect, // overload + for rect class const rect& secondRect) { rect rectSum; rectSum.length = firstRect.length + secondRect.length; rectSum.width = firstRect.width + secondRect.width; rectSum.area = firstRect.area + secondRect.area; return rectSum; } void displayMenu() // menu options { cout << endl; cout << "What would you like to do?" << endl; cout << "1. Add the area of 2 circles."<< endl; cout << "2. Add the area of 2 rectangles."<< endl; cout << "3. Compare the area of a circle and a rectangle."<< endl; cout << "4. Exit."; } // ************************************************** 更改为firstCircle.area == firstRect.area的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

有很多方法可以编写所需的比较运算符。不干预类代码的最简单方法就是实现几个非成员运算符:

bool operator==(const circle& c, const rect& r) { return r.area == c.area; }
bool operator==(const rect& r, const circle& c) { return c.area == r.area; }

您可以将它们放在main函数上方的源文件中,或放入单独的头文件中。请注意,由于circle的成员是公开的,因此他们不必成为rectarea的朋友。

另一种方法是编写成员函数,但首先我们需要修复您现有的比较运算符:

class circle
{
public:
    bool operator==(const circle& other) const { return area == other.area; }

    // ...skipped...
};

它与您的版本有何不同?

  1. 这是一个将thisother进行比较的成员函数,仅使用一个参数。
  2. circle类以外的地方打电话是公开的
  3. 它是const限定的,因为它只需要查找类数据而无需修改。

还可以使用const限定符来比较const对象,如下所示:

const circle c1 = getSomeCircle();
const circle& c2 = otherCircle;
c1 == c2; // Note: statement has no effect, despite it's syntax is correct.
c2 == c1;

非常量限定的比较将无法编译。总而言之,这是用C ++编写比较运算符的最惯用的方式。

最后,让我们添加圆圈以进行矩形比较:

class rect;

class circle
{
public:
    bool operator==(const circle& other) const { return area == other.area; }
    bool operator==(const rect&) const;

    // ...skipped...
};

class circle { /* class definition skipped */ };

bool operator==(const rect& r) const { return area == r.area; }

首先,我们声明了rect类。当class被声明但未定义时(即 forward声明),我们可以使用指向其实例的指针ant引用,但不能使用实例本身。我们不能使用实例的原因之一是类的大小在定义之前是未知的。

然后,我们声明成员operator==接受对向前声明的类的引用,最后在rect定义之后,我们可以实现运算符。

rectcircle的比较可以完全相同的方式实现。

答案 1 :(得分:0)

运算符重载可以像下面这样重载。

#include <iostream>
using namespace std;

class rect;

class circle
{
    friend bool operator==(const circle&,
        const circle&);
    //friend circle operator+(const circle&,
    //  const circle&);
public:
    double radius, area;
    //void calcArea();
public:
    friend bool operator == (const circle&, const rect&);
    double getArea()const { return area; }
};

class rect
{
    friend bool operator==(const rect&,
        const rect&);
    //friend rect operator+(const rect&,
    //  const rect&);
public:
    double length, width, area;
    //void calcArea();
public:
    friend bool operator == (const rect&, const circle&);
    double getArea() const{ return area; }
};

int main(int argc, char *argv[]) {
    circle c;
    rect r;
    if (c == r) {
        cout << "It was a miracle a random circle is equal to a random rectangle in size!!!" << endl;
    }
    return 0;
}

bool operator == (const circle &c1, const circle &c2) {
    return c1.getArea() == c2.getArea();
}

bool operator == (const rect &r1, const rect &r2) {
    return r1.getArea() == r2.getArea();
}

bool operator == (const circle &c, const rect &r) {
    return c.getArea() == r.getArea();
}

bool operator == (const rect &r, const circle &c) {
    return c.getArea() == r.getArea();
}