Question
Can someone please help? I've have completed this up until part 5 but I don't know what to do from there (C++ Programming) In this
Can someone please help? I've have completed this up until part 5 but I don't know what to do from there
(C++ Programming)
In this lab, you are required to develop c++ class that models Fraction object which has two int data members named myNumerator and myDenominator.
Given this definition of a new type Fraction, we can make declarations like the following:
Fraction frac1, frac2, frac3;
These declarations define three Fraction objects with the following forms:
COMPLETE)Checking point 1: three constructors are required. Default constructor to set up a Fraction object as 0/1. Constructor with two ints as argument to set up the numerator and denominator. Copy constructor with another Fraction object as an argument to copy the argument fraction object. So you can define Fraction f; Fraction f(2,3); Fraction f(f1); in your test program.
(COMPLETE)Checking point 2: getter and setter functions for myNumerator and myDenominator.
COMPLETE)Checking point 3: define a toString function member for your Fraction class. so that when f is a Fraction whose numerator is 3 and whose denominator is 4, then a message:
cout
will display
3/4
COMPLETE)Checking point 4: Add an overloaded ==, and != operators to the class
Checking point 5: Add an overloaded +, -,* to the class
Checking point 6: implement the simplify member function: if class Fraction had a simplify() operation so that fractions like: 2/6, 6/12, 12/4 could be simplified to 1/3, 1/2, and 3/1, respectively.
Such an operation is useful to keep fractional results as simple and easy to read as possible. To provide this capability, we will implement a Fraction function member named simplify(). In a function member like operator*, -, +which constructs its answer in a Fraction named result, we can simplify this answer by calling simplify() as follows:
result.simplify();
1. Find gcd, the greatest common divisor of myNumerator and myDenominator.
2. Replace myNumerator by myNumerator/gcd.
3. Replace myDenominator by myDenominator/gcd.
Checking point7: Add overloaded >> and
***Here is what I have so far:
// Lab_8_vs_project.cpp
//
#include
#include
using namespace std;
class Fraction {
private:
int num;
int den;
public:
Fraction()
{
num = 0;
den = 1;
}
Fraction(int num, int den)
{
this->num = num;
this->den = den;
}
Fraction(Fraction ©)
{
this->num = copy.num;
this->den = copy.den;
}
void setNum(int num)
{
this->num = num;
}
void setDen(int den)
{
this->den = den;
}
int getNum()
{
return num;
}
int getDen()
{
return den;
}
string toString()
{
int GCD = gcd();
num /= GCD;
den /= GCD;
ostringstream res;
res
return res.str();
}
int gcd()
{
int divi = (num>den ? num : den);
int div = (num int rem = divi%div; while (rem != 0) { divi = div; div = rem; rem = divi%div; } return div; } bool operator==(Fraction &two) { if (this->num == two.num && this->den == two.den) return true; return false; } bool operator!=(Fraction &two) { if (this->num == two.num && this->den == two.den) return false; return true; } Fraction operator+(const Fraction &two) { Fraction result; result.setNum(this->den*two.num + this->num*two.den); result.setDen(this->den*two.den); int GCD = result.gcd(); result.setNum(result.getNum() / GCD); result.setDen(result.getDen() / GCD); return result; } Fraction operator-(Fraction &two) { Fraction result; result.setNum(this->num*two.den - two.num*this->den); result.setDen(this->den*two.den); int GCD = result.gcd(); result.setNum(result.getNum() / GCD); result.setDen(result.getDen() / GCD); return result; } Fraction operator*(Fraction &two) { Fraction result; result.setNum(this->num*two.num); result.setDen(this->den*two.den); int GCD = result.gcd(); result.setNum(result.getNum() / GCD); result.setDen(result.getDen() / GCD); return result; } }; int main() { Fraction one(5, 3); Fraction two(8, 6); cout cout Fraction add, sub, mul; add = one + two; sub = one - two; mul = one*two; cout cout cout cin.get(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started