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