Check if two copy constructors are equal to eachother

I'm trying to see how I can set two fractions equal to eachother. With that, I have created a class called Fraction() that should be able to take two parameters in it such as Fraction One(5, 5) and Fraction Two(1, 1) to see if they equal to eachother, or if one of them is less or more.

Currently I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  class Fraction{
  public:
      Fraction(){}
      Fraction(int x, int y){
          first_num = x;
          second_num = y;
          total = first_num/second_num;

          returnTotal(total);
      }
      double returnTotal(double n){
      cout << n << endl;
      return n;
      }
  private:
      int first_num;
      int second_num;
      double total;
};


Now, the only thing I can do is get the Fraction One(5, 5) and Fraction Two(1, 1), But I don't have any way of comparing these to eachohter. It will just print out the total of both of these. And if I try to continue in main with comparison it won't let me.

I've tried:

1
2
3
4
5
6
7
8
9
10
int main(){
    Fraction One(1, 2);
    Fraction Two(2, 9);

    if(One == Two){
       cout << "They are equal" << endl;
    }

    return 0;
}


But that will simply give me an error saying that "No operator "==" matches these operands".

How can I compare these two copy constructors with eachother?

RE: Solved. I had to use operator overload in order to match the copy constructors with eachother. Down below is the code for getting it to work.
Last edited on
You need bool operator==(const Fraction &that) const, which you can implement like any other function.
Thank you @coder777, how could I implement something like that?
Last edited on
Side-note: your example main() declares two variables, One and Two but the condition has two unknown names f1 and f2. You should compare One and Two.

Similar example:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
int main(){
    int One {7};
    int Two {42};

    if ( One == Two ) {
       std::cout << "They are equal\n";
    }
    return 0;
}



When is one fraction equal to an another?
Is it when One.total == Two.total ?

One could think so. However, your Fraction::total is a double. A floating point value. Comparing them is strange and weird. Definitely not intuitive.

I have to ask why you create fractions from doubles? Would you create a fraction
2.718 / 3.14


Perhaps it would be enough to have "whole" fractions, like 4 / 3 or 16 / 12? Those you can compare with integer math, although you do need to do a bit more legwork.
@keskiverto I cleaned up the code with ur suggestions, the f2 and f1 is me misspelling. Sorry!

Would you be so kindly to assist me in the right way of comparing these?

I did some stuff with the fraction, would this be a good start on it? (I just made addition first, because that seemt to be the easiest way of grasping the concept). I'm a bit unsure on how to do this with comparing though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Fraction {
	public:
		int left;
		int right;
		Fraction(){}
		Fraction(int x, int y) {
			left = x;
			right = y;
		}
		friend Fraction operator+(const Fraction& x, const Fraction& y);
};
Fraction operator+(const Fraction& x, const Fraction& y) {
	Fraction sum;
	sum.left = x.left + y.left;
	sum.right = x.right + y.right;
	return sum;
}

int main() {
	Fraction f1(1, 2);
	Fraction f2(2, 9);

	Fraction result;
	result = f1 + f2;

	cout << result.left << " " << result.right << endl;
}
Last edited on
if its a high precision fraction class, you can do all the common denominator juggling to see if 2 fractions are equal.

if its for practical usage, ...

if fabs(num1/(double)denom1 - num2/(double)denom2) < 0.000000001 //your favorite tolerance value here
they are equal.

something like that in the body of your operator == overload.

comparison operator overloads tend to get redundant. you may want to make a private function that returns something like (in this case) the difference from that equation, and then you can do this...
operator == as above, if (fabs(fx(a,b)) < tinynumber) then equal else not
operator < if not equal, then (if fx(a,b) < 0) its true
operator > if not equal, then (if fx(a,b) > 0) its true
and <= and >= etc... typically you need <, >, ==, <=, >=, and possibly !=

Last edited on
That sum ... What are "left" and "right"? What is a "fraction"?

Is it left/right ? In that case you do f1=1/2, f2=2/9

How much is 1/2 + 2/9 ? Should it be 3/11?
If yes, then 1/2+1/2==2/4. In other words adding two halves together gives you one half?

Perhaps 1/2+1/2 should be 2/2 == 1/1, and 1/2+2/9 should make 13/18 ?


You have made the members public. Everybody can access them. There would be no need for friends.

A more canonical way is to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Foo {
  // member variables
public:
  Foo & operator=+ (const Foo & rhs);
};

Foo & Foo::operator+= (const Foo & rhs) {
  // add the rhs to this
  return *this;
}

Foo operator+ (Foo lhs, const Foo & rhs) {
  lhs += rhs;
  return lhs;
}

The += is a member and can access the private members.
The + is not a member nor a friend, because it uses the += that does all the work.

You should write relational operators == and <. The rest you get with #include <utility>
See http://www.cplusplus.com/reference/utility/rel_ops/
(Example uses == on doubles which is not nice.)
Thank you guys! I finally figured out how to do the comparing. What do you think of this?
1
2
3
4
5
6
7
8
bool operator==(const Fraction& x, const Fraction& y) {
	if (x.left == x.right && y.left == y.right) {
		return true;
	}
	else {
		return false;
	}
}

And in int main():
1
2
3
if (f1 == f2) {
		cout << "They are equal" << endl;
	}

That seem to work when both copy constructors are 100% equal, example:
1
2
Fraction f1(1, 1);
Fraction f2(1, 1);


I will edit the code with the great suggestions when it comes to practicing both good code and so forth, thanks!
Last edited on
That seem to work
How can this compile? Where do left/right come from?

See:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  class Fraction{
  public:
      Fraction(){}
      Fraction(int x, int y){
          first_num = x;
          second_num = y;
          total = first_num/second_num; // Note: this is an integer division

          returnTotal(total);
      }
      double returnTotal(double n){
      cout << n << endl;
      return n;
      }

// Note:
bool operator==(const Fraction& x) const {
	return (first_num == x.first_num) && (second_num == x.second_num);
}

  private:
      int first_num;
      int second_num;
      double total; // Note: this is not needed since it can be calculate any time
};


https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division#3602857
@coder777 look at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Fraction {
	public:
		int left;
		int right;
		double total;
		Fraction(){}
		Fraction(int x, int y) {
			left = x;
			right = y;
		}
};
bool operator==(const Fraction& x, const Fraction& y) {
	double leftSide = x.left/y.left;
	double rightSide = x.right / y.right;

	if (leftSide == rightSide) {
		return true;
	}
	else {
		return false;
	}
}
bool operator<(const Fraction& x, const Fraction& y) {
	double leftSide = x.left / y.left;
	double rightSide = x.right / y.right;

	if (leftSide < rightSide) {
		return true;
	}
	else {
		return false;
	}
}
bool operator>(const Fraction& x, const Fraction& y) {
	double leftSide = x.left / y.left;
	double rightSide = x.right / y.right;

	if (leftSide > rightSide) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	Fraction f1(4, 2);
	Fraction f2(1, 2);

	if (f1 == f2) {
		cout << "They are equal" << endl;
	}
	else if (f1 < f2) {
		cout << "f1 is less" << endl;
	}
	else {
		cout << "f2 is less" << endl;
	}

}
Last edited on
1
2
	Fraction f1(4, 2);
	Fraction f2(1, 2);


f2 is less
5 4



Well, @AnInteger, I hope that there aren't too many programmers (or anyone else for that matter) who believe that
4/2 + 1/2 ... is ... 5/4
I should've removed that from the part there @lastchance. It was just a test. It wasn't supposed to be a mathematical way of adding with fraction there. Just a way of grasping the concept of operator overloading.
Last edited on
I did give you a link to rel_ops. Even if you won't use it, you should look what it does.

For example, it says that operator> can be as simple as:
1
2
3
bool operator>  (const Fractio & x, const Fraction & y) {
  return y<x;
}


That is clearly shorter than your test version.

Furthermore, lets suppose that you notice an error in the logic. Since you essentially have the same logic in op< and op>, you have to fix both. That is tedious and error-prone. The oneliner version of op> above needs no maintenance; it is automatically correct, if op< is fixed.
This won't work if you expect 1/2 to == 2/4 etc.
that is why I suggested simply comparing the division... you could reduce the fractions to a common denominator and compare, but its a lot of work vs just 2 divisions and a comparison, both in code and internally. If you need absolute perfect precision, eg a banking program using fractions, then you should use the difficult method. If its just a fraction calculator for playing around, division is sufficient.

bool operator==(const Fraction& x, const Fraction& y) {
if (x.left == x.right && y.left == y.right) {
return true;
}
else {
return false;
}
}
...
Topic archived. No new replies allowed.