C Programming Help Here is the question httpstackoverflowcom

C++ Programming Help:

Here is the question: http://stackoverflow.com/questions/40632236/operator-overloading-in-a-fraction-program-c?noredirect=1#comment68497069_40632236

Solution

Here is the modified code for you:

#include<iostream>
using namespace std;

class Fraction {

friend ostream &operator<<( ostream&, const Fraction&);
friend istream &operator>>( istream&, Fraction&);

public:
Fraction(int = 0, int = 0, int = 1);

Fraction &operator+=(int);
Fraction &operator++();
Fraction operator++(int);

Fraction operator+(const Fraction &rhs) const
{
return Fraction(f1 * rhs.f2 + f2 * rhs.f1, f2 * rhs.f2);
}
Fraction operator-(const Fraction &rhs) const
{
return Fraction(f1 * rhs.f2 - f2 * rhs.f1, f2 * rhs.f2);
}
Fraction operator*(const Fraction &rhs) const
{
return Fraction(f1 * rhs.f1, f2 * rhs.f2);
}
Fraction operator/(const Fraction &rhs) const
{
return Fraction(f1 * rhs.f2, f2 * rhs.f1);
}
bool operator==(const Fraction &rhs) const
{
return f1 == rhs.f1 && f2 == rhs.f2;
}
bool operator!=(const Fraction &rhs) const
{
return f1 != rhs.f1 || f2 != rhs.f2;
}
  

bool operator> (const Fraction &rhs) const
{
return f1 > rhs.f1;
}

private:
int f1, f2;
};


Fraction &Fraction::operator ++() // preincrement
{
f1++;
f2++;
return *this;
}

Fraction Fraction::operator ++(int) // postincrement
{
Fraction temp = *this;
f1++;
f2++;
return temp;
}

// constructor
Fraction::Fraction(int a, int b, int c)
: f1(a), f2(b){}


// overloaded stream-insertion operator
ostream &operator<<( ostream &output, const Fraction &one)
{
output << \"Fraction one is: \" << one.f1 << \" \";
output << \"Fraction two is: \" << one.f2 << \" \" << endl;

return output; // enables cascading eg cout << a << b;
}

// overloaded stream-extraction operator
istream &operator>>( istream &input, Fraction &one)
{
input >> one.f1 >> one.f2;
return input; // enables cascading eg cin >> a >> b;
}

int main()
{
Fraction f1, f2, sum, diff, prod, quo;
cin >> f1;
cin >> f2;

cout << \"The first fraction is: \" <<f1 << endl << endl;
cout << \"THe second fraction is: \" <<f2 << endl << endl;

sum = f1 + f2;
diff = f1 - f2;
prod = f1 * f2;
quo = f1 / f2;

cout << \"The sum is: \" << sum << endl << endl;
cout << \"The difference is: \" << diff << endl << endl;
cout << \"The product is: \" << prod << endl << endl;
cout << \"The quotient is: \" << quo << endl << endl;

if(f1==f2)
cout<< \"The two fractions are equal!\";
else
cout<< \"The two fractions are not equal!\";

if(f1!=f2)
cout << \"The two fractions are not equal!\";
else
cout << \"The two fractions are equal.\";

cout << \"Pre-increment: \" << ++f1;
cout << \"Post-increment: \" << f1++;
cout << \"Post-increment: \" << f1;

return 0;
}

C++ Programming Help: Here is the question: http://stackoverflow.com/questions/40632236/operator-overloading-in-a-fraction-program-c?noredirect=1#comment6849706
C++ Programming Help: Here is the question: http://stackoverflow.com/questions/40632236/operator-overloading-in-a-fraction-program-c?noredirect=1#comment6849706
C++ Programming Help: Here is the question: http://stackoverflow.com/questions/40632236/operator-overloading-in-a-fraction-program-c?noredirect=1#comment6849706

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site