2

I have created an class Fraction in C++ that stores the numerator and denominator of a fraction, and has various methods to operate on these numbers. How would I compare the contents of one object to another? My thought was to pass a pointer to the second object to the first object's method that carries out the comparison. Any help would be much appreciated.

albertjorlando
  • 202
  • 2
  • 9
  • Don't you like prototype of [_standard_ comparison operator](http://stackoverflow.com/questions/13829704/overloading-the-comparison-operators-c) with a reference to both objects? – Adriano Repetti Nov 27 '13 at 17:30
  • You can write your own assignement "==" operator overloading function to do the comparision between two objects – Santosh Sahu Nov 27 '13 at 17:31
  • 2
    please do not overload the assignment operator to do comparison. – clcto Nov 27 '13 at 17:32
  • 1
    @SantoshSahu: That would be rather confusing. You'd usually write a comparison operator (`==` and so on) to do comparison, and an assignment operator to do assignment, unless you're laying traps for careless maintenance programmers. – Mike Seymour Nov 27 '13 at 17:32
  • 1
    For a fraction, be careful about simply comparing the numerator and denominator members, since two (denormalised) fractions could represent the same value with different members. – Mike Seymour Nov 27 '13 at 17:34
  • Thanks for all of the replies!! I will certainly read into operator overloading. As for the comparison I would probably just convert each to into decimal form and compare that way. – albertjorlando Nov 27 '13 at 17:54

4 Answers4

4

You can overload operator ==, like this:

bool operator==(const Fraction &left, const Fraction &right) {
    // Warning: this check is oversimplified; it may overflow!
    return left.num*right.denom == right.num*left.denom;
}

Now you can do this:

Fraction a(10, 20);
Fraction b(2, 4);
if (a == b) {
    ...
}

General information on operator overloading: link.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Another suggestion is to have method `bool operator==(const Fraction& other)` defined in the class. – Thomas Matthews Nov 27 '13 at 18:25
  • @ThomasMatthews Although overloading the member operator `==` is a valid alternative, I prefer implementing the non-member version of the operator `==`, because it is more "symmetric". – Sergey Kalinichenko Nov 27 '13 at 18:29
3

You should simply overload == operator.

zar
  • 11,361
  • 14
  • 96
  • 178
2

Take a look at this. Operator overloading

I recommend that you declare and define the appropriate overloaded operators. The answers at the above link provide you with guidance about how to accomplish that. That will allow you to use operators on class instances (objects) with the same natural syntax that you would use for built in types.

Community
  • 1
  • 1
shawn1874
  • 1,288
  • 1
  • 10
  • 26
1

If you are comparing with an object of the same type you can directly implement functions which compare the members even if they are private. If you want to test for equality, inequality and lower than / greater than then I suggest that you overload the appropriate operators:

Operator overloading

If, however, you wish to compare objects of different types then you have a few options. You can implement accessor methods ("getters") and use those in a comparing method, you can change the access rights to those variables (not encouraged) or you can use the friend keyword but use it sparingly.

When should you use 'friend' in C++?

Community
  • 1
  • 1
ApplePie
  • 8,814
  • 5
  • 39
  • 60