Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-13-2002
Turbo02
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
• 12-13-2002
RoD
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.
• 12-13-2002
Turbo02
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) { }```
• 12-13-2002
beege31337
Quote:

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

}
• 12-13-2002
Stoned_Coder
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.
• 12-13-2002
RoD
nm i read it too quickly, sorry dude.
• 12-13-2002
Stoned_Coder
BTW Thats not how rational numbers are added. code is illustration purposes only.You will have to write a correct operator+.
• 12-14-2002
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.
• 12-14-2002
Turbo02
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; } //*******************************add******************************************** //function accepts 2 rational types through call by value mechanism //function adds two rational numbers 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; }```
• 12-15-2002
ammar
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 h1.add(h2)```
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.
• 12-15-2002
Turbo02
Yeah i know thats for the main function but I wanted to know if my addition function is correct
• 12-15-2002
golfinguy4
Wouldn't you want to return a rational reference (&Rational) instead of a Rational?
• 12-15-2002
Stoned_Coder
No. operator + should always return an object and not a reference. Remember this rule. It will serve you well.
• 12-15-2002
Turbo02
Thanks for the help guys but I jsut cant understand it.
• 12-16-2002
Stoned_Coder
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last