Overloaded Operators - How do I use them?

todd831
todd831 used Ask the Experts™
on
I am having some trouble trying to figure out how to overload the +, -, *, /, =, !=, <, > operators.  Also how do I use them?  I am entering 2 fractions how do I use them in my overloaded operators?  Thanks for your help.


//Create a constructor that prevents a 0 denominator in a fraction, reduces, or simplifies fractions
//that are not in reduced form and avoids negative denominators.

//These last two comments are where I am stuck.  What do I do with them?
//Overload the addition, subtraction, multiplication and division operators for this class.
//Overload the relational and equality operators for this class.

#include <iostream.h>

class RationalNumber
{
public:
     RationalNumber(int num, int denom, int num2, int denom2);                    //constructor num=numerator, denom=denominator
     RationalNumber RationalNumber::operator +(RationalNumber a); //I think these are my overloaded operators
     RationalNumber RationalNumber::operator -(RationalNumber a);
     RationalNumber RationalNumber::operator *(RationalNumber a);
     RationalNumber RationalNumber::operator /(RationalNumber a);
     RationalNumber RationalNumber::operator <(RationalNumber a);
     RationalNumber RationalNumber::operator >(RationalNumber a);
     RationalNumber RationalNumber::operator =(RationalNumber a);
     RationalNumber RationalNumber::operator !=(RationalNumber a);
     
private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

};

RationalNumber::RationalNumber(int num, int denom, int num2, int denom2)
{
     numerator = num;
     denominator = denom;
     numerator2 = num2;
     denominator2 = denom2;

     cout << "Numerator one is: " << numerator << "\n";
     cout << "Denominator one is: " << denominator << "\n";
     cout << "Numerator two is: " << numerator2 << "\n";
     cout << "Denominator two is: " << denominator2 << "\n";

//for first fraction
     if (denominator == 0 || denominator < 0)
          cout << "Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int i = numerator > denominator ? numerator : denominator;

          while(i > 1)
          {
               if(numerator % i == 0 && denominator % i == 0)  
               {
               numerator /= i;
               denominator /= i;
               }
               --i;
          }
         
     }

     cout << "Simplified Numerator one is: " << numerator << "\n";
     cout << "Simplified Denominator one is: " << denominator << "\n";

//For second fraction
     if (denominator2 == 0 || denominator2 < 0)
          cout << "Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int j = numerator2 > denominator2 ? numerator2 : denominator2;

          while(j > 1)
          {
               if(numerator2 % j == 0 && denominator2 % j == 0)  
               {
               numerator2 /= j;
               denominator2 /= j;
               }
               --j;
          }
         
     }
     cout << "Simplified Numerator two is: " << numerator2 << "\n";
     cout << "Simplified Denominator two is: " << denominator2 << "\n";

}

void main(void)
{
     
     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber Call(top, bot, top2, bot2);

}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ummm, where are the implementations of the overloads?

Commented:
Your operators need to have function bodies just like other members of the class.

like this:
RationalNumber RationalNumber::Operator+(RationalNumber a)
{
//
// adding code goes here
//
return a;
}

now when you say
RationalNumber A, B;
A = A + B;

your function will get called.

Hope that's what you were looking for.
-Melekor

Author

Commented:
burcarpat - That is the problem. I am not sure how to use them?

Melekor - Thanks much.  But I have some questions.
//So is this below my overloaded operator constructor?  And are mine in the right spot and written correctly in the CLASS?  See other comments below.
 RationalNumber RationalNumber::Operator+(RationalNumber a)
{
//
// adding code goes here
//
return a;
}

//Is this in main?  I think it is.  
now when you say
RationalNumber A, B;
A = A + B;

//Thanks again for your help.  I am having a very hard time understanding this overloading and class stuff.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Commented:
Overloaded functions are like any other functions, but with the same name. The only thing that differs is the argument list.
If you want a function add() to be overloaded, the following code will help you understand the concept:

int add(int a, int b)
{
    int c;
 
    c = a + b;
   
    return c;
}

float add(float a, float b)
{
    float c;

    c = a + b;
 
    return c;
}

You can see that two functions differ in the argument list and in the definition. Now when you call add() with 2 int arguments, the first function will be called. If you call the same add() with float arguments, then the second add() will be called.
The same is true for operator overloading.

Hope you got the concept cleared. If you have more doubts about this please clarify.

-Narendra
i got your problem.
 suppose you have got a class fraction
 in which thre is num and denum.
 and suppose that you got to add num of one instance to another:
 or say you want to add every elment of one to other:
here's how to do it:
 
class fract
 {
  private:
   int num,denum;
 public:
  int operator +(fract a)
   {
    fract b;
    b.num=this->num+a.num;
    b.denum=this->denum+a.denum;
    return(b);
   }
  /*add other functions and
   cunstructors her*/
  }
 
 hope this solves your problem        

Commented:
todd831,
Did you get your problem solved!?
Please do give your feedback....

Author

Commented:
Well ssnkumar.  Still rather confused.  Here is where I am at.  I think I have my class and overloaded operators right now.  But still confused on how I call the Operator+ function from main().  I left some code out that is on the top.

class RationalNumber
{
public:
     RationalNumber(int num, int denom, int num2, int denom2);     //constructor num=numerator, denom=denominator
     RationalNumber RationalNumber::operator +(RationalNumber a);

private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

RationalNumber RationalNumber::operator +(RationalNumber d)
{
     RationalNumber f;  //This is compile error number 1
     
     f.numerator=this->numerator + a.numerator;
   f.denominator=this->denominator + a.denominator;
   return(f);

}

void main(void)
{
     
     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber Call(top, bot, top2, bot2);
     RationalNumber add(top);//Hopefully calls the + overloader?
}
Well My call RationalNumber add(top); does not work.  I get compile errors.  These are below.  Can I get an example of what I am trying to do with the call to it from main?

error C2512: 'RationalNumber' : no appropriate default constructor available

'__thiscall RationalNumber::RationalNumber(const class RationalNumber &)' : cannot convert parameter 1 from 'int' to 'const class RationalNumber &'
        Reason: cannot convert from 'int' to 'const class RationalNumber'
        No constructor could take the source type, or constructor overload resolution was ambiguous

Author

Commented:
Well folks I believe I got them working.  thanks for all the help...todd831

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial