For cxample, if we create a MixedFraction object with the following valucs: whol
ID: 3756712 • Letter: F
Question
For cxample, if we create a MixedFraction object with the following valucs: whole43, numerator-264 and denominator-27 Then the constructor uses the rule and convert the values to whole- 52, numerator-7 and denominator - 9 (8 marks) (c) Overload theoperator so that the statements MixedFraction mfF1(43, 264, 27); will produce the result mfl52 7/9 (5 marks) (d) Overload the operator so that two instances of MixedFraction can be added using the + operator. A MixedFraction can be represented by three data members namely whole. numerator and denominator. All data members are integers. Identify suitable coding modules for the MixedEraction class with the following specifications. For example mfl52 7/9 m.£235 3/4 (a) Define the three data members: whole, numerator and denominator (5 marks) (3 marks) (c)Ovcrload theopcrator so that two instances of MixedFraction can be (b)Implement a parameterised constructor that receives three arguments that represent multiplied using the * operator. the values ofwhole, numerator and denominator respectively. The constructor will initialise the thrce data members with the arguments with the following rules. For examplc mfl52 7/9 mf2 = 35 3/4 mf4mflm£21886 29/36 An crror messagc is displayed if the valuc of denominator is 0. The constructor replaces the denominator with the value1 (5 marks) The constructor uses the lowes tTerm ( ) function to convert the fraction into its lowest term. () Write the lowestTerm function to to convert a a fraction into its lowest term. The constructor calls this function with two arguments: a numerator and a · denominator If the numerator is greater than or cqual to the denominator, then (5 marks) numerator is decreased by the value of denominator and the value of (g) Write the main function that will demonstrate how all the operators can be used. whole is increased by 1. This is repeated until the value of numerator is less than the value of denominator (4 marks)Explanation / Answer
//mixedFraction.h
#pragma once
#include<iostream>
using namespace std;
class mixedFraction
{
int whole;
int numerator;
int denominator;
public:
mixedFraction();
mixedFraction(int w, int n, int d);
void lowestTerm();
friend ostream& operator<<(ostream &out, mixedFraction &obj);
mixedFraction &operator+(mixedFraction &obj);
mixedFraction &operator*(mixedFraction &obj);
};
---------------------------------------------------
//mixedFraction.cpp
#include "MixedFraction.h"
mixedFraction::mixedFraction()
{
whole = 0;
numerator = 0;
denominator = 1;
}
mixedFraction::mixedFraction(int w, int n, int d)
{
if (d == 0)
{
cout << "Denominator cannot be zero ";
}
whole = w;
numerator = n;
denominator = d;
lowestTerm();
}
void mixedFraction::lowestTerm()
{
while (numerator >= denominator)
{
numerator -= denominator;
++whole;
}
//take gcd of two
int gcd;
for (int i = 1; i <= numerator && i <= denominator; ++i)
{
if (numerator % i == 0 && denominator % i == 0)
gcd = i;
}
//divide numerator and denominator by gcd
numerator /= gcd;
denominator /= gcd;
}
ostream& operator<<(ostream &out, mixedFraction &obj)
{
out << obj.whole << " " << obj.numerator << "/" << obj.denominator ;
return out;
}
mixedFraction &mixedFraction::operator+(mixedFraction &obj)
{
int w, d, n;
w = 0;
n = (whole*denominator + numerator)*obj.denominator + (obj.whole*obj.denominator + obj.numerator)*denominator;
d = denominator * obj.denominator;
mixedFraction result(w, n, d);
return result;
}
mixedFraction &mixedFraction::operator*(mixedFraction &obj)
{
int w, d, n;
w = 0;
n = (whole*denominator + numerator) * (obj.whole*obj.denominator + obj.numerator);
d = denominator * obj.denominator;
mixedFraction result(w, n, d);
return result;
}
-----------------------------------------------
//main.cpp
#include"MixedFraction.h"
int main()
{
mixedFraction m1(43, 264, 27),m2(35,3,4),m3,m4;
cout <<"m1: "<< m1<<endl;
cout << "m2: " << m2<<endl;
m3 = m1 + m2;
cout << "Addition of m1 and m2: " << m1 << " + " << m2 << " = " << m3<<endl;
m4 = m1 * m2;
cout << "Multiplication of m1 and m2: " << m1 << " * " << m2 << " = " << m4<<endl;
}
/*output
m1: 52 7/9
m2: 35 3/4
Addition of m1 and m2: 52 7/9 + 35 3/4 = 88 19/36
Multiplication of m1 and m2: 52 7/9 * 35 3/4 = 1886 29/36
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.