Hey whats up everybody. Im new to the board and Im trying to understand this overloading thing but I dont have a clue. Anyways I have to write a program that adds 2 rational numbers and I have to overload + - * / . I also have to overload the relational and equality operators. Im not sure what that is. Any help will be appreciated

2. Its basically a easier better way to write statements, heres a quick cheat sheet.

Code:
```replace variable = variable + variable;

with variable += variable; //Adds variable to itself

replace variable = variable * variable;

with variable *= variable; // * variable by variable

and so on...```

Hope this helps.

3. Sorry man I dont understand that. I need to do something like this. Im not sure what to put inside the function. How can I add 2 rational numbers if I only pass in one. Im not sure but I think you can only pass in one parameter when you overload something. I also have numerator and denominator in my private section.

Code:
```Rational Rational::Operator +(Rational &rat)
{

}```

4. Originally posted by Ride -or- Die
Its basically a easier better way to write statements, heres a quick cheat sheet.

I think you want something like

Rational Rational::operator+(const Rational & rat1 , const Rational & rat2){

//put you code to add here

//and return the result

}

5. Code:
```class Rational
{
private:
int _numerator;
int _denominator;
public:
Rational() : _numerator(0),_denominator(1) {}
Rational(int num,int denom) : _numerator(num),_denominator((denom!=0) ? denom:1) {}
Rational(const Rational& rhs) : _denominator(rhs._denominator),_numerator(rhs._numerator){}
virtual ~Rational() {}
Rational operator + (const Rational& rhs)
{
Rational result(*this);
result._denominator+=rhs._denominator;
result._numerator+=rhs._numerator;
return result;
}
};```
Basic rational numbers class with construction,destruction and operator+ to add 2 rationals together defined.

6. nm i read it too quickly, sorry dude.

7. BTW Thats not how rational numbers are added. code is illustration purposes only.You will have to write a correct operator+.

8. overloading means using the same function name (or operator symbol) and pass it two different sets of paramenters and still get it to work.

Example:

//add takes two ints and does something with them

//yes, it is important which sequence you use parameters as this function is different than the one above to the compiler.

need I go further? The compiler can determine which version of add to call based on which set of parameters is passed. Note that polymorphism is a related, but different concept, having to do with object methods wherein methods of the same name perform differently based on the type of the object, not the parameters passed to the function/method.

You can do the same thing (overloading) with operators as operators are just shorthand versions of functions.

9. Ok this is what I have for my addition function. Please tell me whats wrong here I dont have a clue.
Code:
```#include <iomanip.h>
#include <stdio.h>
//******************************************************************************
class Rational{
friend ostream &operator<<( ostream &, Rational &);
public:
Rational(){ numerator = 0; denominator = 1;}; //constructor function
Rational( int n, int d) {numerator = n; denominator = d (d==0) ? 1:d;}
Rational operator +(Rational);
Rational operator -(Rational);
Rational operator *(Rational);
Rational operator /(Rational);
int gcd (int n, int d);
private:
int numerator;
int denominator;

};

//********************************gcd*******************************************
//function accepts 2 integers through call by value mechanism
//function computes the greatest common divisor and returns the GCD
int Rational :: gcd (int n, int d)
{
if (d == 0)
return n;
else
return (gcd(d, n % d));
}

//******************************************************************************
ostream& operator <<(ostream& out, rational& rat)
{
out << "The Rational value is: " << rat.numerator << " / " << rat.denominator << endl;
return out;
}
//function accepts 2 rational types through call by value mechanism
Rational Rational::operator +(Rational& rat)
{
Rational value;

value.numerator = rat.numerator * rat.denominator + rat.numerator * rat.denominator;
value.denominator = rat.denominator * rat.denominator;
int i = gcd(numerator, denominator);
if(i != 0 && i != 1){
numerator /= i;
denominator /= i;
}
return value;

}```

10. You will need for many thing for example if you have a class of HugeInteger( a data type that can store very big numbers ), and you want to add two of them, without overloading you'll do:
Code:
```hugeInt h1, h2;
//initialize
where add is a member function in class hugeInt.
but the other way is to overload the addition operator, so you can write:
Code:
```hugeInt h2, h2;
//initialize
h1 + h2;```
which makes more sense...
But remember you can only overload existing operators for user defined datatypes, and you cannot overload all operators.

11. Yeah i know thats for the main function but I wanted to know if my addition function is correct

12. Wouldn't you want to return a rational reference (&Rational) instead of a Rational?

13. No. operator + should always return an object and not a reference. Remember this rule. It will serve you well.

14. Thanks for the help guys but I jsut cant understand it.

15. heres a link to a simple rational numbers class. Please dont use the code as it is but instead rewrite it yourself. This will just show you how to do the operator overloads and describe some helpful functions such as gcd() and samedenominator() and lcm().
If you are still stuck then post your attempt and ill help.
http://www.gpc.peachnet.edu/~jbenson...ary/Rational.h
http://www.gpc.peachnet.edu/~jbenson...y/Rational.cpp