Mixed Fraction Classes: Exercise
Test your understanding with this exercise.
In this lesson, we’ll implement the code for the missing three fraction functions: subtraction, multiplication, and division.
Your implementation
Following the instructions, write your own implementation here:
#ifndef MIXEDFRACTION_H#define MIXEDFRACTION_H#include <string>using namespace std;class MixedFraction {private:int s; // +1 for positive, -1 for negativeint w; // Whole partint n; // Numeratorint d; // Denominatorint GCD(int a, int b);void improperFraction();void sameDenominator(MixedFraction& other);void computeSign();void convertToMixedFraction();public:MixedFraction(int sign=1, int whole=0, int numerator=0, int denominator=1);void print(const string& msg) const;MixedFraction add(const MixedFraction& other) const;// Write prorotypes of subtract, multiply and divide functions};#endif // MIXEDFRACTION_H
Exercise: Complete the MixedFraction
class
The first exercise is to add three prototypes to the MixedFraction.h
file inside the MixedFraction
class declaration.
Exercise 2: Implement the multiply()
function
Write the multiply()
function (in MixedFraction.cpp
) as a member function in the MixedFraction
class and uncomment the lines in main()
where multiply()
is called.
Exercise 3: Implement the division()
function
Write the division()
function (in MixedFraction.cpp
) as a member function in the MixedFraction
class and uncomment the lines in main()
where division()
is called.
If you have trouble getting to the solution, you can click the “Show Solution” button to see how to implement it.