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;
}

Well you are making a novice mistake her - not to worry - we all did. You should not overload the binary operators like +,-,==, and so on as members, but as globals and make the global operators friends of the class if neccessary. The reason for this is that members always have the this-object as implicit parameter and you only have limited control about type modifaction for "this". This leads to an dis-symmetry in the logically symmetric arguments of your operator. A better approach is to overload only the unary opeators as members and the binary ones as globals and then you are able to avoid this dissymmetry.

class X
{
//...
   X& operator += (const X& rhs)
   {
      // implement
      return *this;
   }
   // do the same for -=, *=, ...

   // then make friends with the global binary operators (if neccessary :- you might
   // also avoid making them friends if you have public accessors...)
   friend X& operator + (const X& lhs, const X& rhs); // lhs and rhs hve the same type
                                                      // and modifyers
   friend bool operator == (const X& lhs, const X& rhs);

   // [B]And here is the answer to your actual question:[/B]
   // implement 2 global overloads with switched argument types
   friend X& operator + (const X& lhs, const Y& rhs);
   friend X& operator + (const Y& lhs, const X& rhs);
}

// this goes into the implentation file (not the header)
X& operator +(const X& lhs, const X& rhs)
{
   // here you can use the unary += operator of X to do your adding
   X reval = lhs;
   reval += rhs;

  return reval;
}
bool operator == (const X& lhs, const X& rhs)
{
  // do your equality operation
  return lhs.member1 == rhs.member1 && lhs.member2 == rhs.member2;
}

X& operator + (const X& lhs, const Y& rhs)
{
    X reval = rhs;
    // here do the adding of a Y-object
    reval.member1 += rhs.ymemberXYZ;
    return reval;
}
X& operator + (const Y& lhs, const X& rhs)
{
   // here you might be able to reuse the implementation of the operator above
   // if your + operator is meant to be symmetric (x+y == y+x which is true for most
   // arithmetic types
   return rhs+lhs; // this will call the operator above
}

Hope that helps.

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;
}

How many copies of this thread are you going to create? I've already answered this question, among others, in one of your other threads.

Edited 5 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.