# Overloaded Operators - How do I use them?

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 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® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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)
{
//
//
return a;
}

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

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

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)
{
//
//
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.

Commented:
Overloaded functions are like any other functions, but with the same name. The only thing that differs is the argument list.

{
int c;

c = a + b;

return c;
}

{
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.

-Narendra
Commented:
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);
}
cunstructors her*/
}

Commented:
todd831,
Did you get your problem solved!?

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

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

Do more with