In: Computer Science
Written in C++
Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator.
Also include a member function that returns the values of the numerator divided by the denominator as a double.
Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the numerator and denominator, and dividing by that number.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<iostream>
using namespace std;
class Fraction{
private:
int num, den;
public:
Fraction(int num=1, int
den=1);
void setNumerator(int num);
void setDenominator(int den);
double toDouble() const;
void displayToLowestTerm()
const;
};
Fraction::Fraction(int num, int den): num(num),
den(den){}
void Fraction::setNumerator(int num){this->num =num;}
void Fraction::setDenominator(int den){this->den=den;}
double Fraction::toDouble() const{
return num*1.0/den;
}
void Fraction::displayToLowestTerm() const{
int gcd = 1;
for(int f=1; f<num && f<den; f++){
if(num%f==0 && den%f==0)
gcd = f;
}
cout<<"Reduced Fraction: " <<
num/gcd<<"/"<<den/gcd <<endl;
}
int main(){
Fraction f1(56,88);
cout<<"To double: " <<f1.toDouble()
<< endl;
f1.displayToLowestTerm();
return 0;
}
===================================================================