2

I've implemented the field of rational numbers and the operators (+,*,-,/) and (==,>,<) on this field in C++. This was straightforward, I created a class called rational for this.

I've also added the mathematical operators (+,*,-,/) and (==,>,<) for operations between fractions and integers. In my code any operation between an integer and a rational results in a rational.

I'm having trouble with the final step: I want to create an alternative division operator | on the integers, so that int a | int b = rational (a,b). How can this be done in C++? The issue is that I don't know how to add any methods to the "class of integers", I don't know how this "hidden" class can be edited. If I could do this I would just add the method

rational operator | (int x){
rational temp;
temp.numerator=value;
temp.denominator=x.value;
temp.reduce();
return temp;
}

to the integer class. Where I called the class variable of the integer class value. And used my existing reduction method from the rational class.

  • 3
    You cannot overload operators where both operands are built in types, or both operands are pointers to any type. – NathanOliver Sep 24 '19 at 16:37
  • 1
    "_I don't know how to add any methods to the "class of integers", I don't know how this "hidden" class can be edited._" Such class is not hidden. There is no such class in C++. – Algirdas Preidžius Sep 24 '19 at 16:37
  • @NathanOliver I thought there would be a smart way to overload operators where both operands are built in types. My compiler already suggested that there is not, but I thought maybe there is a way of doing it. I kinda see why though, you could of course break stuff with this feature. Thank you. –  Sep 24 '19 at 17:32

5 Answers5

3

First of all, I would advise against doing this, because operator | does not usually have this side effect. Overloading the operator in this way violates rule number 2 in these general rules for overloading operators.

Okay, with that our of the way, C++ does not allow you to overload operators for integral data types. That would potentially break code if they did.

However, you can overload operators with rationals. You can do it something like this:

class rational {
    rational operator|(rational other) {
        return rational(this->numerator, other.numerator);
    }
};

Then, you can create some constructors like this:

class rational {
    // I'm assuming you have something like this for the implementation of operator| to work
    rational(int n, int d) {
        this->numerator = n;
        this->denominator = d;
    }
    // This constructor creates a rational by putting everything over 1
    // This will function the same mathematically as the number itself
    rational(int n) {
        this->numerator = n;
        this->denominator = 1; // a single number is put over 1
    }

};

Now, you can use operator| like this:

rational r = rational(5) | 7;

Which seems to be close to what you want.


Note: When you get to here, if you make rationals accept a single number in a constructor and put it over 1, you may realize that you don't need a special operator| to create a rational this way. If your rational implements division by using multiplication by the reciprocal, you can simply do this instead:

rational r = rational(5) / 7;

Which is a lot clearer what you're intending to do anyway.

  • Thank you @Chipster. I already implemented this though. I thought there would be a smart way to overload operators where both operands are built in types. But as my compiler already suggested and many here have stated there is not. –  Sep 24 '19 at 17:30
1

I don't know how this "hidden" class can be edited.

There is no "hidden" class for integers. int and other integer types are not classes at all; and it is not possible to overload their arithmetic operators.

The issue is that I don't know how to add any methods to the "class of integers",

The first step is of course to create a class for integers. Just like you created a class for rationals.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

You can't: if you could change the behaviour and type of the expression a | b for int a and int b then can you imagine the hell that would be let loose?

Fortunately though the notation rational temp{a, b} is not a million miles away from what you wanted (assuming your rational class has the numerator and denominator members in that order), and given it's standard C++ it’s also readable.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I see the potential issues and my compiler already stated, that it is forbidden. But it would have been handy in my special case... thank you for confirming, that it won't work. –  Sep 24 '19 at 17:36
1

You can't, as other shave stated.

But what you could easily do is accept that Rational(int dividend) generates dividend/1.

Then the user could easily write Rational(dividend)/divisor, which you /can/ overload. You have to decide if that is better than Rational(dividend,divisor). Of course they might also write dividend/Rational(divisor), and you can overload that as well, if you want.

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27
  • Thanks this is pretty much the route I took after not getting my initial idea to work. –  Sep 24 '19 at 17:38
0

As said in commentaries you cannot overload built-in types.

What you can do is: Wrap your integer in a new class where you overload this class operators.