-3

I need help getting my function to overload the + operator to add a integer and a Rational object. Right now I am able to add two rational objects with my overloaded +. I made a second overloaded + function and now I am able to add an integer and an object if the object is on the lhs and the integer is on the rhs. If i try to add them with the object on the rhs and the integer on the lhs i get a bunch of errors. If someone could help me out I would appreciate it. An example of what i want it to do is

```
//What I get a bunch of errors with when i compile
int lhs(3);
Rational rhs(2);
Rational result;
result = lhs + rhs;
// What i got to work
Rational lhs(2);
int rhs(5);
Rational result;
result = lhs + rhs;
```

My code so far is below..

```
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Rational{
friend ostream &operator<<(ostream &, const Rational &);
friend istream &operator>>(istream &, Rational &);
public:
int num;
int den;
bool neg;
Rational(int n, int d); // contructor for rationals
Rational(int n); // contructor for reals
Rational();//default constructor
int get_num();
int get_denom();
int gcd();
void simplify();
bool is_negative();
Rational operator +(Rational&);
Rational operator +(int);
Rational operator -(Rational);
Rational operator *(Rational);
Rational operator /(Rational);
Rational& operator +=(Rational&);
Rational operator -=(Rational);
Rational& operator /=(Rational&);
Rational& operator *=(Rational&);
Rational operator ++(int);
Rational operator ++();
bool operator ==(Rational&);
bool operator <(Rational&);
};
int main(){
Rational result, a, rat(5), rat2(1,2), rat3(5/2), rat4(3,2);
int rhs = 3;
int lhs = 2;
result = lhs + rat2;
}
Rational::Rational(int n, int d)
{
if(n == 0)
{
num = n;
den = 1;
}else
{
num = n;
den = d;
}
}
Rational::Rational(int n)
{
num = n;
den = 1;
}
Rational::Rational()
{
num = 0;
den = 1;
}
int Rational::get_num()
{
simplify();
return abs(num);
}
int Rational::get_denom()
{
simplify();
return abs(den);
}
int Rational::gcd()
{ int a = num;
int b = den;
int temp;
while(b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
void Rational::simplify()
{
int gcdNum = gcd();
if(gcdNum != 0)
{
num = num / gcdNum;
den = den / gcdNum;
}
}
bool Rational::is_negative()
{
if(num < 0||den < 0)
neg = true;
else
neg = false;
return neg;
}
Rational Rational:: operator +(Rational& ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den + den * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational:: operator +(int intPassed)
{
Rational result;
result.num = intPassed * den + num;
result.den = den;
return result;
}
Rational Rational:: operator -(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den - den * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational:: operator *(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational:: operator /(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den;
ratResult.den = den * ratPassed.num;
return ratResult;
}
Rational& Rational::operator *=(Rational &m)
{
num *= m.num;
den *= m.den;
return *this;
}
Rational& Rational::operator +=(Rational &m)
{
num = num * m.den + m.num * den;
den *= m.den;
return *this;
}
Rational& Rational::operator /=(Rational &m)
{
num *= m.den;
den *= m.num;
return *this;
}
Rational Rational:: operator ++(int unused)
{
Rational temp = *this;
num += den;
return temp;
}
Rational Rational:: operator ++()
{
num += den;
return *this;
}
bool Rational:: operator ==(Rational& rhs)
{
return(num * rhs.den == den * rhs.num);
}
bool Rational:: operator <(Rational& rhs)
{
simplify();
rhs.simplify();
return(num * rhs.den) < (rhs.num * den);
}
ostream &operator<<(ostream &output, const Rational &rnum)
{
output << rnum.num << "/" << rnum.den;
return output;
}
istream &operator>>(istream &input, Rational &rnum)
{
input >> rnum.num;
input.ignore(1);
input >> rnum.den;
return input;
}
```