I'm having trouble figuring out how to overload the +=, -=, *=, and /= for a program that does operations with rational numbers. Could someone please help me out.

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class 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 -(Rational);
	  Rational operator *(Rational);
	  Rational operator /(Rational);
	  Rational operator +=(Rational);
	 // Rational operator -=(Rational);
	  //Rational operator /=(Rational);
	  //Rational operator *=(Rational);
   
	  //int num, den, real, final_num, numer1, numer2, denom;

};



int main(){
	Rational result, a, rat(1,2), rat2(3,2), rat3(6,-3), rat4(-3,-8);
	
	//result.den = a.get_denom();
	//result.num = a.get_num();
	//result.neg = a.is_negative();

	//cout << result.num << "/" << result.den << endl;
	//cout << result.neg << endl;
	rat += rat2;

	cout << rat.num << "/" << rat.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 -(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 ratPassed)
{
	Rational ratResult;
	ratResult.num = num * ratPassed.den + den * ratPassed.num;
	ratResult.den = den * ratPassed.den;

	return ratResult;
}
/*Rational Rational::operator *=(Rational lhs, Rational rhs)
{
	lhs.num = lhs.num * rhs.num;
	lhs.den = lhs.den * lhs.den;

	return lhs;
}*/
	
	//Rational operator -=(Rational)
	  //Rational operator /=(Rational)
	  //Rational operator *=(Rational)
引领3妓生活 commented: 222 +0
Member Avatar for embooglement

I'm going to go out on a limb and say this is for an assignment.

What exactly are you having trouble with when it comes to overloading those operators? I'm not going to do your assignment for you, but I will try and point you in the right direction.

I found this I havn't tried it yet but I was just wondering what some of the things in this function do or are. Like the & after Rational, what is that for? I know the & before m means it is being passed by reference, but i'm not sure what the other one is for. Also what is *this, I think it's a pointer to the current object. If I'm right though could someone give me a little more info on it and how it is used most of the time. Thank you.

Rational& Rational::operator *=(const Rational &m)
{
num *= m.num;
den *= m.den;
return *this;
}
Member Avatar for embooglement

Anything of the form "Type&" means it's a reference to an instance of Type. So the & after Rational means the exact same thing as the & before m, namely that it's a reference. If you don't fully understand what references are, I'd suggest some solid googling of the topic, because they're fairly fundamental to using C++ effectively.

And yes, "this" is a pointer to the current object. That means that "*this" (dereferencing this) IS the object. So by doing "return *this;" that means that you're returning a reference to the current object. The reason you want to do that for assignment operators is so they can be appropriately chained together, like "a = b = c;" which would assign the value of c to a and b.

One general pattern that's used for overloading assignment operators is to use whatever overloaded version of whatever operator they're compounded with.

Put in a slightly different way, if you're defining operator+=, you should use the operator+ you created for the class.

So something like this:

Rational& Rational::operator+=(const Rational& m)
{
    *this = *this + m;
    return *this;
}

So notice that the only thing I defined n the addition-assignment operator was the assignment part. I left the addition part to the already created addition operator. You'll find that overloading things this way is usually the easiest way to do it, and makes updating code much easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.