Hi, I am new to C++, verrry new. I have an assignment due for our class and I dont understand what we are supposed to do with it. Maybe someone can help me understand it or possibly point me in the right direction. Noo I am not looking for someone to take me to the destination point, but possible draw me a map on how to get there??? Thanks, I would appreciate it very much.
I have to modify the Fraction.h and Fraction.cpp files so that the program implements the overloaded operators *, /, -,>, and <.
I have to use the following program to test the new fraction class and I cant modify this program, because this is what they will be grading me on:
Code:
#include <iostream>
#include "Fraction.h"
int main()
{
Fraction a, b , c, d, e, f;
cout << "Enter 2 fractions\n";
cin >> b >> c;
a = b * c;
d = b / c;
e = b - c;
cout << "After multiply " << a << endl;
cout << "After divide " << d << endl;
cout << "After subtract " << e << endl;
if (b < c)
{
cout << b << " is less than " << c << "\n";
}
else
{
cout << b << " is not less than " << c << "\n";
}
if (b > c)
{
cout << b << " is greater than " << c << "\n";
}
else
{
cout << b << " is not greater than " << c << "\n";
}
return 0;
}
Fraction.h file
Code:
#include <iostream>
using namespace std;
class Fraction
{
private:
int top;
int bottom;
int gcd();
public:
Fraction(int, int);
Fraction();
void input(istream&);
void output(ostream&);
void add(Fraction, Fraction);
};
Fraction.cpp file
Code:
#include "Fraction.h"
Fraction::Fraction()
{
top = 0;
bottom = 1;
}
Fraction::Fraction(int a, int b)
{
top = a;
if (b != 0)
bottom = b;
else
bottom = 1;
}
void Fraction::input(istream& in)
{
int a, b;
char slash;
in >> a >> slash >> b;
if (slash != '/' || b == 0)
{
top = 1; bottom = 1;
}
else
{
top = a; bottom = b;
}
}
void Fraction::output(ostream& out)
{
out << top << "/" << bottom;
}
void Fraction::add(Fraction a, Fraction b)
{
bottom = a.bottom * b.bottom;
top = a.top * b.bottom + b.top * a.bottom;
int x = gcd();
bottom = bottom / x;
top = top / x;
}
int Fraction::gcd()
{
int x = top;
while ((top % x != 0) || (bottom % x != 0))
{
x--;
}
return x;
}