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

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

}

//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::RationalNu

{

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

}

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

like this:

RationalNumber RationalNumber::Operator+(

{

//

// 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

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

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

return(b);

}

/*add other functions and

cunstructors her*/

}

hope this solves your problem

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial