Write a fraction class whose objects will represent fractions. You should provid
ID: 3539385 • Letter: W
Question
Write a fraction class whose objects will represent fractions. You should provide the following member functions:
Two constructors, a default constructor which assigns the value 0 to the fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the fraction, and the second parameter will represent the initial denominator of the fraction.
Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a fraction object. They should be named AddedTo, Subtract, MultipliedBy, and DividedBy.
A boolean operation named isEqualTo that compares two fraction objects for equality.
An output operation named print that displays the value of a fraction object on the screen in the form numerator/denominator.
Your class should have exactly two data members, one to represent the numerator of the fraction being represented, and one to represent the denominator of the fraction being represented.
When adding or subtracting fractions, remember that you must first find the common denominator. The easy way to do this is to multiply the denominators together and use that product as the common denominator.
I am providing a client program for you below. You should copy and paste this into a file and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below, so that you can check your results. Since you are not writing the client program, you are not required to include comments in it.
This client should produce the output shown here:
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
fraction f1(9,8);
fraction f2(2,3);
fraction result;
cout << "The result starts off at ";
result.print();
cout << endl;
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.MultipliedBy(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.DividedBy(f2);
result.print();
cout << endl;
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.AddedTo(f2);
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2);
result.print();
cout << endl;
if (f1.isEqualTo(f2)){
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}
const fraction f3(12, 8);
const fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
system ("PAUSE");//exclude statement if not using Dev-C++
return 0;
}
// Fraction class specification file
class fraction
{
public:
fraction (); //default class constructor
fraction (int numerator2, int denominator2); // class constructor
void set (int numerator2, int denominator2);
fraction MultipliedBy (fraction otherfraction) const;
fraction DividedBy (fraction otherfraction) const;
fraction AddedTo (fraction otherfraction) const;
fraction Subtract (fraction otherfraction) const;
bool isEqualTo (fraction otherfraction) const;
void normalise();
void print() const;
private:
int numerator;
int denominator;
};
// Fraction class implementation file
#include "fraction.h"
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
while(a%b != 0)
{
int t = a%b;
a = b;
b = t;
}
return b;
}
fraction::fraction ()
{
numerator=0;
denominator=1;
}
fraction::fraction (int numerator2, int denominator2)
{
numerator=numerator2;
denominator=denominator2;
normalise();
}
void fraction::set (int numerator2, int denominator2)
{
numerator=numerator2;
denominator=denominator2;
normalise();
}
fraction fraction ::MultipliedBy (fraction otherfraction) const
{
int tempNumerator = numerator*otherfraction.numerator;
int tempDenominator = denominator*otherfraction.denominator;
return fraction(tempNumerator, tempDenominator);
}
fraction fraction:: DividedBy (fraction otherfraction) const
{
int tempNumerator = numerator*otherfraction.denominator;
int tempDenominator = denominator*otherfraction.numerator;
return fraction(tempNumerator, tempDenominator);
}
fraction fraction ::AddedTo (fraction otherfraction) const
{
int tempNumerator = numerator*otherfraction.denominator + otherfraction.numerator*denominator ;
int tempDenominator = denominator*otherfraction.denominator;
return fraction(tempNumerator, tempDenominator);
}
fraction fraction ::Subtract (fraction otherfraction) const
{
int tempNumerator = numerator*otherfraction.denominator - otherfraction.numerator*denominator ;
int tempDenominator = denominator*otherfraction.denominator;
return fraction(tempNumerator, tempDenominator);
}
bool fraction::isEqualTo (fraction otherfraction) const
{
return (numerator*otherfraction.denominator == otherfraction.numerator*denominator) ;
}
void fraction::normalise()
{
int factor = gcd(numerator, denominator);
numerator = numerator/factor;
denominator = denominator/factor;
}
void fraction::print() const
{
cout << "(" << numerator << "," << denominator << ")";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.