0

I'm trying to make my program be able to add Rational objects with integers using type conversion. I've gotten it to compile but when i run it i just get a bunch of random numbers over 1 so something like this -8993458/1. My code for the conversion is.

```
operator int(){return s;}
```

Could someone please help me solve my problem. My full code 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;
int s;
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&);
operator int(){return s;}
};
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;
cout << result.num << "/" << result.den << endl;
}
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;
}
```

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting