First of all this is a Homework assignment. I just need some help solving an issue related to the Fraction operator+() function. It is supposed to add together two fractions from an array in the function BinarayMathTest, but the Fraction::operator+() function only returns the denominator.
#include <iostream>
#include <string>
using namespace std;
class Fraction
{
private:
int num,denom;
public:
Fraction operator + (const Fraction &right)const;
friend ostream&operator<<(ostream&stream,Fraction obj);
Fraction(int a=0,int b=1){num=a; denom=b;}
};
ostream&operator<<(ostream&stream,Fraction obj)
{
stream<<obj.num<<'/';
stream<<obj.denom;
return stream;
}
Fraction Fraction::operator+(const Fraction &right)const
{
Fraction temp;
Fraction tempo;
Fraction full;
temp.num = ((num*right.denom) + (right.num*denom));
tempo.denom =(denom*right.denom);
full = (temp,tempo);
return full;
}
void BinaryMathTest();
int main()
{
BinaryMathTest();
return 0;
}
void BinaryMathTest()
{
cout << "\n----- Testing binary arithmetic between Fractions\n";
const Fraction fr[] = {Fraction(1, 6), Fraction(1,3),
Fraction(-2,3), Fraction(5), Fraction(-4,3)};
for (int i = 0; i < 4; i++) {
cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1]
<<endl;}}
/* OUTPUT
----- Testing binary arithmetic between Fractions
1/6 + 1/3 = 0/18
1/3 + -2/3 = 0/9
-2/3 + 5/1 = 0/3
5/1 + -4/3 = 0/3
*/