posted this in beginner, got no answer, could really use some help, having trouble understanding all the parts of creating a class.
I'm supposed to write a class Fraction 2 constructors: Fraction (); and Fraction (int numerator, int denominator); The second constructor will reduce the fraction if needed. Section 4.2 has an algorithm for finding the greatest common divisor of two numbers. Use it to write a function GCD; if the denominator is negative, the constructor will make it positive and make the numerator negative. the denominator of a fraction should not be 0. If the user passes a 0 denominator, the proper thing to do would be throw an exception, but we have not studied exception handling yet. So for now, we will assume a well-behaved use who will never pass a denominator of 0 to the constructor this is what i have so far all on main, have not separated into header and function.cpp yet
#include <iostream>
#include <string>
usingnamespace std;
class Fraction{
public:
Fraction();
Fraction(int numerator, int denominator);
string fractionToString();
int GCD(int a, int b);
private:
int numerator;
int denominator;
int a;
int b;
void reduce();
};
int main(int argc, constchar * argv[])
{
return 0;
}
Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}
Fraction::Fraction(int a, int b)
{
numerator = a;
denominator = b;
}
void Fraction::reduce()
{
int a, b, r, gcd;
a = (numerator);
b = (denominator);
if (a == 0)
{
numerator = 0;
denominator = 1;
return;
}
while (a != 0)
if (a<b)
{
r = a; a = b; b = r;
a = a - b;
}
int gcd = b;
numerator = numerator / gcd;
denominator = denominator / gcd;
}
int Fraction::GCD(int a, int b)
{
while (a != b){
if (b > a){
b = b - a;
}
else { a = a - b; }
}
return a, b;
}
string Fraction::fractionToString()
{
return"numerator"; to_string(numerator); "/"; "denomenator"; to_string(denominator);
}