title: C++ Overloading == to compare objects from different classes
body: <p>For a lab, I had to overload the + operator to add objects from the same class and the == operator to compare objects from two <em>different</em> classes. The function to overload the == operator is giving me so much trouble (likely because I'm using it to compare the object areas of different classes). I've looked for a solution relentlessly, and I've tried all the suggestions found without success, so I'm forced to ask using my specific code:</p>
  
  <pre><code>// OBJECTIVES:
  // Add areas of 2 circles
  // Add areas of 2 rectangles
  // Compare areas of a circle and a rectangle
  
  #include &lt;iostream&gt;
  using namespace std;
  
  // **********************Header*********************
  class circle
  {
      friend bool operator==(const circle&amp; ,
                             const circle&amp;);
      friend circle operator+(const circle&amp; , 
                              const circle&amp;);
  public:
      double radius, area;
      void calcArea();
  };
  
  class rect
  {
      friend bool operator==(const rect&amp; , 
                             const rect&amp;);
      friend rect operator+(const rect&amp; , 
                            const rect&amp;);
  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 &gt;&gt; selection;
          cout &lt;&lt; endl;
  
          if (selection == 1) // add area of 2 circles
          {
              firstCircle.calcArea();
              secondCircle.calcArea();
              circle thirdCircle = firstCircle + secondCircle;
  
              cout &lt;&lt; "The sum of your two circles is: " ;
              cout &lt;&lt; thirdCircle.area;
              cout &lt;&lt; endl;
          }
  
          else if (selection == 2) // add area of 2 rectangles
          {
              firstRect.calcArea();
              secondRect.calcArea();
              rect thirdRect = firstRect + secondRect;
  
              cout &lt;&lt; "The sum of your two rectangles is: " ;
              cout &lt;&lt; thirdRect.area;
              cout &lt;&lt; endl;
  
          }
  
          else if (selection == 3) // compare areas of a circle and a rectangle
          {
              firstCircle.calcArea();
              firstRect.calcArea();
  
              if (firstCircle.area == firstRect.area)
              {
                  cout &lt;&lt; "The area of your circle is equal to that of your rectangle." &lt;&lt; endl;
              }
              else
              {
                  cout &lt;&lt; "The area of your circle is not equal to that of your rectangle." &lt;&lt; endl;
              }
          }
  
          else if (selection == 4) // exit program
          {
              return 0;
          }
  
          else
          {
              cout &lt;&lt; "Please enter a valid selection.";
              cout &lt;&lt; endl;
              continue;
          }
      } while (1);
  
      return 0;
  }
  // **************************************************
  
  // ******************Implementation******************
  void circle::calcArea() // compute circle area
  {
      cout &lt;&lt; "Enter a radius: ";
      cin &gt;&gt; radius;
  
      area = 3.14159265359 * radius * radius;
  }
  
  void rect::calcArea() // compute rectangle area
  {
      cout &lt;&lt; "Enter a length: ";
      cin &gt;&gt; length;
      cout &lt;&lt; "Enter a width: ";
      cin &gt;&gt; width;
  
      area = length * width;
  }
  
  bool operator==(const circle&amp; firstCircle, // compare areas of objects
                  const rect&amp; firstRect)  // from different classes
  {
      return (firstCircle.area == firstRect.area &amp;&amp; 
              firstCircle.area == firstRect.area);
  }
  
  circle operator+ (const circle&amp; firstCircle, // overload + for circle class
                      const circle&amp; secondCircle)
  {
      circle circleSum;
  
      circleSum.radius = firstCircle.radius + secondCircle.radius;
      circleSum.area = firstCircle.area + secondCircle.area;
  
      return circleSum;
  }
  
  rect operator+ (const rect&amp; firstRect, // overload + for rect class
                      const rect&amp; 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 &lt;&lt; endl;
      cout &lt;&lt; "What would you like to do?" &lt;&lt; endl;
      cout &lt;&lt; "1. Add the area of 2 circles."&lt;&lt; endl;
      cout &lt;&lt; "2. Add the area of 2 rectangles."&lt;&lt; endl;
      cout &lt;&lt; "3. Compare the area of a circle and a rectangle."&lt;&lt; endl;
      cout &lt;&lt; "4. Exit.";
  }
  // **************************************************
  </code></pre>
  
  <p>Right now, I'm not using the overloaded == to compare the rectangle and circle areas because then I get a lot of compiler errors. Any help you guys can offer me to be able to change <code>firstCircle.area == firstRect.area</code> to <code>firstCircle == firstRect</code> would be so greatly appreciated.</p>