I am having some issues with my project. This is homework and I am not looking for answers, just some help. I am to create a RationalNumber class that takes two rational numbers (fractions), reduces them, and I am then supposed to use overloaded operators to add, subtract, multiply, and divide them (along with relational and equality operators as well but i need to handle the arithmetic first). I thought that i had my head wrapped around the notion of operator overloading, but I am still having issues. The program compiles but it certainly does not display the correct values. Any advice would be greatly appreciated! I am very new to C++ so don't be too hard on me. :-)

Header File:

#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H

#include <iostream>
using namespace std;


class RationalNumber {

	

public:

	RationalNumber(int, int);				//constructor to initialize setFraction functions
	RationalNumber();						//default constructor
		

	void setFraction(int, int);				//set fraction ones values and reduce it
	void setFractionTwo(int, int);			//set fraction ones values and reduce it
	void print();							//function to print the rational numbers

	RationalNumber operator+(RationalNumber &);			//overload the + operator
	RationalNumber operator-(RationalNumber &) ;		//overload the - operator
	RationalNumber operator*(RationalNumber&) ;			//overload the * operator
	RationalNumber operator/(RationalNumber&) ;			//overload the / operator

	RationalNumber operator>(RationalNumber &);
	RationalNumber operator<(RationalNumber &);
	RationalNumber operator>=(RationalNumber &);
	RationalNumber operator<=(RationalNumber &);

	RationalNumber operator==(RationalNumber &);
	RationalNumber operator!=(RationalNumber &);


private:
	
	int numerator;			//variable for first numerator
	int denominator;		//variable for first denominator
	int numerator2;			//variable for second numerator
	int denominator2;		//variable for second denominator

};


#endif

Source Code

#include <iostream>
#include "RationalNumber.h"
using namespace std;


RationalNumber::RationalNumber(int n, int d) {			//constructor initializes set fraction 
	
	setFraction(n, d);									//pass values top setFraction

	setFractionTwo(n, d);
}

RationalNumber::RationalNumber() {}


void RationalNumber::setFraction(int n, int d) {		//function to reduce the first fraction

	int a = n;
	int b = d;
	int r;
	
		while (d != 0) 

		{	
			r = n % d;
			n = d;
			d = r;		
		}
		

	numerator = a/n;
	denominator = b/n;

}

 void RationalNumber::setFractionTwo(int n, int d) {		//function to reduce the second fraction		

	int a = n;
	int b = d;
	int r;
	
		while (d != 0) 

		{	
			r = n % d;
			n = d;
			d = r;		
		}
		

	numerator2 = a/n;
	denominator2 = b/n;

} 


RationalNumber RationalNumber::operator+(RationalNumber &c) {

	RationalNumber result;

	result.numerator = ((numerator * denominator2) + (denominator * numerator2));
	result.denominator = (denominator * denominator2);

	return result;


} 

RationalNumber RationalNumber::operator-( RationalNumber &c)  {

	RationalNumber result;

	result.numerator = ((numerator * denominator2) - (denominator * numerator2));
	result.denominator = (denominator * denominator2);

	return result;


}

RationalNumber RationalNumber::operator*( RationalNumber &c)  {

	RationalNumber result;

	result.numerator = ((numerator * numerator2) - (denominator * denominator2));
	result.denominator = (denominator * denominator2);

	return result;


}

RationalNumber RationalNumber::operator/( RationalNumber &c)  {

	RationalNumber result;

	result.numerator = (numerator * denominator2);
	result.denominator = (denominator * denominator2);

	return result;


}

void RationalNumber::print()  { 

   cout << '(' << numerator << "/" << denominator << ')';

}

Driver

#include <iostream>
#include "RationalNumber.h"
using namespace std;


int main() {

int x, y;


  RationalNumber number;  
  RationalNumber number2;
  RationalNumber a; 


  cout << "Enter your first Numerator and Denominator: ";

  cin >> x >> y;

  number.setFraction(x, y);


  cout << "Enter your second Numerator and Denominator: ";

  cin >> x >> y;

  number2.setFraction(x, y);

  cout << "\n\n";

 
	cout << "First fraction reduced is: ";
 
	number.print();

	cout << "\n\n";

	cout << "Second fraction reduced is: ";  
	
	number2.print();

	 cout << "\n\n";



	a = number + number2;

	cout << "The two fractions added together: ";

	 a.print();

	 cout << "\n\n";



	cout << "The two fractions subtracted: ";

	a = number - number2;

	a.print();

	cout << "\n\n";



	cout << "The two fractions multiplied: ";

	a = number * number2;

	a.print();

	cout << "\n\n";



	cout << "The two fractions divided: ";

	a = number / number2;

	a.print();

	cout << "\n\n";


	

	
  
  


  
	system("pause");

	return 0;

};

I just don't think that I am on the right track.

I did not receive any errors. The program ran fine however the values were incorrect. I am trying a different approach and it is working better. I have re-written the source file. The issue that i am having now is reducing the fractions after they are added/subtracted, etc. I am assuming that I can add a loop into the operator functions to remedy this. Here is my progress so far:

#include <iostream>
#include "RationalNumber.h"
using namespace std;



RationalNumber::RationalNumber (int n, int d) 
	:numerator(n), denominator(d)	{

		setFraction(n, d);

}

RationalNumber::RationalNumber(){}


void RationalNumber::setFraction(int n, int d) {		

	int a = n;
	int b = d;
	int r;
	
		while (d != 0) 

		{	
			r = n % d;
			n = d;
			d = r;		
		}
		

	numerator = a/n;
	denominator = b/n;

}

void RationalNumber::print() {

	cout << '(' << numerator << "/" << denominator << ')';

}

RationalNumber RationalNumber::operator+(RationalNumber &c) {

	RationalNumber result;

	result.numerator = ((numerator * c.denominator) + (denominator * c.numerator));
	result.denominator = (denominator * c.denominator);

	return result;


}  

RationalNumber RationalNumber::operator-(RationalNumber &c) {

	RationalNumber result;

	result.numerator = ((numerator * c.denominator) - (denominator * c.numerator));
	result.denominator = (denominator * c.denominator);

	if(result.numerator == 0)

	{result.denominator = 0;   }

	return result;


} 

RationalNumber RationalNumber::operator*(RationalNumber &c) {

	RationalNumber result;

	result.numerator = (numerator * c.numerator);
	result.denominator = (denominator * c.denominator);

	return result;


} 

RationalNumber RationalNumber::operator/(RationalNumber &c) {

	RationalNumber result;

	result.numerator = (numerator * c.denominator);
	result.denominator = (denominator * c.denominator);

	return result;


}

>>The issue that i am having now is reducing the fractions after they are added/subtracted, etc.

You already have a function to reduce the fraction, why not reuse it by calling it within your operator functions. As so for example:

RationalNumber RationalNumber::operator+(RationalNumber &c) {

	RationalNumber result;

	result.setFraction(((numerator * c.denominator) + (denominator * c.numerator)),(denominator * c.denominator));

	return result;
}

Also, note that you can construct the object with the num / denom parameters directly too, as so:

RationalNumber RationalNumber::operator+(RationalNumber &c) {

	RationalNumber result(((numerator * c.denominator) + (denominator * c.numerator)),(denominator * c.denominator));

	return result;
}

And, then you might as well do this:

RationalNumber RationalNumber::operator+(RationalNumber &c) {
	return RationalNumber(((numerator * c.denominator) + (denominator * c.numerator)),(denominator * c.denominator));
}

hope it helps!

I see! I never thought to use my existing function, duh. So basically when I return the object it is passing those values back itself and sending them through my setFraction function again. Brilliant! I have all of my arithmetic operators working successfully. Now I am working on the relational and equality operators.

Thank you BTW for your advice. I completed this program about fifteen minutes ago. Everything works great. Here is my final source file.

#include <iostream>
#include "RationalNumber.h"
using namespace std;



RationalNumber::RationalNumber (int n, int d)					
	:numerator(n), denominator(d)	{							

		setFraction(n, d);										

}

RationalNumber::RationalNumber(){}


void RationalNumber::setFraction(int n, int d) {

	int a = n;													
	int b = d;
	int r;
	
		while (d != 0) 

		{	
			r = n % d;
			n = d;
			d = r;		
		}
		

	numerator = a/n;
	denominator = b/n;


		if (numerator <= 0) {									

			numerator, denominator = 0; }						


		if (denominator <= 0)  {								

			denominator = 1;									

				cout << "\nInvalid denominator value.  Results not accurate.  Reset value to 1.\n\n";  }				

}

void RationalNumber::print() {									

	cout << '(' << numerator << "/" << denominator << ')';

}


RationalNumber RationalNumber::operator+(RationalNumber &c) {			

	return RationalNumber (((numerator * c.denominator) +				

		(denominator * c.numerator)), (denominator * c.denominator)) ;		

}  


RationalNumber RationalNumber::operator-(RationalNumber &c) {			

	return RationalNumber ((numerator * c.denominator) -				
		(denominator * c.numerator), (denominator * c.denominator));	

} 


RationalNumber RationalNumber::operator*(RationalNumber &c) {							

	return RationalNumber((numerator * c.numerator), (denominator * c.denominator));	

} 


RationalNumber RationalNumber::operator/(RationalNumber &c) {							

	return RationalNumber ((numerator * c.denominator), (denominator * c.numerator));	

} 


bool RationalNumber::operator>(RationalNumber &c)  {						

	if ((numerator * c.denominator) > (c.numerator * denominator))		

	return true;

	else

	return false;
}


bool RationalNumber::operator<(RationalNumber &c)  {						

	if ((numerator * c.denominator) < (c.numerator * denominator))

	return true;

	else

	return false;
} 


bool RationalNumber::operator>=(RationalNumber &c)  {

	if ((numerator * c.denominator) >= (c.numerator * denominator))

	return true;

	else

	return false;
}


bool RationalNumber::operator<=(RationalNumber &c)  {	

	if ((numerator * c.denominator) <= (c.numerator * denominator))

	return true;

	else

	return false;
}



bool RationalNumber::operator==(RationalNumber &c) {

	return (numerator == c.numerator, denominator == c.denominator);	

}

Edited 5 Years Ago by DrShroom: n/a

This question has already been answered. Start a new discussion instead.